View Javadoc

1   /*
2    * Copyright 2006-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sourceforge.domian.specification;
17  
18  
19  import org.apache.commons.lang.NotImplementedException;
20  import org.apache.commons.lang.Validate;
21  
22  import static java.lang.Boolean.FALSE;
23  import static java.lang.Boolean.TRUE;
24  import static net.sourceforge.domian.util.ReflectionUtils.canCastAtLeastOneWay;
25  
26  
27  /**
28   * Specification for string approval.
29   * <p/>
30   * This specification is value bound.
31   * The value is a string, and candidate string objects must be equal to this value to be approved by this specification.
32   * Letter case is irrelevant.
33   *
34   * @author Eirik Torske
35   * @since 0.1
36   */
37  final class EqualIgnoreCaseStringSpecification extends AbstractValueBoundSpecification<String> {
38  
39      EqualIgnoreCaseStringSpecification(final String value) {
40          super(value);
41      }
42  
43      @Override
44      protected RelationalOperator getBinaryRelation(final Boolean negated) {
45          throw new NotImplementedException();
46      }
47  
48      @Override
49      protected Boolean getDisjointDecision(final RelationalOperator candidateRelationalOperator, final String candidateOperandValue) {
50          throw new NotImplementedException();
51      }
52  
53      @Override
54      public Boolean isSatisfiedBy(final String candidate) {
55          return candidate != null && this.value.equalsIgnoreCase(candidate);
56      }
57  
58      @Override
59      public Boolean isDisjointWith(final Specification<?> specification) {
60          Validate.notNull(specification, "Specification parameter cannot be null");
61          // If same/equal specifications:
62          if (this == specification || this.equals(specification)) {
63              return FALSE;
64          }
65          // If specifications are not of the same type:
66          if (!canCastAtLeastOneWay(this.getType(), specification.getType())) {
67              return TRUE;
68          }
69          if (specification instanceof EqualSpecification) {
70              if ((((EqualSpecification) specification).getType().equals(String.class))) {
71                  return !this.value.equalsIgnoreCase((String) ((EqualSpecification) specification).getValue());
72              }
73          }
74          if (specification instanceof EqualIgnoreCaseStringSpecification) {
75              return !this.value.equalsIgnoreCase(((EqualIgnoreCaseStringSpecification) specification).getValue());
76          }
77          if (specification instanceof WildcardExpressionMatcherStringSpecification) {
78              WildcardExpressionMatcherIgnoreCaseStringSpecification ignoreCaseSpecificationVersion =
79                      new WildcardExpressionMatcherIgnoreCaseStringSpecification(((WildcardExpressionMatcherStringSpecification) specification).originalWildcardExpression);
80              return !ignoreCaseSpecificationVersion.isSatisfiedBy(this.value);
81          }
82          return TRUE;
83      }
84  }