1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * -------------------------
  25. * KeyedValueComparator.java
  26. * -------------------------
  27. * (C) Copyright 2003-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: KeyedValueComparator.java,v 1.3 2005/01/12 14:52:22 mungady Exp $
  33. *
  34. * Changes:
  35. * --------
  36. * 05-Mar-2003 : Version 1 (DG);
  37. * 27-Aug-2003 : Moved SortOrder from org.jfree.data --> org.jfree.util (DG);
  38. * 12-Jan-2005 : Added accessor methods (DG);
  39. *
  40. */
  41. package org.jfree.data;
  42. import java.util.Comparator;
  43. import org.jfree.util.SortOrder;
  44. /**
  45. * A utility class that can compare and order two {@link KeyedValue} instances and sort them
  46. * into ascending or descending order by key or by value.
  47. */
  48. public class KeyedValueComparator implements Comparator {
  49. /** The comparator type. */
  50. private KeyedValueComparatorType type;
  51. /** The sort order. */
  52. private SortOrder order;
  53. /**
  54. * Creates a new comparator.
  55. *
  56. * @param type the type (<code>BY_KEY</code> or <code>BY_VALUE</code>,
  57. * <code>null</code> not permitted).
  58. * @param order the order (<code>null</code> not permitted).
  59. */
  60. public KeyedValueComparator(KeyedValueComparatorType type, SortOrder order) {
  61. if (order == null) {
  62. throw new IllegalArgumentException("Null 'order' argument.");
  63. }
  64. this.type = type;
  65. this.order = order;
  66. }
  67. /**
  68. * Returns the type.
  69. *
  70. * @return The type (never <code>null</code>).
  71. */
  72. public KeyedValueComparatorType getType() {
  73. return this.type;
  74. }
  75. /**
  76. * Returns the sort order.
  77. *
  78. * @return The sort order (never <code>null</code>).
  79. */
  80. public SortOrder getOrder() {
  81. return this.order;
  82. }
  83. /**
  84. * Compares two {@link KeyedValue} instances and returns an <code>int</code> that indicates the
  85. * relative order of the two objects.
  86. *
  87. * @param o1 object 1.
  88. * @param o2 object 2.
  89. *
  90. * @return An int indicating the relative order of the objects.
  91. */
  92. public int compare(Object o1, Object o2) {
  93. if (o2 == null) {
  94. return -1;
  95. }
  96. if (o1 == null) {
  97. return 1;
  98. }
  99. int result;
  100. KeyedValue kv1 = (KeyedValue) o1;
  101. KeyedValue kv2 = (KeyedValue) o2;
  102. if (this.type == KeyedValueComparatorType.BY_KEY) {
  103. if (this.order.equals(SortOrder.ASCENDING)) {
  104. result = kv1.getKey().compareTo(kv2.getKey());
  105. }
  106. else if (this.order.equals(SortOrder.DESCENDING)) {
  107. result = kv2.getKey().compareTo(kv1.getKey());
  108. }
  109. else {
  110. throw new IllegalArgumentException("Unrecognised sort order.");
  111. }
  112. }
  113. else if (this.type == KeyedValueComparatorType.BY_VALUE) {
  114. Number n1 = kv1.getValue();
  115. Number n2 = kv2.getValue();
  116. if (n2 == null) {
  117. return -1;
  118. }
  119. if (n1 == null) {
  120. return 1;
  121. }
  122. double d1 = n1.doubleValue();
  123. double d2 = n2.doubleValue();
  124. if (this.order.equals(SortOrder.ASCENDING)) {
  125. if (d1 > d2) {
  126. result = 1;
  127. }
  128. else if (d1 < d2) {
  129. result = -1;
  130. }
  131. else {
  132. result = 0;
  133. }
  134. }
  135. else if (this.order.equals(SortOrder.DESCENDING)) {
  136. if (d1 > d2) {
  137. result = -1;
  138. }
  139. else if (d1 < d2) {
  140. result = 1;
  141. }
  142. else {
  143. result = 0;
  144. }
  145. }
  146. else {
  147. throw new IllegalArgumentException("Unrecognised sort order.");
  148. }
  149. }
  150. else {
  151. throw new IllegalArgumentException("Unrecognised type.");
  152. }
  153. return result;
  154. }
  155. }