2121import java .util .Iterator ;
2222import java .util .NoSuchElementException ;
2323import java .util .Objects ;
24+ import java .util .stream .Collectors ;
2425
2526import opennlp .tools .util .jvm .StringInterners ;
2627
3031public class StringList implements Iterable <String > {
3132
3233 private final String [] tokens ;
33-
3434 private final boolean caseSensitive ;
3535
36+ // It is safe to use caching of the hashCode for this class
37+ private transient Integer hashCode = null ; // initial value is uncomputed
38+
3639 /**
3740 * Initializes a {@link StringList} instance. By default, this instance is case-sensitive.
3841 * <p>
@@ -53,7 +56,7 @@ public StringList(String singleToken) {
5356 *
5457 * @param tokens The string parts of the new {@link StringList}.
5558 * Must not be an empty tokens array or {@code null}.
56- *
59+ *
5760 * @throws IllegalArgumentException Thrown if parameters were invalid.
5861 */
5962 public StringList (String ... tokens ) {
@@ -73,15 +76,13 @@ public StringList(String... tokens) {
7376 * @throws IllegalArgumentException Thrown if parameters were invalid.
7477 */
7578 public StringList (boolean isCaseSensitive , String ... tokens ) {
76-
7779 Objects .requireNonNull (tokens , "tokens must not be null" );
7880
7981 if (tokens .length == 0 ) {
8082 throw new IllegalArgumentException ("tokens must not be empty" );
8183 }
8284
8385 this .tokens = new String [tokens .length ];
84-
8586 for (int i = 0 ; i < tokens .length ; i ++) {
8687 this .tokens [i ] = StringInterners .intern (tokens [i ]);
8788 }
@@ -161,8 +162,11 @@ public boolean compareToIgnoreCase(StringList tokens) {
161162
162163 @ Override
163164 public int hashCode () {
164- // if lookup is too slow optimize this
165- return StringUtil .toLowerCase (toString ()).hashCode ();
165+ if (hashCode == null ) {
166+ // compute once and cache to safe CPU cycles during use
167+ this .hashCode = StringUtil .toLowerCase (String .join ("," , tokens )).hashCode ();
168+ }
169+ return hashCode ;
166170 }
167171
168172 @ Override
@@ -184,21 +188,7 @@ public boolean equals(Object obj) {
184188 */
185189 @ Override
186190 public String toString () {
187- StringBuilder string = new StringBuilder ();
188-
189- string .append ('[' );
190-
191- for (int i = 0 ; i < size (); i ++) {
192- string .append (getToken (i ));
193-
194- if (i < size () - 1 ) {
195- string .append (',' );
196- }
197- }
198-
199- string .append (']' );
200-
201- return string .toString ();
191+ return Arrays .stream (tokens ).collect (Collectors .joining ("," , "[" , "]" ));
202192 }
203193
204194 /**
0 commit comments