|
| 1 | +package com.mmnaseri.utils.spring.data.domain.impl; |
| 2 | + |
| 3 | +import com.mmnaseri.utils.spring.data.domain.Operator; |
| 4 | +import com.mmnaseri.utils.spring.data.domain.OperatorContext; |
| 5 | +import com.mmnaseri.utils.spring.data.domain.impl.matchers.*; |
| 6 | +import com.mmnaseri.utils.spring.data.error.DuplicateOperatorException; |
| 7 | +import org.apache.commons.logging.Log; |
| 8 | +import org.apache.commons.logging.LogFactory; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.concurrent.CopyOnWriteArraySet; |
| 13 | + |
| 14 | +/** |
| 15 | + * This class is used to store the operators used in the name of a query method. Operators are matched by |
| 16 | + * the "suffixes" eagerly (meaning that "EqualTo" will precede over "To"). |
| 17 | + * |
| 18 | + * @author Milad Naseri ([email protected]) |
| 19 | + * @since 1.0 (9/29/15) |
| 20 | + */ |
| 21 | +public class DefaultOperatorContext implements OperatorContext { |
| 22 | + |
| 23 | + private static final Log log = LogFactory.getLog(DefaultOperatorContext.class); |
| 24 | + private final Set<Operator> operators; |
| 25 | + |
| 26 | + public DefaultOperatorContext() { |
| 27 | + this(true); |
| 28 | + } |
| 29 | + |
| 30 | + public DefaultOperatorContext(boolean registerDefaults) { |
| 31 | + operators = new CopyOnWriteArraySet<>(); |
| 32 | + if (registerDefaults) { |
| 33 | + log.info("Registering all the default operators"); |
| 34 | + operators.add(new ImmutableOperator("AFTER", 1, new IsGreaterThanMatcher(), "After", "IsAfter")); |
| 35 | + operators.add(new ImmutableOperator("BEFORE", 1, new IsLessThanMatcher(), "Before", "IsBefore")); |
| 36 | + operators.add(new ImmutableOperator("CONTAINING", 1, new ContainingMatcher(), "Containing", "IsContaining", "Contains")); |
| 37 | + operators.add(new ImmutableOperator("BETWEEN", 2, new IsBetweenMatcher(), "Between", "IsBetween")); |
| 38 | + operators.add(new ImmutableOperator("NOT_BETWEEN", 2, new IsNotBetweenMatcher(), "NotBetween", "IsNotBetween")); |
| 39 | + operators.add(new ImmutableOperator("ENDING_WITH", 1, new EndingWithMatcher(), "EndingWith", "IsEndingWith", "EndsWith")); |
| 40 | + operators.add(new ImmutableOperator("FALSE", 0, new IsFalseMatcher(), "False", "IsFalse")); |
| 41 | + operators.add(new ImmutableOperator("GREATER_THAN", 1, new IsGreaterThanMatcher(), "GreaterThan", "IsGreaterThan")); |
| 42 | + operators.add(new ImmutableOperator("GREATER_THAN_EQUALS", 1, new IsGreaterThanOrEqualToMatcher(), "GreaterThanEqual", "IsGreaterThanEqual")); |
| 43 | + operators.add(new ImmutableOperator("IN", 1, new IsInMatcher(), "In", "IsIn")); |
| 44 | + operators.add(new ImmutableOperator("IS", 1, new IsEqualToMatcher(), "Is", "EqualTo", "IsEqualTo", "Equals")); |
| 45 | + operators.add(new ImmutableOperator("NOT_NULL", 0, new IsNotNullMatcher(), "NotNull", "IsNotNull")); |
| 46 | + operators.add(new ImmutableOperator("NULL", 0, new IsNullMatcher(), "Null", "IsNull")); |
| 47 | + operators.add(new ImmutableOperator("LESS_THAN", 1, new IsLessThanMatcher(), "LessThan", "IsLessThan")); |
| 48 | + operators.add(new ImmutableOperator("LESS_THAN_EQUAL", 1, new IsLessThanOrEqualToMatcher(), "LessThanEqual", "IsLessThanEqual")); |
| 49 | + operators.add(new ImmutableOperator("LIKE", 1, new IsLikeMatcher(), "Like", "IsLike")); |
| 50 | + operators.add(new ImmutableOperator("NEAR", 1, null, "Near", "IsNear")); |
| 51 | + operators.add(new ImmutableOperator("NOT", 1, new IsNotMatcher(), "IsNot", "Not", "IsNotEqualTo", "DoesNotEqual")); |
| 52 | + operators.add(new ImmutableOperator("NOT_IN", 1, new IsNotInMatcher(), "NotIn", "IsNotIn")); |
| 53 | + operators.add(new ImmutableOperator("NOT_LIKE", 1, new IsNotLikeMatcher(), "NotLike", "IsNotLike")); |
| 54 | + operators.add(new ImmutableOperator("REGEX", 1, new RegexMatcher(), "Regex", "MatchesRegex", "Matches")); |
| 55 | + operators.add(new ImmutableOperator("STARTING_WITH", 1, new StartingWithMatcher(), "StartingWith", "IsStartingWith", "StartsWith")); |
| 56 | + operators.add(new ImmutableOperator("TRUE", 0, new IsTrueMatcher(), "True", "IsTrue")); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void register(Operator operator) { |
| 62 | + log.info("Registering operator " + operator.getName() + " which will respond to suffixes " + Arrays.toString(operator.getTokens())); |
| 63 | + for (Operator item : operators) { |
| 64 | + for (String token : item.getTokens()) { |
| 65 | + for (String newToken : operator.getTokens()) { |
| 66 | + if (newToken.equals(token)) { |
| 67 | + throw new DuplicateOperatorException(item, token); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + operators.add(operator); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Operator getBySuffix(String suffix) { |
| 77 | + for (Operator operator : operators) { |
| 78 | + for (String token : operator.getTokens()) { |
| 79 | + if (token.equals(suffix)) { |
| 80 | + return operator; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments