55
66import com .deepl .api .utils .*;
77import java .util .*;
8+ import java .util .function .BiFunction ;
89import org .jetbrains .annotations .*;
910
1011/** Stores the entries of a glossary. */
@@ -45,8 +46,6 @@ public static GlossaryEntries fromTsv(String tsv) {
4546 } else {
4647 String sourceTerm = trimWhitespace (splitLine [0 ]);
4748 String targetTerm = trimWhitespace (splitLine [1 ]);
48- validateGlossaryTerm (sourceTerm );
49- validateGlossaryTerm (targetTerm );
5049 if (result .containsKey (sourceTerm )) {
5150 throw new IllegalArgumentException (
5251 String .format (
@@ -98,17 +97,33 @@ public String get(Object key) {
9897 }
9998
10099 /**
101- * Adds the given source term and target term to the glossary entries.
100+ * Adds the given source term and target term to the glossary entries. Both terms are trimmed of
101+ * leading and trailing whitespace, then checked with {@link #validateGlossaryTerm}; the trimmed
102+ * terms are stored.
102103 *
103104 * @param sourceTerm key with which the specified value is to be associated
104105 * @param targetTerm value to be associated with the specified key
105106 * @return The previous target term associated with this source term, or null if this source term
106107 * was not present.
107108 */
108109 public String put (String sourceTerm , String targetTerm ) throws IllegalArgumentException {
109- validateGlossaryTerm (sourceTerm );
110- validateGlossaryTerm (targetTerm );
111- return entries .put (sourceTerm , targetTerm );
110+ String source = checkedTerm (sourceTerm );
111+ return entries .put (source , checkedTerm (targetTerm ));
112+ }
113+
114+ /**
115+ * Replaces each target term with the result of the given function. Every new term is trimmed and
116+ * checked like in {@link #put}; if any is invalid, no entry is changed.
117+ */
118+ @ Override
119+ public void replaceAll (BiFunction <? super String , ? super String , ? extends String > function ) {
120+ Map <String , String > replaced = new HashMap <>();
121+ for (Map .Entry <String , String > entryPair : entries .entrySet ()) {
122+ replaced .put (
123+ entryPair .getKey (),
124+ checkedTerm (function .apply (entryPair .getKey (), entryPair .getValue ())));
125+ }
126+ entries .putAll (replaced );
112127 }
113128
114129 @ Override
@@ -140,10 +155,101 @@ public Collection<String> values() {
140155 return entries .values ();
141156 }
142157
158+ /**
159+ * Returns a view of the entries. {@link Map.Entry#setValue} trims and checks the new target term
160+ * like {@link #put}.
161+ */
143162 @ NotNull
144163 @ Override
145164 public Set <Entry <String , String >> entrySet () {
146- return entries .entrySet ();
165+ Set <Entry <String , String >> backing = entries .entrySet ();
166+ return new AbstractSet <Entry <String , String >>() {
167+ @ Override
168+ public @ NotNull Iterator <Entry <String , String >> iterator () {
169+ Iterator <Entry <String , String >> iterator = backing .iterator ();
170+ return new Iterator <Entry <String , String >>() {
171+ @ Override
172+ public boolean hasNext () {
173+ return iterator .hasNext ();
174+ }
175+
176+ @ Override
177+ public Entry <String , String > next () {
178+ return new CheckedEntry (iterator .next ());
179+ }
180+
181+ @ Override
182+ public void remove () {
183+ iterator .remove ();
184+ }
185+ };
186+ }
187+
188+ @ Override
189+ public int size () {
190+ return backing .size ();
191+ }
192+
193+ @ Override
194+ public boolean contains (Object o ) {
195+ return backing .contains (o );
196+ }
197+
198+ @ Override
199+ public boolean remove (Object o ) {
200+ return backing .remove (o );
201+ }
202+
203+ @ Override
204+ public void clear () {
205+ backing .clear ();
206+ }
207+ };
208+ }
209+
210+ /** An entry of the backing map whose setValue checks the new target term. */
211+ private static final class CheckedEntry implements Map .Entry <String , String > {
212+ private final Map .Entry <String , String > entry ;
213+
214+ CheckedEntry (Map .Entry <String , String > entry ) {
215+ this .entry = entry ;
216+ }
217+
218+ @ Override
219+ public String getKey () {
220+ return entry .getKey ();
221+ }
222+
223+ @ Override
224+ public String getValue () {
225+ return entry .getValue ();
226+ }
227+
228+ @ Override
229+ public String setValue (String value ) {
230+ return entry .setValue (checkedTerm (value ));
231+ }
232+
233+ @ Override
234+ public boolean equals (Object o ) {
235+ return entry .equals (o );
236+ }
237+
238+ @ Override
239+ public int hashCode () {
240+ return entry .hashCode ();
241+ }
242+
243+ @ Override
244+ public String toString () {
245+ return entry .toString ();
246+ }
247+ }
248+
249+ /** Validates the given term and returns it trimmed. */
250+ private static String checkedTerm (String term ) {
251+ validateGlossaryTerm (term );
252+ return trimWhitespace (term );
147253 }
148254
149255 /**
@@ -165,7 +271,7 @@ public static void validateGlossaryTerm(String term) throws IllegalArgumentExcep
165271 if ((ch <= 31 ) || (128 <= ch && ch <= 159 ) || ch == '\u2028' || ch == '\u2029' ) {
166272 throw new IllegalArgumentException (
167273 String .format (
168- "Term '%s' contains invalid character: '%c' (U+%04d )" , term , ch , (int ) ch ));
274+ "Term '%s' contains invalid character: '%c' (U+%04X )" , term , ch , (int ) ch ));
169275 }
170276 }
171277 }
@@ -188,30 +294,21 @@ public String toTsv() {
188294 }
189295
190296 /**
191- * Strips whitespace characters from the beginning and end of the given string. Implemented here
192- * because String.strip() is not available in Java 8.
297+ * Strips whitespace as defined by {@link Character#isWhitespace} from the beginning and end of
298+ * the given string, like spoken terms. Implemented here because String.strip() is not in Java 8.
193299 *
194300 * @param input String to have whitespace trimmed.
195301 * @return Input string with whitespace removed from ends.
196302 */
197303 private static String trimWhitespace (String input ) {
198- int left = 0 ;
199- for (; left < input .length (); left ++) {
200- char ch = input .charAt (left );
201- if (ch != ' ' && ch != '\t' ) {
202- break ;
203- }
304+ int start = 0 ;
305+ int end = input .length ();
306+ while (start < end && Character .isWhitespace (input .charAt (start ))) {
307+ start ++;
204308 }
205- if (left >= input .length ()) {
206- return "" ;
207- }
208- int right = input .length () - 1 ;
209- for (; left < right ; right --) {
210- char ch = input .charAt (right );
211- if (ch != ' ' && ch != '\t' ) {
212- break ;
213- }
309+ while (end > start && Character .isWhitespace (input .charAt (end - 1 ))) {
310+ end --;
214311 }
215- return input .substring (left , right + 1 );
312+ return input .substring (start , end );
216313 }
217314}
0 commit comments