Skip to content

Commit

Permalink
#4550 - Clean up code
Browse files Browse the repository at this point in the history
- Use var
  • Loading branch information
reckart committed Mar 12, 2024
1 parent d3db389 commit 74b0d82
Showing 1 changed file with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());

Expand All @@ -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());
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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 + "].");
Expand All @@ -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);
Expand All @@ -269,20 +266,22 @@ private void detatch(CAS aCas, AnnotationFS fs)
@Override
public List<Pair<LogMessage, AnnotationFS>> validate(CAS aCas)
{
List<Pair<LogMessage, AnnotationFS>> messages = new ArrayList<>();
for (SpanLayerBehavior behavior : behaviors) {
long startTime = currentTimeMillis();
var messages = new ArrayList<Pair<LogMessage, AnnotationFS>>();

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;
}
Expand Down

0 comments on commit 74b0d82

Please sign in to comment.