You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Compare.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data.util.filter;
  17. import com.vaadin.data.Container.Filter;
  18. import com.vaadin.data.Item;
  19. import com.vaadin.data.Property;
  20. /**
  21. * Simple container filter comparing an item property value against a given
  22. * constant value. Use the nested classes {@link Equal}, {@link Greater},
  23. * {@link Less}, {@link GreaterOrEqual} and {@link LessOrEqual} instead of this
  24. * class directly.
  25. *
  26. * This filter also directly supports in-memory filtering.
  27. *
  28. * The reference and actual values must implement {@link Comparable} and the
  29. * class of the actual property value must be assignable from the class of the
  30. * reference value.
  31. *
  32. * @since 6.6
  33. */
  34. public abstract class Compare implements Filter {
  35. public enum Operation {
  36. EQUAL, GREATER, LESS, GREATER_OR_EQUAL, LESS_OR_EQUAL
  37. }
  38. private final Object propertyId;
  39. private final Operation operation;
  40. private final Object value;
  41. /**
  42. * A {@link Compare} filter that accepts items for which the identified
  43. * property value is equal to <code>value</code>.
  44. *
  45. * For in-memory filters, {@link Comparable#compareTo(Object)} or, if not
  46. * Comparable, {@link #equals(Object)} is used for the comparison. For other
  47. * containers, the comparison implementation is container dependent and may
  48. * use e.g. database comparison operations.
  49. *
  50. * @since 6.6
  51. */
  52. public static final class Equal extends Compare {
  53. /**
  54. * Construct a filter that accepts items for which the identified
  55. * property value is equal to <code>value</code>.
  56. *
  57. * For in-memory filters, equals() is used for the comparison. For other
  58. * containers, the comparison implementation is container dependent and
  59. * may use e.g. database comparison operations.
  60. *
  61. * @param propertyId
  62. * the identifier of the property whose value to compare
  63. * against value, not null
  64. * @param value
  65. * the value to compare against - null values may or may not
  66. * be supported depending on the container
  67. */
  68. public Equal(Object propertyId, Object value) {
  69. super(propertyId, value, Operation.EQUAL);
  70. }
  71. }
  72. /**
  73. * A {@link Compare} filter that accepts items for which the identified
  74. * property value is greater than <code>value</code>.
  75. *
  76. * For in-memory filters, the values must implement {@link Comparable} and
  77. * {@link Comparable#compareTo(Object)} is used for the comparison. For
  78. * other containers, the comparison implementation is container dependent
  79. * and may use e.g. database comparison operations.
  80. *
  81. * @since 6.6
  82. */
  83. public static final class Greater extends Compare {
  84. /**
  85. * Construct a filter that accepts items for which the identified
  86. * property value is greater than <code>value</code>.
  87. *
  88. * For in-memory filters, the values must implement {@link Comparable}
  89. * and {@link Comparable#compareTo(Object)} is used for the comparison.
  90. * For other containers, the comparison implementation is container
  91. * dependent and may use e.g. database comparison operations.
  92. *
  93. * @param propertyId
  94. * the identifier of the property whose value to compare
  95. * against value, not null
  96. * @param value
  97. * the value to compare against - null values may or may not
  98. * be supported depending on the container
  99. */
  100. public Greater(Object propertyId, Object value) {
  101. super(propertyId, value, Operation.GREATER);
  102. }
  103. }
  104. /**
  105. * A {@link Compare} filter that accepts items for which the identified
  106. * property value is less than <code>value</code>.
  107. *
  108. * For in-memory filters, the values must implement {@link Comparable} and
  109. * {@link Comparable#compareTo(Object)} is used for the comparison. For
  110. * other containers, the comparison implementation is container dependent
  111. * and may use e.g. database comparison operations.
  112. *
  113. * @since 6.6
  114. */
  115. public static final class Less extends Compare {
  116. /**
  117. * Construct a filter that accepts items for which the identified
  118. * property value is less than <code>value</code>.
  119. *
  120. * For in-memory filters, the values must implement {@link Comparable}
  121. * and {@link Comparable#compareTo(Object)} is used for the comparison.
  122. * For other containers, the comparison implementation is container
  123. * dependent and may use e.g. database comparison operations.
  124. *
  125. * @param propertyId
  126. * the identifier of the property whose value to compare
  127. * against value, not null
  128. * @param value
  129. * the value to compare against - null values may or may not
  130. * be supported depending on the container
  131. */
  132. public Less(Object propertyId, Object value) {
  133. super(propertyId, value, Operation.LESS);
  134. }
  135. }
  136. /**
  137. * A {@link Compare} filter that accepts items for which the identified
  138. * property value is greater than or equal to <code>value</code>.
  139. *
  140. * For in-memory filters, the values must implement {@link Comparable} and
  141. * {@link Comparable#compareTo(Object)} is used for the comparison. For
  142. * other containers, the comparison implementation is container dependent
  143. * and may use e.g. database comparison operations.
  144. *
  145. * @since 6.6
  146. */
  147. public static final class GreaterOrEqual extends Compare {
  148. /**
  149. * Construct a filter that accepts items for which the identified
  150. * property value is greater than or equal to <code>value</code>.
  151. *
  152. * For in-memory filters, the values must implement {@link Comparable}
  153. * and {@link Comparable#compareTo(Object)} is used for the comparison.
  154. * For other containers, the comparison implementation is container
  155. * dependent and may use e.g. database comparison operations.
  156. *
  157. * @param propertyId
  158. * the identifier of the property whose value to compare
  159. * against value, not null
  160. * @param value
  161. * the value to compare against - null values may or may not
  162. * be supported depending on the container
  163. */
  164. public GreaterOrEqual(Object propertyId, Object value) {
  165. super(propertyId, value, Operation.GREATER_OR_EQUAL);
  166. }
  167. }
  168. /**
  169. * A {@link Compare} filter that accepts items for which the identified
  170. * property value is less than or equal to <code>value</code>.
  171. *
  172. * For in-memory filters, the values must implement {@link Comparable} and
  173. * {@link Comparable#compareTo(Object)} is used for the comparison. For
  174. * other containers, the comparison implementation is container dependent
  175. * and may use e.g. database comparison operations.
  176. *
  177. * @since 6.6
  178. */
  179. public static final class LessOrEqual extends Compare {
  180. /**
  181. * Construct a filter that accepts items for which the identified
  182. * property value is less than or equal to <code>value</code>.
  183. *
  184. * For in-memory filters, the values must implement {@link Comparable}
  185. * and {@link Comparable#compareTo(Object)} is used for the comparison.
  186. * For other containers, the comparison implementation is container
  187. * dependent and may use e.g. database comparison operations.
  188. *
  189. * @param propertyId
  190. * the identifier of the property whose value to compare
  191. * against value, not null
  192. * @param value
  193. * the value to compare against - null values may or may not
  194. * be supported depending on the container
  195. */
  196. public LessOrEqual(Object propertyId, Object value) {
  197. super(propertyId, value, Operation.LESS_OR_EQUAL);
  198. }
  199. }
  200. /**
  201. * Constructor for a {@link Compare} filter that compares the value of an
  202. * item property with the given constant <code>value</code>.
  203. *
  204. * This constructor is intended to be used by the nested static classes only
  205. * ({@link Equal}, {@link Greater}, {@link Less}, {@link GreaterOrEqual},
  206. * {@link LessOrEqual}).
  207. *
  208. * For in-memory filtering, comparisons except EQUAL require that the values
  209. * implement {@link Comparable} and {@link Comparable#compareTo(Object)} is
  210. * used for the comparison. The equality comparison is performed using
  211. * {@link Object#equals(Object)}.
  212. *
  213. * For other containers, the comparison implementation is container
  214. * dependent and may use e.g. database comparison operations. Therefore, the
  215. * behavior of comparisons might differ in some cases between in-memory and
  216. * other containers.
  217. *
  218. * @param propertyId
  219. * the identifier of the property whose value to compare against
  220. * value, not null
  221. * @param value
  222. * the value to compare against - null values may or may not be
  223. * supported depending on the container
  224. * @param operation
  225. * the comparison {@link Operation} to use
  226. */
  227. Compare(Object propertyId, Object value, Operation operation) {
  228. this.propertyId = propertyId;
  229. this.value = value;
  230. this.operation = operation;
  231. }
  232. @Override
  233. public boolean passesFilter(Object itemId, Item item) {
  234. final Property<?> p = item.getItemProperty(getPropertyId());
  235. if (null == p) {
  236. return false;
  237. }
  238. Object value = p.getValue();
  239. switch (getOperation()) {
  240. case EQUAL:
  241. return compareEquals(value);
  242. case GREATER:
  243. return compareValue(value) > 0;
  244. case LESS:
  245. return compareValue(value) < 0;
  246. case GREATER_OR_EQUAL:
  247. return compareValue(value) >= 0;
  248. case LESS_OR_EQUAL:
  249. return compareValue(value) <= 0;
  250. }
  251. // all cases should have been processed above
  252. return false;
  253. }
  254. /**
  255. * Checks if the this value equals the given value. Favors Comparable over
  256. * equals to better support e.g. BigDecimal where equals is stricter than
  257. * compareTo.
  258. *
  259. * @param otherValue
  260. * The value to compare to
  261. * @return true if the values are equal, false otherwise
  262. */
  263. private boolean compareEquals(Object otherValue) {
  264. if (value == null || otherValue == null) {
  265. return (otherValue == value);
  266. } else if (value == otherValue) {
  267. return true;
  268. } else if (value instanceof Comparable
  269. && otherValue.getClass()
  270. .isAssignableFrom(getValue().getClass())) {
  271. return ((Comparable) value).compareTo(otherValue) == 0;
  272. } else {
  273. return value.equals(otherValue);
  274. }
  275. }
  276. @SuppressWarnings({ "unchecked", "rawtypes" })
  277. protected int compareValue(Object value1) {
  278. if (null == value) {
  279. return null == value1 ? 0 : -1;
  280. } else if (null == value1) {
  281. return 1;
  282. } else if (getValue() instanceof Comparable
  283. && value1.getClass().isAssignableFrom(getValue().getClass())) {
  284. return -((Comparable) getValue()).compareTo(value1);
  285. }
  286. throw new IllegalArgumentException("Could not compare the arguments: "
  287. + value1 + ", " + getValue());
  288. }
  289. @Override
  290. public boolean appliesToProperty(Object propertyId) {
  291. return getPropertyId().equals(propertyId);
  292. }
  293. @Override
  294. public boolean equals(Object obj) {
  295. if (obj == null) {
  296. return false;
  297. }
  298. // Only objects of the same class can be equal
  299. if (!getClass().equals(obj.getClass())) {
  300. return false;
  301. }
  302. final Compare o = (Compare) obj;
  303. // Checks the properties one by one
  304. if (getPropertyId() != o.getPropertyId() && null != o.getPropertyId()
  305. && !o.getPropertyId().equals(getPropertyId())) {
  306. return false;
  307. }
  308. if (getOperation() != o.getOperation()) {
  309. return false;
  310. }
  311. return (null == getValue()) ? null == o.getValue() : getValue().equals(
  312. o.getValue());
  313. }
  314. @Override
  315. public int hashCode() {
  316. return (null != getPropertyId() ? getPropertyId().hashCode() : 0)
  317. ^ (null != getValue() ? getValue().hashCode() : 0);
  318. }
  319. /**
  320. * Returns the property id of the property to compare against the fixed
  321. * value.
  322. *
  323. * @return property id (not null)
  324. */
  325. public Object getPropertyId() {
  326. return propertyId;
  327. }
  328. /**
  329. * Returns the comparison operation.
  330. *
  331. * @return {@link Operation}
  332. */
  333. public Operation getOperation() {
  334. return operation;
  335. }
  336. /**
  337. * Returns the value to compare the property against.
  338. *
  339. * @return comparison reference value
  340. */
  341. public Object getValue() {
  342. return value;
  343. }
  344. }