001: package org.andromda.utils.beans;
002:
003: import java.util.Collection;
004:
005: import org.andromda.core.common.ClassUtils;
006: import org.andromda.core.common.ExceptionUtils;
007:
008: /**
009: * Used to contain sort criteria.
010: *
011: * @author Chad Brandon
012: */
013: public class SortCriteria {
014: /**
015: * Creates a SortCriteria object with the default ascending ordering
016: * @param sortBy
017: */
018: public SortCriteria(final String sortBy) {
019: this (sortBy, Ordering.ASCENDING);
020: }
021:
022: /**
023: * Creates a new instance of this SortCriteria class.
024: *
025: *
026: * @param sortBy the property to sort by, this can
027: * be nested.
028: * @param ordering the ordering to sort by, "ASCENDING", "DESCENDING".
029: */
030: public SortCriteria(final String sortBy, final Ordering ordering) {
031: ExceptionUtils.checkEmpty("sortBy", sortBy);
032: ExceptionUtils.checkNull("ordering", ordering);
033: try {
034: final Collection validOrderings = ClassUtils
035: .getStaticFieldValues(String.class,
036: SortCriteria.class);
037: if (validOrderings.contains(ordering)) {
038: throw new IllegalArgumentException(
039: "ordering must be of one of the following types: "
040: + validOrderings);
041: }
042: } catch (final Throwable throwable) {
043: throw new SortException(throwable);
044: }
045: this .sortBy = sortBy;
046: this .ordering = ordering;
047: }
048:
049: /**
050: * The ordering by which sorting shall occur.
051: */
052: private Ordering ordering;
053:
054: /**
055: * Gets the current ordering to be used.
056: *
057: * @return Ordering
058: */
059: public Ordering getOrdering() {
060: return ordering;
061: }
062:
063: /**
064: * Sets the ordering to use for sorting.
065: *
066: * @param ordering the ordering.
067: */
068: public void setOrdering(final Ordering ordering) {
069: this .ordering = ordering;
070: }
071:
072: /**
073: * Stores the name of the property to sort by.
074: */
075: private String sortBy;
076:
077: /**
078: * Gets the sort by name.
079: *
080: * @return String
081: */
082: public String getSortBy() {
083: return sortBy;
084: }
085:
086: /**
087: * Sets the name of the property by which to sort.
088: *
089: * @param sortBy the name of the property by which to sort.
090: */
091: public void setSortBy(final String sortBy) {
092: this .sortBy = sortBy;
093: }
094:
095: /**
096: * Represents the types of ordering that may occur when sorting
097: * with the {@link BeanSorter}.
098: *
099: * @author Chad Brandon
100: */
101: public static final class Ordering {
102: /**
103: * Indicates sorting should be performed <em>ascending</em>.
104: */
105: public static final Ordering ASCENDING = new Ordering(
106: "ASCENDING");
107:
108: /**
109: * Indicates sorting should be performed <em>descending</em>.
110: */
111: public static final Ordering DESCENDING = new Ordering(
112: "DESCENDING");
113:
114: /**
115: * The actual value of the enumeration.
116: */
117: private String value;
118:
119: private Ordering(final String ordering) {
120: this .value = ordering;
121: }
122:
123: /**
124: * @see java.lang.Object#toString()
125: */
126: public String toString() {
127: return this.value;
128: }
129: }
130: }
|