View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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  
17  package net.sf.ehcache.search.expression;
18  
19  import net.sf.ehcache.search.SearchException;
20  import net.sf.ehcache.search.attribute.AttributeType;
21  
22  /***
23   * Range criteria
24   *
25   * @author teck
26   */
27  public class Between extends ComparableValue {
28  
29      private final Comparable min;
30      private final Comparable max;
31      private final boolean minInclusive;
32      private final boolean maxInclusive;
33  
34      /***
35       * Constructor
36       *
37       * @param attributeName attribute name
38       * @param min           minimum value of range
39       * @param max           maximum value of range
40       * @param minInclusive  is minimum inclusive?
41       * @param maxInclusive  is maximum inclusive?
42       */
43      public Between(String attributeName, Object min, Object max, boolean minInclusive, boolean maxInclusive) {
44          super(attributeName, computeType(attributeName, min, max));
45  
46          this.min = (Comparable) min;
47          this.max = (Comparable) max;
48          this.minInclusive = minInclusive;
49          this.maxInclusive = maxInclusive;
50      }
51  
52      private static AttributeType computeType(String attributeName, Object min, Object max) {
53          if ((min == null) || (max == null)) {
54              throw new NullPointerException();
55          }
56  
57          AttributeType minType = AttributeType.typeFor(attributeName, min);
58          AttributeType maxType = AttributeType.typeFor(attributeName, max);
59  
60          if (minType != maxType) {
61              throw new SearchException("Different types for min (" + minType + ") and max (" + maxType + ")");
62          }
63  
64          return minType;
65      }
66  
67      /***
68       * Get the minimum value
69       *
70       * @return min value
71       */
72      public Comparable getMin() {
73          return min;
74      }
75  
76      /***
77       * Get the maximum value
78       *
79       * @return max value
80       */
81      public Comparable getMax() {
82          return max;
83      }
84  
85      /***
86       * @return true if the min is included in range
87       */
88      public boolean isMinInclusive() {
89          return minInclusive;
90      }
91  
92      /***
93       * @return true if the max is included in range
94       */
95      public boolean isMaxInclusive() {
96          return maxInclusive;
97      }
98  
99      /***
100      * {@inheritDoc}
101      */
102     @Override
103     protected boolean executeComparable(Comparable attributeValue) {
104         int minCmp = attributeValue.compareTo(min);
105         if (minCmp < 0 || minCmp == 0 && !minInclusive) {
106             return false;
107         }
108 
109         int maxCmp = attributeValue.compareTo(max);
110         if (maxCmp > 0 || maxCmp == 0 && !maxInclusive) {
111             return false;
112         }
113 
114         return true;
115     }
116 
117     /***
118      * {@inheritDoc}
119      */
120     @Override
121     protected boolean executeComparableString(Comparable attributeValue) {
122         int minCmp = luceneStringCompare(attributeValue.toString(), min.toString());
123         if (minCmp < 0 || minCmp == 0 && !minInclusive) {
124             return false;
125         }
126 
127         int maxCmp = luceneStringCompare(attributeValue.toString(), max.toString());
128         if (maxCmp > 0 || maxCmp == 0 && !maxInclusive) {
129             return false;
130         }
131 
132         return true;
133     }
134 
135 }