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 12KB

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