Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

DefaultItemSorter.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import java.io.Serializable;
  6. import java.util.ArrayList;
  7. import java.util.Collection;
  8. import java.util.Comparator;
  9. import java.util.List;
  10. import com.vaadin.data.Container;
  11. import com.vaadin.data.Container.Sortable;
  12. import com.vaadin.data.Item;
  13. import com.vaadin.data.Property;
  14. /**
  15. * Provides a default implementation of an ItemSorter. The
  16. * <code>DefaultItemSorter</code> adheres to the
  17. * {@link Sortable#sort(Object[], boolean[])} rules and sorts the container
  18. * according to the properties given using
  19. * {@link #setSortProperties(Sortable, Object[], boolean[])}.
  20. * <p>
  21. * A Comparator is used for comparing the individual <code>Property</code>
  22. * values. The comparator can be set using the constructor. If no comparator is
  23. * provided a default comparator is used.
  24. *
  25. */
  26. public class DefaultItemSorter implements ItemSorter {
  27. private java.lang.Object[] sortPropertyIds;
  28. private boolean[] sortDirections;
  29. private Container container;
  30. private Comparator<Object> propertyValueComparator;
  31. /**
  32. * Constructs a DefaultItemSorter using the default <code>Comparator</code>
  33. * for comparing <code>Property</code>values.
  34. *
  35. */
  36. public DefaultItemSorter() {
  37. this(new DefaultPropertyValueComparator());
  38. }
  39. /**
  40. * Constructs a DefaultItemSorter which uses the <code>Comparator</code>
  41. * indicated by the <code>propertyValueComparator</code> parameter for
  42. * comparing <code>Property</code>values.
  43. *
  44. * @param propertyValueComparator
  45. * The comparator to use when comparing individual
  46. * <code>Property</code> values
  47. */
  48. public DefaultItemSorter(Comparator<Object> propertyValueComparator) {
  49. this.propertyValueComparator = propertyValueComparator;
  50. }
  51. /*
  52. * (non-Javadoc)
  53. *
  54. * @see com.vaadin.data.util.ItemSorter#compare(java.lang.Object,
  55. * java.lang.Object)
  56. */
  57. @Override
  58. public int compare(Object o1, Object o2) {
  59. Item item1 = container.getItem(o1);
  60. Item item2 = container.getItem(o2);
  61. /*
  62. * Items can be null if the container is filtered. Null is considered
  63. * "less" than not-null.
  64. */
  65. if (item1 == null) {
  66. if (item2 == null) {
  67. return 0;
  68. } else {
  69. return 1;
  70. }
  71. } else if (item2 == null) {
  72. return -1;
  73. }
  74. for (int i = 0; i < sortPropertyIds.length; i++) {
  75. int result = compareProperty(sortPropertyIds[i], sortDirections[i],
  76. item1, item2);
  77. // If order can be decided
  78. if (result != 0) {
  79. return result;
  80. }
  81. }
  82. return 0;
  83. }
  84. /**
  85. * Compares the property indicated by <code>propertyId</code> in the items
  86. * indicated by <code>item1</code> and <code>item2</code> for order. Returns
  87. * a negative integer, zero, or a positive integer as the property value in
  88. * the first item is less than, equal to, or greater than the property value
  89. * in the second item. If the <code>sortDirection</code> is false the
  90. * returned value is negated.
  91. * <p>
  92. * The comparator set for this <code>DefaultItemSorter</code> is used for
  93. * comparing the two property values.
  94. *
  95. * @param propertyId
  96. * The property id for the property that is used for comparison.
  97. * @param sortDirection
  98. * The direction of the sort. A false value negates the result.
  99. * @param item1
  100. * The first item to compare.
  101. * @param item2
  102. * The second item to compare.
  103. * @return a negative, zero, or positive integer if the property value in
  104. * the first item is less than, equal to, or greater than the
  105. * property value in the second item. Negated if
  106. * {@code sortDirection} is false.
  107. */
  108. protected int compareProperty(Object propertyId, boolean sortDirection,
  109. Item item1, Item item2) {
  110. // Get the properties to compare
  111. final Property<?> property1 = item1.getItemProperty(propertyId);
  112. final Property<?> property2 = item2.getItemProperty(propertyId);
  113. // Get the values to compare
  114. final Object value1 = (property1 == null) ? null : property1.getValue();
  115. final Object value2 = (property2 == null) ? null : property2.getValue();
  116. // Result of the comparison
  117. int r = 0;
  118. if (sortDirection) {
  119. r = propertyValueComparator.compare(value1, value2);
  120. } else {
  121. r = propertyValueComparator.compare(value2, value1);
  122. }
  123. return r;
  124. }
  125. /*
  126. * (non-Javadoc)
  127. *
  128. * @see
  129. * com.vaadin.data.util.ItemSorter#setSortProperties(com.vaadin.data.Container
  130. * .Sortable, java.lang.Object[], boolean[])
  131. */
  132. @Override
  133. public void setSortProperties(Container.Sortable container,
  134. Object[] propertyId, boolean[] ascending) {
  135. this.container = container;
  136. // Removes any non-sortable property ids
  137. final List<Object> ids = new ArrayList<Object>();
  138. final List<Boolean> orders = new ArrayList<Boolean>();
  139. final Collection<?> sortable = container
  140. .getSortableContainerPropertyIds();
  141. for (int i = 0; i < propertyId.length; i++) {
  142. if (sortable.contains(propertyId[i])) {
  143. ids.add(propertyId[i]);
  144. orders.add(Boolean.valueOf(i < ascending.length ? ascending[i]
  145. : true));
  146. }
  147. }
  148. sortPropertyIds = ids.toArray();
  149. sortDirections = new boolean[orders.size()];
  150. for (int i = 0; i < sortDirections.length; i++) {
  151. sortDirections[i] = (orders.get(i)).booleanValue();
  152. }
  153. }
  154. /**
  155. * Provides a default comparator used for comparing {@link Property} values.
  156. * The <code>DefaultPropertyValueComparator</code> assumes all objects it
  157. * compares can be cast to Comparable.
  158. *
  159. */
  160. public static class DefaultPropertyValueComparator implements
  161. Comparator<Object>, Serializable {
  162. @Override
  163. @SuppressWarnings("unchecked")
  164. public int compare(Object o1, Object o2) {
  165. int r = 0;
  166. // Normal non-null comparison
  167. if (o1 != null && o2 != null) {
  168. // Assume the objects can be cast to Comparable, throw
  169. // ClassCastException otherwise.
  170. r = ((Comparable<Object>) o1).compareTo(o2);
  171. } else if (o1 == o2) {
  172. // Objects are equal if both are null
  173. r = 0;
  174. } else {
  175. if (o1 == null) {
  176. r = -1; // null is less than non-null
  177. } else {
  178. r = 1; // non-null is greater than null
  179. }
  180. }
  181. return r;
  182. }
  183. }
  184. }