From 74b0d8266226299c378e356a62394fc037b329c5 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Tue, 12 Mar 2024 09:45:30 +0100 Subject: [PATCH] #4550 - Clean up code - Use var --- .../annotation/layer/span/SpanAdapter.java | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/inception/inception-api-annotation/src/main/java/de/tudarmstadt/ukp/inception/annotation/layer/span/SpanAdapter.java b/inception/inception-api-annotation/src/main/java/de/tudarmstadt/ukp/inception/annotation/layer/span/SpanAdapter.java index 2ed2e4c3e23..3b62f40e623 100644 --- a/inception/inception-api-annotation/src/main/java/de/tudarmstadt/ukp/inception/annotation/layer/span/SpanAdapter.java +++ b/inception/inception-api-annotation/src/main/java/de/tudarmstadt/ukp/inception/annotation/layer/span/SpanAdapter.java @@ -30,8 +30,6 @@ import org.apache.commons.lang3.tuple.Pair; import org.apache.uima.cas.CAS; -import org.apache.uima.cas.Feature; -import org.apache.uima.cas.Type; import org.apache.uima.cas.text.AnnotationFS; import org.apache.uima.fit.util.CasUtil; import org.slf4j.Logger; @@ -99,8 +97,7 @@ public AnnotationFS add(SourceDocument aDocument, String aDataOwner, CAS aCas, i int aEnd) throws AnnotationException { - return handle( - new CreateSpanAnnotationRequest(aDocument, aDataOwner, aCas, aBegin, aEnd)); + return handle(new CreateSpanAnnotationRequest(aDocument, aDataOwner, aCas, aBegin, aEnd)); } public AnnotationFS handle(CreateSpanAnnotationRequest aRequest) throws AnnotationException @@ -152,16 +149,16 @@ public AnnotationFS move(SourceDocument aDocument, String aDocumentOwner, CAS aC public AnnotationFS handle(MoveSpanAnnotationRequest aRequest) throws AnnotationException { - MoveSpanAnnotationRequest request = aRequest; + var request = aRequest; // Adjust the move request (e.g. adjust offsets to the configured granularity) or // reject the request (e.g. reject cross-sentence annotations) - for (SpanLayerBehavior behavior : behaviors) { + for (var behavior : behaviors) { request = behavior.onMove(this, request); } - int oldBegin = request.getAnnotation().getBegin(); - int oldEnd = request.getAnnotation().getEnd(); + var oldBegin = request.getAnnotation().getBegin(); + var oldEnd = request.getAnnotation().getEnd(); moveSpanAnnotation(request.getCas(), request.getAnnotation(), request.getBegin(), request.getEnd()); @@ -176,8 +173,8 @@ public AnnotationFS handle(MoveSpanAnnotationRequest aRequest) throws Annotation private AnnotationFS createSpanAnnotation(CAS aCas, int aBegin, int aEnd) throws AnnotationException { - Type type = CasUtil.getType(aCas, getAnnotationTypeName()); - AnnotationFS newAnnotation = aCas.createAnnotation(type, aBegin, aEnd); + var type = CasUtil.getType(aCas, getAnnotationTypeName()); + var newAnnotation = aCas.createAnnotation(type, aBegin, aEnd); log.trace("Created span annotation {}-{} [{}]", newAnnotation.getBegin(), newAnnotation.getEnd(), newAnnotation.getCoveredText()); @@ -216,7 +213,7 @@ private AnnotationFS moveSpanAnnotation(CAS aCas, AnnotationFS aAnnotation, int @Override public void delete(SourceDocument aDocument, String aDocumentOwner, CAS aCas, VID aVid) { - AnnotationFS fs = selectByAddr(aCas, AnnotationFS.class, aVid.getId()); + var fs = selectByAddr(aCas, AnnotationFS.class, aVid.getId()); aCas.removeFsFromIndexes(fs); // delete associated attachFeature @@ -230,7 +227,7 @@ public void delete(SourceDocument aDocument, String aDocumentOwner, CAS aCas, VI public AnnotationFS restore(SourceDocument aDocument, String aDocumentOwner, CAS aCas, VID aVid) throws AnnotationException { - AnnotationFS fs = selectByAddr(aCas, AnnotationFS.class, aVid.getId()); + var fs = selectByAddr(aCas, AnnotationFS.class, aVid.getId()); if (getAttachFeatureName() != null) { attach(aCas, fs.getBegin(), fs.getEnd(), fs); @@ -246,8 +243,8 @@ public AnnotationFS restore(SourceDocument aDocument, String aDocumentOwner, CAS private void attach(CAS aCas, int aBegin, int aEnd, AnnotationFS newAnnotation) throws IllegalPlacementException { - Type theType = getType(aCas, getAttachTypeName()); - Feature attachFeature = theType.getFeatureByBaseName(getAttachFeatureName()); + var theType = getType(aCas, getAttachTypeName()); + var attachFeature = theType.getFeatureByBaseName(getAttachFeatureName()); if (selectCovered(aCas, theType, aBegin, aEnd).isEmpty()) { throw new IllegalPlacementException("No annotation of type [" + getAttachTypeName() + "] to attach to at location [" + aBegin + "-" + aEnd + "]."); @@ -258,8 +255,8 @@ private void attach(CAS aCas, int aBegin, int aEnd, AnnotationFS newAnnotation) private void detatch(CAS aCas, AnnotationFS fs) { - Type theType = getType(aCas, getAttachTypeName()); - Feature attachFeature = theType.getFeatureByBaseName(getAttachFeatureName()); + var theType = getType(aCas, getAttachTypeName()); + var attachFeature = theType.getFeatureByBaseName(getAttachFeatureName()); if (attachFeature != null) { selectCovered(aCas, theType, fs.getBegin(), fs.getEnd()).get(0) .setFeatureValue(attachFeature, null); @@ -269,20 +266,22 @@ private void detatch(CAS aCas, AnnotationFS fs) @Override public List> validate(CAS aCas) { - List> messages = new ArrayList<>(); - for (SpanLayerBehavior behavior : behaviors) { - long startTime = currentTimeMillis(); + var messages = new ArrayList>(); + + for (var behavior : behaviors) { + var startTime = currentTimeMillis(); messages.addAll(behavior.onValidate(this, aCas)); log.trace("Validation for [{}] on [{}] took {}ms", behavior.getClass().getSimpleName(), getLayer().getUiName(), currentTimeMillis() - startTime); } + return messages; } @Override public Selection select(VID aVid, AnnotationFS aAnno) { - Selection selection = new Selection(); + var selection = new Selection(); selection.selectSpan(aAnno); return selection; }