001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.util.comparator;
018:
019: import java.io.Serializable;
020: import java.util.Comparator;
021:
022: /**
023: * A decorator for a comparator, with an "ascending" flag denoting
024: * whether comparison results should be treated in forward (standard
025: * ascending) order or flipped for reverse (descending) order.
026: *
027: * @author Keith Donald
028: * @author Juergen Hoeller
029: * @since 1.2.2
030: */
031: public class InvertibleComparator implements Comparator, Serializable {
032:
033: private final Comparator comparator;
034:
035: private boolean ascending = true;
036:
037: /**
038: * Create an InvertibleComparator that sorts ascending by default.
039: * For the actual comparison, the specified Comparator will be used.
040: * @param comparator the comparator to decorate
041: */
042: public InvertibleComparator(Comparator comparator) {
043: this .comparator = comparator;
044: }
045:
046: /**
047: * Create an InvertibleComparator that sorts based on the provided order.
048: * For the actual comparison, the specified Comparator will be used.
049: * @param comparator the comparator to decorate
050: * @param ascending the sort order: ascending (true) or descending (false)
051: */
052: public InvertibleComparator(Comparator comparator, boolean ascending) {
053: this .comparator = comparator;
054: setAscending(ascending);
055: }
056:
057: /**
058: * Specify the sort order: ascending (true) or descending (false).
059: */
060: public void setAscending(boolean ascending) {
061: this .ascending = ascending;
062: }
063:
064: /**
065: * Return the sort order: ascending (true) or descending (false).
066: */
067: public boolean isAscending() {
068: return ascending;
069: }
070:
071: /**
072: * Invert the sort order: ascending -> descending or
073: * descending -> ascending.
074: */
075: public void invertOrder() {
076: this .ascending = !this .ascending;
077: }
078:
079: public int compare(Object o1, Object o2) {
080: int result = this .comparator.compare(o1, o2);
081: if (result != 0) {
082: // Invert the order if it is a reverse sort.
083: if (!this .ascending) {
084: if (Integer.MIN_VALUE == result) {
085: result = Integer.MAX_VALUE;
086: } else {
087: result *= -1;
088: }
089: }
090: return result;
091: }
092: return 0;
093: }
094:
095: public boolean equals(Object obj) {
096: if (this == obj) {
097: return true;
098: }
099: if (!(obj instanceof InvertibleComparator)) {
100: return false;
101: }
102: InvertibleComparator other = (InvertibleComparator) obj;
103: return (this .comparator.equals(other.comparator) && this .ascending == other.ascending);
104: }
105:
106: public int hashCode() {
107: return this .comparator.hashCode();
108: }
109:
110: public String toString() {
111: return "InvertibleComparator: [" + this .comparator
112: + "]; ascending=" + this.ascending;
113: }
114:
115: }
|