Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPENNLP-1596 Modernize immutable collection creation #638

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand Down Expand Up @@ -71,7 +70,7 @@ private List<String> lemmatize(String word, String postag) {
lemmas.add(asString(wordData.getStem()));
}
}
return Collections.unmodifiableList(new ArrayList<>(lemmas));
return List.copyOf(lemmas);
}

private String asString(CharSequence tag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package opennlp.tools.doccat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -57,13 +56,8 @@ public DocumentSample(String category, String[] text, Map<String, Object> extraI
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());
}

/**
Expand Down Expand Up @@ -117,7 +111,6 @@ public boolean equals(Object obj) {
}

if (obj instanceof DocumentSample a) {

return getCategory().equals(a.getCategory())
&& Arrays.equals(getText(), a.getText());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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;

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,7 +29,7 @@ protected EventAnnotation(String id, String type, String eventTrigger, Map<Strin
super(id, type);

this.eventTrigger = Objects.requireNonNull(eventTrigger);
this.arguments = Collections.unmodifiableMap(new HashMap<>(arguments));
this.arguments = Map.copyOf(arguments);
}

public String getEventTrigger() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package opennlp.tools.formats.muc;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

class MucElementNames {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
/**
* 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -60,9 +58,9 @@ public LemmaSample(List<String> tokens, List<String> tags, List<String> lemmas)

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);
}

/**
Expand Down Expand Up @@ -120,7 +118,6 @@ public boolean equals(Object obj) {
}

if (obj instanceof LemmaSample a) {

return Arrays.equals(getTokens(), a.getTokens())
&& Arrays.equals(getTags(), a.getTags())
&& Arrays.equals(getLemmas(), a.getLemmas());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ public NameFinderEventStream(ObjectStream<NameSample> dataStream, String type,
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public NameSample(String id, String[] sentence, Span[] names, String[][] additio
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public NameSampleTypeFilter(String[] types, ObjectStream<NameSample> samples) {
*/
public NameSampleTypeFilter(Set<String> types, ObjectStream<NameSample> samples) {
super(samples);
this.types = Collections.unmodifiableSet(new HashSet<>(types));
this.types = Set.copyOf(types);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public TokenSample(String text, Span[] tokenSpans) {
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() ||
Expand Down
59 changes: 23 additions & 36 deletions opennlp-uima/src/main/java/opennlp/uima/normalizer/Normalizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -201,8 +193,9 @@ public void process(CAS tcas) {
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 {

Expand All @@ -216,24 +209,18 @@ public void process(CAS tcas) {
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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down