Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Compare.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. @Override
  220. public boolean passesFilter(Object itemId, Item item) {
  221. final Property<?> p = item.getItemProperty(getPropertyId());
  222. if (null == p) {
  223. return false;
  224. }
  225. Object value = p.getValue();
  226. switch (getOperation()) {
  227. case EQUAL:
  228. return (null == this.value) ? (null == value) : this.value
  229. .equals(value);
  230. case GREATER:
  231. return compareValue(value) > 0;
  232. case LESS:
  233. return compareValue(value) < 0;
  234. case GREATER_OR_EQUAL:
  235. return compareValue(value) >= 0;
  236. case LESS_OR_EQUAL:
  237. return compareValue(value) <= 0;
  238. }
  239. // all cases should have been processed above
  240. return false;
  241. }
  242. @SuppressWarnings({ "unchecked", "rawtypes" })
  243. protected int compareValue(Object value1) {
  244. if (null == value) {
  245. return null == value1 ? 0 : -1;
  246. } else if (null == value1) {
  247. return 1;
  248. } else if (getValue() instanceof Comparable
  249. && value1.getClass().isAssignableFrom(getValue().getClass())) {
  250. return -((Comparable) getValue()).compareTo(value1);
  251. }
  252. throw new IllegalArgumentException("Could not compare the arguments: "
  253. + value1 + ", " + getValue());
  254. }
  255. @Override
  256. public boolean appliesToProperty(Object propertyId) {
  257. return getPropertyId().equals(propertyId);
  258. }
  259. @Override
  260. public boolean equals(Object obj) {
  261. // Only objects of the same class can be equal
  262. if (!getClass().equals(obj.getClass())) {
  263. return false;
  264. }
  265. final Compare o = (Compare) obj;
  266. // Checks the properties one by one
  267. if (getPropertyId() != o.getPropertyId() && null != o.getPropertyId()
  268. && !o.getPropertyId().equals(getPropertyId())) {
  269. return false;
  270. }
  271. if (getOperation() != o.getOperation()) {
  272. return false;
  273. }
  274. return (null == getValue()) ? null == o.getValue() : getValue().equals(
  275. o.getValue());
  276. }
  277. @Override
  278. public int hashCode() {
  279. return (null != getPropertyId() ? getPropertyId().hashCode() : 0)
  280. ^ (null != getValue() ? getValue().hashCode() : 0);
  281. }
  282. /**
  283. * Returns the property id of the property to compare against the fixed
  284. * value.
  285. *
  286. * @return property id (not null)
  287. */
  288. public Object getPropertyId() {
  289. return propertyId;
  290. }
  291. /**
  292. * Returns the comparison operation.
  293. *
  294. * @return {@link Operation}
  295. */
  296. public Operation getOperation() {
  297. return operation;
  298. }
  299. /**
  300. * Returns the value to compare the property against.
  301. *
  302. * @return comparison reference value
  303. */
  304. public Object getValue() {
  305. return value;
  306. }
  307. }