This is an automated email from the ASF dual-hosted git repository.

rzo1 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opennlp.git


The following commit(s) were added to refs/heads/main by this push:
     new 689e7ac5 OPENNLP-1596 Modernize immutable collection creation
689e7ac5 is described below

commit 689e7ac53a4243af23b847a8583c5d316a940a6d
Author: Martin Wiesner <[email protected]>
AuthorDate: Mon Jul 15 10:58:51 2024 +0200

    OPENNLP-1596 Modernize immutable collection creation
---
 .../lemmatizer/MorfologikLemmatizer.java           |  3 +-
 .../java/opennlp/tools/doccat/DocumentSample.java  | 11 +---
 .../formats/brat/AnnotationConfiguration.java      |  4 +-
 .../tools/formats/brat/EventAnnotation.java        |  4 +-
 .../opennlp/tools/formats/muc/MucElementNames.java | 14 ++---
 .../tools/formats/muc/MucNameContentHandler.java   | 21 +-------
 .../ontonotes/OntoNotesNameSampleStream.java       |  3 +-
 .../java/opennlp/tools/lemmatizer/LemmaSample.java |  9 ++--
 .../tools/namefind/NameFinderEventStream.java      |  6 +--
 .../java/opennlp/tools/namefind/NameSample.java    |  2 +-
 .../tools/namefind/NameSampleTypeFilter.java       |  2 +-
 .../java/opennlp/tools/tokenize/TokenSample.java   |  2 +-
 .../java/opennlp/uima/normalizer/Normalizer.java   | 59 +++++++++-------------
 .../uima/util/AnnotationComboIteratorTest.java     |  2 -
 14 files changed, 43 insertions(+), 99 deletions(-)

diff --git 
a/opennlp-morfologik-addon/src/main/java/opennlp/morfologik/lemmatizer/MorfologikLemmatizer.java
 
b/opennlp-morfologik-addon/src/main/java/opennlp/morfologik/lemmatizer/MorfologikLemmatizer.java
index 6a3b8db1..42d4ecb4 100644
--- 
a/opennlp-morfologik-addon/src/main/java/opennlp/morfologik/lemmatizer/MorfologikLemmatizer.java
+++ 
b/opennlp-morfologik-addon/src/main/java/opennlp/morfologik/lemmatizer/MorfologikLemmatizer.java
@@ -20,7 +20,6 @@ package opennlp.morfologik.lemmatizer;
 import java.io.IOException;
 import java.nio.file.Path;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Objects;
@@ -71,7 +70,7 @@ public class MorfologikLemmatizer implements Lemmatizer {
         lemmas.add(asString(wordData.getStem()));
       }
     }
-    return Collections.unmodifiableList(new ArrayList<>(lemmas));
+    return List.copyOf(lemmas);
   }
 
   private String asString(CharSequence tag) {
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/doccat/DocumentSample.java 
b/opennlp-tools/src/main/java/opennlp/tools/doccat/DocumentSample.java
index b64adb43..95d7b120 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/doccat/DocumentSample.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/doccat/DocumentSample.java
@@ -17,7 +17,6 @@
 
 package opennlp.tools.doccat;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -57,13 +56,8 @@ public class DocumentSample implements Sample {
     Objects.requireNonNull(text, "text must not be null");
 
     this.category = Objects.requireNonNull(category, "category must not be 
null");
-    this.text = Collections.unmodifiableList(new 
ArrayList<>(Arrays.asList(text)));
-
-    if (extraInformation == null) {
-      this.extraInformation = Collections.emptyMap();
-    } else {
-      this.extraInformation = extraInformation;
-    }
+    this.text = List.of(text);
+    this.extraInformation = Objects.requireNonNullElse(extraInformation, 
Collections.emptyMap());
   }
 
   /**
@@ -117,7 +111,6 @@ public class DocumentSample implements Sample {
     }
 
     if (obj instanceof DocumentSample a) {
-
       return getCategory().equals(a.getCategory())
           && Arrays.equals(getText(), a.getText());
     }
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/AnnotationConfiguration.java
 
b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/AnnotationConfiguration.java
index 35e7221e..b1c7703f 100644
--- 
a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/AnnotationConfiguration.java
+++ 
b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/AnnotationConfiguration.java
@@ -25,7 +25,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.nio.charset.StandardCharsets;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -42,8 +41,7 @@ public class AnnotationConfiguration {
   private final Map<String, String> typeToClassMap;
 
   public AnnotationConfiguration(Map<String, String> typeToClassMap) {
-
-    this.typeToClassMap = Collections.unmodifiableMap(new 
HashMap<>(typeToClassMap));
+    this.typeToClassMap = Map.copyOf(typeToClassMap);
   }
 
   public String getTypeClass(String type) {
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/EventAnnotation.java 
b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/EventAnnotation.java
index 96495698..387b8b09 100644
--- 
a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/EventAnnotation.java
+++ 
b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/EventAnnotation.java
@@ -17,8 +17,6 @@
 
 package opennlp.tools.formats.brat;
 
-import java.util.Collections;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
 
@@ -31,7 +29,7 @@ public class EventAnnotation extends BratAnnotation {
     super(id, type);
 
     this.eventTrigger = Objects.requireNonNull(eventTrigger);
-    this.arguments = Collections.unmodifiableMap(new HashMap<>(arguments));
+    this.arguments = Map.copyOf(arguments);
   }
 
   public String getEventTrigger() {
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/formats/muc/MucElementNames.java 
b/opennlp-tools/src/main/java/opennlp/tools/formats/muc/MucElementNames.java
index 6574fd04..23eb09f2 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/formats/muc/MucElementNames.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/formats/muc/MucElementNames.java
@@ -17,8 +17,6 @@
 
 package opennlp.tools.formats.muc;
 
-import java.util.Collections;
-import java.util.HashSet;
 import java.util.Set;
 
 class MucElementNames {
@@ -32,13 +30,11 @@ class MucElementNames {
   static final Set<String> CONTENT_ELEMENTS;
 
   static {
-    Set<String> contentElementNames = new HashSet<>();
-    contentElementNames.add(MucElementNames.HEADLINE_ELEMENT);
-    contentElementNames.add(MucElementNames.DATELINE_ELEMENT);
-    contentElementNames.add(MucElementNames.DD_ELEMENT);
-    contentElementNames.add(MucElementNames.SENTENCE_ELEMENT);
-
-    CONTENT_ELEMENTS = Collections.unmodifiableSet(contentElementNames);
+    CONTENT_ELEMENTS = Set.of(
+            MucElementNames.HEADLINE_ELEMENT,
+            MucElementNames.DATELINE_ELEMENT,
+            MucElementNames.DD_ELEMENT,
+            MucElementNames.SENTENCE_ELEMENT);
   }
 
   private MucElementNames() {
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/formats/muc/MucNameContentHandler.java
 
b/opennlp-tools/src/main/java/opennlp/tools/formats/muc/MucNameContentHandler.java
index 284a20b7..79734229 100644
--- 
a/opennlp-tools/src/main/java/opennlp/tools/formats/muc/MucNameContentHandler.java
+++ 
b/opennlp-tools/src/main/java/opennlp/tools/formats/muc/MucNameContentHandler.java
@@ -19,8 +19,6 @@ package opennlp.tools.formats.muc;
 
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -43,23 +41,8 @@ public class MucNameContentHandler extends 
SgmlParser.ContentHandler {
   private static final Set<String> EXPECTED_TYPES;
 
   static {
-    Set<String> types = new HashSet<>();
-
-    types.add("PERSON");
-    types.add("ORGANIZATION");
-    types.add("LOCATION");
-    types.add("DATE");
-    types.add("TIME");
-    types.add("MONEY");
-    types.add("PERCENT");
-
-    EXPECTED_TYPES = Collections.unmodifiableSet(types);
-
-    Set<String> nameElements = new HashSet<>();
-    nameElements.add(ENTITY_ELEMENT_NAME);
-    nameElements.add(TIME_ELEMENT_NAME);
-    nameElements.add(NUM_ELEMENT_NAME);
-    NAME_ELEMENT_NAMES = Collections.unmodifiableSet(nameElements);
+    EXPECTED_TYPES = Set.of("PERSON", "ORGANIZATION", "LOCATION", "DATE", 
"TIME", "MONEY", "PERCENT");
+    NAME_ELEMENT_NAMES = Set.of(ENTITY_ELEMENT_NAME, TIME_ELEMENT_NAME, 
NUM_ELEMENT_NAME);
   }
 
   private final Tokenizer tokenizer;
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/formats/ontonotes/OntoNotesNameSampleStream.java
 
b/opennlp-tools/src/main/java/opennlp/tools/formats/ontonotes/OntoNotesNameSampleStream.java
index bff97661..8dc09af8 100644
--- 
a/opennlp-tools/src/main/java/opennlp/tools/formats/ontonotes/OntoNotesNameSampleStream.java
+++ 
b/opennlp-tools/src/main/java/opennlp/tools/formats/ontonotes/OntoNotesNameSampleStream.java
@@ -37,8 +37,7 @@ import opennlp.tools.util.StringUtil;
 /**
  * Name Sample Stream parser for the OntoNotes 4.0 corpus.
  */
-public class OntoNotesNameSampleStream extends
-    FilterObjectStream<String, NameSample> {
+public class OntoNotesNameSampleStream extends FilterObjectStream<String, 
NameSample> {
 
   private final Map<String, String> tokenConversionMap;
 
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/lemmatizer/LemmaSample.java 
b/opennlp-tools/src/main/java/opennlp/tools/lemmatizer/LemmaSample.java
index 6cb70aa1..3ab1d5a7 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/lemmatizer/LemmaSample.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/lemmatizer/LemmaSample.java
@@ -17,9 +17,7 @@
 
 package opennlp.tools.lemmatizer;
 
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 
@@ -60,9 +58,9 @@ public class LemmaSample implements Sample {
 
     validateArguments(tokens.size(), tags.size(), lemmas.size());
 
-    this.tokens = Collections.unmodifiableList(new ArrayList<>(tokens));
-    this.tags = Collections.unmodifiableList(new ArrayList<>(tags));
-    this.lemmas = Collections.unmodifiableList(new ArrayList<>(lemmas));
+    this.tokens = List.copyOf(tokens);
+    this.tags = List.copyOf(tags);
+    this.lemmas = List.copyOf(lemmas);
   }
 
   /**
@@ -120,7 +118,6 @@ public class LemmaSample implements Sample {
     }
 
     if (obj instanceof LemmaSample a) {
-
       return Arrays.equals(getTokens(), a.getTokens())
           && Arrays.equals(getTags(), a.getTags())
           && Arrays.equals(getLemmas(), a.getLemmas());
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/namefind/NameFinderEventStream.java 
b/opennlp-tools/src/main/java/opennlp/tools/namefind/NameFinderEventStream.java
index d6361b34..81ffd6d8 100644
--- 
a/opennlp-tools/src/main/java/opennlp/tools/namefind/NameFinderEventStream.java
+++ 
b/opennlp-tools/src/main/java/opennlp/tools/namefind/NameFinderEventStream.java
@@ -58,11 +58,7 @@ public class NameFinderEventStream extends 
opennlp.tools.util.AbstractEventStrea
                                NameContextGenerator contextGenerator, 
SequenceCodec<String> codec) {
     super(dataStream);
 
-    if (codec == null) {
-      this.codec = new BioCodec();
-    } else {
-      this.codec = codec;
-    }
+    this.codec = Objects.requireNonNullElseGet(codec, BioCodec::new);
     this.contextGenerator = contextGenerator;
     this.contextGenerator.addFeatureGenerator(
         new WindowFeatureGenerator(additionalContextFeatureGenerator, 8, 8));
diff --git a/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSample.java 
b/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSample.java
index dd503b7d..db57fce7 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSample.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSample.java
@@ -66,7 +66,7 @@ public class NameSample implements Sample {
       names = new Span[0];
     }
 
-    this.sentence = Collections.unmodifiableList(new 
ArrayList<>(Arrays.asList(sentence)));
+    this.sentence = List.of(sentence);
     List<Span> namesList = Arrays.asList(names);
     Collections.sort(namesList);
     this.names = Collections.unmodifiableList(namesList);
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSampleTypeFilter.java 
b/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSampleTypeFilter.java
index 4b3f6228..bb818553 100644
--- 
a/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSampleTypeFilter.java
+++ 
b/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSampleTypeFilter.java
@@ -56,7 +56,7 @@ public class NameSampleTypeFilter extends 
FilterObjectStream<NameSample, NameSam
    */
   public NameSampleTypeFilter(Set<String> types, ObjectStream<NameSample> 
samples) {
     super(samples);
-    this.types = Collections.unmodifiableSet(new HashSet<>(types));
+    this.types = Set.copyOf(types);
   }
 
   @Override
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenSample.java 
b/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenSample.java
index a4aca08f..591888b9 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenSample.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenSample.java
@@ -53,7 +53,7 @@ public class TokenSample implements Sample {
     Objects.requireNonNull(tokenSpans, "tokenSpans must not be null");
 
     this.text = Objects.requireNonNull(text, "text must not be null");
-    this.tokenSpans = Collections.unmodifiableList(new 
ArrayList<>(Arrays.asList(tokenSpans)));
+    this.tokenSpans = List.of(tokenSpans);
 
     for (Span tokenSpan : tokenSpans) {
       if (tokenSpan.getStart() < 0 || tokenSpan.getStart() > text.length() ||
diff --git a/opennlp-uima/src/main/java/opennlp/uima/normalizer/Normalizer.java 
b/opennlp-uima/src/main/java/opennlp/uima/normalizer/Normalizer.java
index c303a02e..f90e6fce 100644
--- a/opennlp-uima/src/main/java/opennlp/uima/normalizer/Normalizer.java
+++ b/opennlp-uima/src/main/java/opennlp/uima/normalizer/Normalizer.java
@@ -20,8 +20,6 @@ package opennlp.uima.normalizer;
 import java.io.IOException;
 import java.io.InputStream;
 import java.text.ParseException;
-import java.util.Collections;
-import java.util.HashSet;
 import java.util.Set;
 
 import org.apache.uima.UimaContext;
@@ -65,17 +63,10 @@ public class Normalizer extends CasAnnotator_ImplBase {
   private static final Set<String> SUPPORTED_TYPES;
 
   static {
-    Set<String> supportedTypes = new HashSet<>();
-
-    supportedTypes.add(CAS.TYPE_NAME_STRING);
-    supportedTypes.add(CAS.TYPE_NAME_BYTE);
-    supportedTypes.add(CAS.TYPE_NAME_SHORT);
-    supportedTypes.add(CAS.TYPE_NAME_INTEGER);
-    supportedTypes.add(CAS.TYPE_NAME_LONG);
-    supportedTypes.add(CAS.TYPE_NAME_FLOAT);
-    supportedTypes.add(CAS.TYPE_NAME_DOUBLE);
-
-    SUPPORTED_TYPES = Collections.unmodifiableSet(supportedTypes);
+    SUPPORTED_TYPES = Set.of(CAS.TYPE_NAME_STRING,
+            CAS.TYPE_NAME_BYTE, CAS.TYPE_NAME_SHORT,
+            CAS.TYPE_NAME_INTEGER, CAS.TYPE_NAME_LONG,
+            CAS.TYPE_NAME_FLOAT, CAS.TYPE_NAME_DOUBLE);
   }
 
   private UimaContext context;
@@ -100,23 +91,24 @@ public class Normalizer extends CasAnnotator_ImplBase {
   private StringDictionary mLookupDictionary;
 
   /**
-   * Initializes a new instance.
-   * <p>
-   * Note: Use {@link #initialize(UimaContext) } to initialize this instance. 
Not
-   * use the constructor.
+   * Initializes a {@link Normalizer} instance.
+   *
+   * @apiNote Use {@link #initialize(UimaContext)} to initialize this instance.
+   * Do not use the constructor.
    */
-  public Normalizer() {
+  private Normalizer() {
     // must not be implemented !
   }
 
   /**
-   * Initializes the current instance with the given context.
+   * Initializes the current instance with the given {@link UimaContext 
context}.
    * <p>
-   * Note: Do all initialization in this method, do not use the constructor.
    * @param context context to initialize
+   * @throws ResourceInitializationException Thrown if errors occurred during 
initialization of resources.
+   *
+   * @implNote Do all initialization in this method, do not use the 
constructor.
    */
-  public void initialize(UimaContext context)
-      throws ResourceInitializationException {
+  public void initialize(UimaContext context) throws 
ResourceInitializationException {
 
     super.initialize(context);
 
@@ -201,8 +193,9 @@ public class Normalizer extends CasAnnotator_ImplBase {
           text = normalizedText;
         }
       }
+      String name = mStructureFeature.getRange().getName();
 
-      if (CAS.TYPE_NAME_STRING.equals(mStructureFeature.getRange().getName())) 
{
+      if (CAS.TYPE_NAME_STRING.equals(name)) {
         nameAnnotation.setStringValue(mStructureFeature, text);
       } else {
 
@@ -216,24 +209,18 @@ public class Normalizer extends CasAnnotator_ImplBase {
           continue;
         }
 
-        if (CAS.TYPE_NAME_BYTE.equals(mStructureFeature.getRange().getName())) 
{
+        if (CAS.TYPE_NAME_BYTE.equals(name)) {
           nameAnnotation.setByteValue(mStructureFeature, number.byteValue());
-        } else if (CAS.TYPE_NAME_SHORT.equals(mStructureFeature.getRange()
-            .getName())) {
+        } else if (CAS.TYPE_NAME_SHORT.equals(name)) {
           nameAnnotation.setShortValue(mStructureFeature, number.shortValue());
-        } else if (CAS.TYPE_NAME_INTEGER.equals(mStructureFeature.getRange()
-            .getName())) {
+        } else if (CAS.TYPE_NAME_INTEGER.equals(name)) {
           nameAnnotation.setIntValue(mStructureFeature, number.intValue());
-        } else if (CAS.TYPE_NAME_LONG.equals(mStructureFeature.getRange()
-            .getName())) {
+        } else if (CAS.TYPE_NAME_LONG.equals(name)) {
           nameAnnotation.setLongValue(mStructureFeature, number.longValue());
-        } else if (CAS.TYPE_NAME_FLOAT.equals(mStructureFeature.getRange()
-            .getName())) {
+        } else if (CAS.TYPE_NAME_FLOAT.equals(name)) {
           nameAnnotation.setFloatValue(mStructureFeature, number.floatValue());
-        } else if (CAS.TYPE_NAME_DOUBLE.equals(mStructureFeature.getRange()
-            .getName())) {
-          nameAnnotation
-              .setDoubleValue(mStructureFeature, number.doubleValue());
+        } else if (CAS.TYPE_NAME_DOUBLE.equals(name)) {
+          nameAnnotation.setDoubleValue(mStructureFeature, 
number.doubleValue());
         }
       }
     }
diff --git 
a/opennlp-uima/src/test/java/opennlp/uima/util/AnnotationComboIteratorTest.java 
b/opennlp-uima/src/test/java/opennlp/uima/util/AnnotationComboIteratorTest.java
index b107673f..e3204fe8 100644
--- 
a/opennlp-uima/src/test/java/opennlp/uima/util/AnnotationComboIteratorTest.java
+++ 
b/opennlp-uima/src/test/java/opennlp/uima/util/AnnotationComboIteratorTest.java
@@ -39,8 +39,6 @@ public class AnnotationComboIteratorTest {
    * <p>
    * The iterator was either crashing with a NoSuchElementException or it just 
left
    * out the first token in the next sentence.
-   *
-   * @throws IOException
    */
   @Test
   public void OPENNLP_676() throws IOException {

Reply via email to