選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DefaultItemSorter.java 7.5KB

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