Skip to content

Commit

Permalink
small code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaber committed Mar 17, 2015
1 parent 4e955b5 commit 8284731
Showing 1 changed file with 19 additions and 45 deletions.
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -48,6 +49,8 @@ public class PatternToken implements Cloneable {

private final boolean caseSensitive;
private final boolean stringRegExp;
private final List<PatternToken> andGroupList = new ArrayList<>();
private final List<PatternToken> orGroupList = new ArrayList<>();

private String stringToken;
private PosToken posToken;
Expand All @@ -72,10 +75,6 @@ public class PatternToken implements Cloneable {

/** List of exceptions that are valid for a previous token. */
private List<PatternToken> previousExceptionList;
private List<PatternToken> andGroupList;
private boolean andGroupSet;
private List<PatternToken> orGroupList;
private boolean orGroupSet;

private int skip;
private int minOccurrence = 1;
Expand All @@ -86,7 +85,6 @@ public class PatternToken implements Cloneable {
/** The reference to another element in the pattern. **/
private Match tokenReference;
/** True when the element stores a formatted reference to another element of the pattern. */
private boolean containsMatches;
private String referenceString;
/** String ID of the phrase the element is in. **/
private String phraseName;
Expand All @@ -96,8 +94,6 @@ public class PatternToken implements Cloneable {
* takes most time so it's best to reduce the number of its calls.
*/
private boolean testString;
/** Tells if the element is inside the unification, so that {@link Unifier} tests it. */
private boolean unified;

/** Determines whether the element should be ignored when doing unification **/
private boolean unificationNeutral;
Expand Down Expand Up @@ -174,11 +170,9 @@ public final boolean isExceptionMatched(final AnalyzedToken token) {
* @return true if all conditions are met, false otherwise.
*/
public final boolean isAndExceptionGroupMatched(final AnalyzedToken token) {
if (andGroupSet) {
for (final PatternToken testAndGroup : andGroupList) {
if (testAndGroup.isExceptionMatched(token)) {
return true;
}
for (final PatternToken testAndGroup : andGroupList) {
if (testAndGroup.isExceptionMatched(token)) {
return true;
}
}
return false;
Expand All @@ -194,29 +188,20 @@ public final boolean isExceptionMatchedCompletely(final AnalyzedToken token) {
return isExceptionMatched(token) || isAndExceptionGroupMatched(token);
}

public final void setAndGroupElement(final PatternToken andToken) {
if (andToken != null) {
if (andGroupList == null) {
andGroupList = new ArrayList<>();
}
if (!andGroupSet) {
andGroupSet = true;
}
andGroupList.add(andToken);
}
public final void setAndGroupElement(PatternToken andToken) {
andGroupList.add(Objects.requireNonNull(andToken));
}

/**
* Checks if this element has an AND group associated with it.
* @return true if the element has a group of elements that all should match.
*/
public final boolean hasAndGroup() {
return andGroupSet;
return andGroupList.size() > 0;
}

/**
* Returns the group of elements linked with AND operator.
* @return List of Elements.
*/
public final List<PatternToken> getAndGroup() {
return andGroupList;
Expand All @@ -225,16 +210,8 @@ public final List<PatternToken> getAndGroup() {
/**
* @since 2.3
*/
public final void setOrGroupElement(final PatternToken orToken) {
if (orToken != null) {
if (orGroupList == null) {
orGroupList = new ArrayList<>();
}
if (!orGroupSet) {
orGroupSet = true;
}
orGroupList.add(orToken);
}
public final void setOrGroupElement(PatternToken orToken) {
orGroupList.add(Objects.requireNonNull(orToken));
}

/**
Expand All @@ -243,12 +220,11 @@ public final void setOrGroupElement(final PatternToken orToken) {
* @since 2.3
*/
public final boolean hasOrGroup() {
return orGroupSet;
return orGroupList.size() > 0;
}

/**
* Returns the group of elements linked with OR operator.
* @return List of Elements.
* @since 2.3
*/
public final List<PatternToken> getOrGroup() {
Expand Down Expand Up @@ -413,7 +389,7 @@ public final void setStringPosException(
setStringPosException(token, regExp, inflected, negation, scopeNext, scopePrevious, posToken, posRegExp, posNegation, Boolean.valueOf(caseSensitivity));
}

private void setException(final PatternToken elem, final boolean scopePrevious) {
private void setException(final PatternToken pToken, final boolean scopePrevious) {
exceptionValidPrevious |= scopePrevious;
if (exceptionList == null && !scopePrevious) {
exceptionList = new ArrayList<>();
Expand All @@ -422,12 +398,12 @@ private void setException(final PatternToken elem, final boolean scopePrevious)
previousExceptionList = new ArrayList<>();
}
if (scopePrevious) {
previousExceptionList.add(elem);
previousExceptionList.add(pToken);
} else {
if (!exceptionSet) {
exceptionSet = true;
}
exceptionList.add(elem);
exceptionList.add(pToken);
}
}

Expand Down Expand Up @@ -594,16 +570,15 @@ public final boolean getNegation() {
* @return true when this element refers to another token.
*/
public final boolean isReferenceElement() {
return containsMatches;
return tokenReference != null;
}

/**
* Sets the reference to another token.
* @param match Formatting object for the token reference.
*/
public final void setMatch(final Match match) {
tokenReference = match;
containsMatches = true;
tokenReference = Objects.requireNonNull(match);
}

public final Match getMatch() {
Expand Down Expand Up @@ -732,12 +707,11 @@ public final String getPhraseName() {
}

public final boolean isUnified() {
return unified;
return unificationFeatures != null;
}

public final void setUnification(final Map<String, List<String>> uniFeatures) {
unificationFeatures = uniFeatures;
unified = true;
unificationFeatures = Objects.requireNonNull(uniFeatures);
}

/**
Expand Down

0 comments on commit 8284731

Please sign in to comment.