Skip to content

Commit

Permalink
#2196 Implement equals() of query models in terms of tokens provide…
Browse files Browse the repository at this point in the history
…d by the underlying token source
  • Loading branch information
homedirectory committed Mar 18, 2024
1 parent 25901fe commit 81289d3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.apache.commons.lang3.StringUtils;
import ua.com.fielden.platform.eql.antlr.tokens.util.ListTokenSource;

import java.util.Objects;

import static java.lang.String.format;

public abstract class AbstractModel {
Expand All @@ -22,13 +20,15 @@ public final ListTokenSource getTokenSource() {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((tokenSource == null) ? 0 : tokenSource.hashCode());
result = prime * result + ((tokenSource == null) ? 0 : tokenSource.tokens().hashCode());
return result;
}

@Override
public boolean equals(final Object obj) {
return this == obj || obj instanceof AbstractModel other && Objects.equals(tokenSource, other.tokenSource);
return this == obj || obj instanceof AbstractModel other &&
((tokenSource == null && other.tokenSource == null) ||
(tokenSource != null && other.tokenSource != null && tokenSource.equalTokens(other.tokenSource)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public ListTokenSource(final List<? extends Token> tokens, final String sourceNa
super(tokens, sourceName);
}

/**
* Returns a complete view on tokens provided by this source without advancing the current token position.
*/
public List<Token> tokens() {
return unmodifiableList(tokens);
}
Expand All @@ -36,4 +39,11 @@ public ListTokenSource restart() {
return i == 0 ? this : new ListTokenSource(tokens, getSourceName());
}

/**
* Determines whether this source provides the same tokens as the given one.
*/
public boolean equalTokens(final ListTokenSource that) {
return tokens().equals(that.tokens());
}

}

0 comments on commit 81289d3

Please sign in to comment.