All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.v7.data.util.DefaultItemSorter Maven / Gradle / Ivy

There is a newer version: 8.27.5
Show newest version
/*
 * Copyright (C) 2000-2024 Vaadin Ltd
 *
 * This program is available under Vaadin Commercial License and Service Terms.
 *
 * See  for the full
 * license.
 */
package com.vaadin.v7.data.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

import com.vaadin.data.provider.AbstractBackEndDataProvider;
import com.vaadin.data.provider.DataProvider;
import com.vaadin.data.provider.ListDataProvider;
import com.vaadin.data.provider.Query;
import com.vaadin.server.SerializableComparator;
import com.vaadin.v7.data.Container;
import com.vaadin.v7.data.Container.Sortable;
import com.vaadin.v7.data.Item;
import com.vaadin.v7.data.Property;

/**
 * Provides a default implementation of an ItemSorter. The
 * DefaultItemSorter adheres to the
 * {@link Sortable#sort(Object[], boolean[])} rules and sorts the container
 * according to the properties given using
 * {@link #setSortProperties(Sortable, Object[], boolean[])}.
 * 

* A Comparator is used for comparing the individual Property * values. The comparator can be set using the constructor. If no comparator is * provided a default comparator is used. * * * @deprecated As of 8.0, sorting is integrated into {@link DataProvider} and {@link Query#getSortOrders()}. * For in-memory case, you can use also {@link ListDataProvider#setSortComparator(SerializableComparator)}. * For back-end DataProviders, see {@link AbstractBackEndDataProvider#setSortOrders(List)}. */ @Deprecated public class DefaultItemSorter implements ItemSorter { private Object[] sortPropertyIds; private boolean[] sortDirections; private Container container; private Comparator propertyValueComparator; /** * Constructs a DefaultItemSorter using the default Comparator * for comparing Propertyvalues. * */ public DefaultItemSorter() { this(new DefaultPropertyValueComparator()); } /** * Constructs a DefaultItemSorter which uses the Comparator * indicated by the propertyValueComparator parameter for * comparing Propertyvalues. * * @param propertyValueComparator * The comparator to use when comparing individual * Property values */ public DefaultItemSorter(Comparator propertyValueComparator) { this.propertyValueComparator = propertyValueComparator; } /* * (non-Javadoc) * * @see com.vaadin.data.util.ItemSorter#compare(java.lang.Object, * java.lang.Object) */ @Override public int compare(Object o1, Object o2) { Item item1 = container.getItem(o1); Item item2 = container.getItem(o2); /* * Items can be null if the container is filtered. Null is considered * "less" than not-null. */ if (item1 == null) { if (item2 == null) { return 0; } else { return 1; } } else if (item2 == null) { return -1; } for (int i = 0; i < sortPropertyIds.length; i++) { int result = compareProperty(sortPropertyIds[i], sortDirections[i], item1, item2); // If order can be decided if (result != 0) { return result; } } return 0; } /** * Compares the property indicated by propertyId in the items * indicated by item1 and item2 for order. Returns * a negative integer, zero, or a positive integer as the property value in * the first item is less than, equal to, or greater than the property value * in the second item. If the sortDirection is false the * returned value is negated. *

* The comparator set for this DefaultItemSorter is used for * comparing the two property values. * * @param propertyId * The property id for the property that is used for comparison. * @param sortDirection * The direction of the sort. A false value negates the result. * @param item1 * The first item to compare. * @param item2 * The second item to compare. * @return a negative, zero, or positive integer if the property value in * the first item is less than, equal to, or greater than the * property value in the second item. Negated if * {@code sortDirection} is false. */ protected int compareProperty(Object propertyId, boolean sortDirection, Item item1, Item item2) { // Get the properties to compare final Property property1 = item1.getItemProperty(propertyId); final Property property2 = item2.getItemProperty(propertyId); // Get the values to compare final Object value1 = (property1 == null) ? null : property1.getValue(); final Object value2 = (property2 == null) ? null : property2.getValue(); // Result of the comparison int r = 0; if (sortDirection) { r = propertyValueComparator.compare(value1, value2); } else { r = propertyValueComparator.compare(value2, value1); } return r; } /* * (non-Javadoc) * * @see com.vaadin.data.util.ItemSorter#setSortProperties(com.vaadin.data. * Container .Sortable, java.lang.Object[], boolean[]) */ @Override public void setSortProperties(Container.Sortable container, Object[] propertyId, boolean[] ascending) { this.container = container; // Removes any non-sortable property ids final List ids = new ArrayList(); final List orders = new ArrayList(); final Collection sortable = container .getSortableContainerPropertyIds(); for (int i = 0; i < propertyId.length; i++) { if (sortable.contains(propertyId[i])) { ids.add(propertyId[i]); orders.add(Boolean .valueOf(i < ascending.length ? ascending[i] : true)); } } sortPropertyIds = ids.toArray(); sortDirections = new boolean[orders.size()]; for (int i = 0; i < sortDirections.length; i++) { sortDirections[i] = (orders.get(i)).booleanValue(); } } /** * Provides a default comparator used for comparing {@link Property} values. * The DefaultPropertyValueComparator assumes all objects it * compares can be cast to Comparable. * */ @Deprecated public static class DefaultPropertyValueComparator implements Comparator, Serializable { @Override @SuppressWarnings("unchecked") public int compare(Object o1, Object o2) { int r = 0; // Normal non-null comparison if (o1 != null && o2 != null) { // Assume the objects can be cast to Comparable, throw // ClassCastException otherwise. r = ((Comparable) o1).compareTo(o2); } else if (o1 == o2) { // Objects are equal if both are null r = 0; } else { if (o1 == null) { r = -1; // null is less than non-null } else { r = 1; // non-null is greater than null } } return r; } } }