001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2024 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2009-2024 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.util;
037
038
039
040import java.io.Serializable;
041import java.util.Comparator;
042
043
044
045/**
046 * This class provides an implementation of a {@code Comparator} object that may
047 * be used to iterate through values in what would normally be considered
048 * reverse order.
049 *
050 * @param  <T>  The type of object to use with this comparator.
051 */
052@NotMutable()
053@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
054public final class ReverseComparator<T>
055       implements Comparator<T>, Serializable
056{
057  /**
058   * The serial version UID for this serializable class.
059   */
060  private static final long serialVersionUID = -4615537960027681276L;
061
062
063
064  // The comparator that will be used to make the underlying determination.
065  @Nullable private final Comparator<T> baseComparator;
066
067
068
069  /**
070   * Creates a new comparator that will sort items in reverse order.  The
071   * generic type for this class must implement the {@link Comparable}
072   * interface.
073   */
074  public ReverseComparator()
075  {
076    baseComparator = null;
077  }
078
079
080
081  /**
082   * Creates a new comparator that will sort items in the reverse order that
083   * they would be normally sorted using the given comparator.
084   *
085   * @param  baseComparator  The base comparator that will be used to make the
086   *                         determination.
087   */
088  public ReverseComparator(@NotNull final Comparator<T> baseComparator)
089  {
090    this.baseComparator = baseComparator;
091  }
092
093
094
095  /**
096   * Compares the provided objects to determine their relative order in a
097   * sorted list.
098   *
099   * @param  o1  The first object to compare.
100   * @param  o2  The second object to compare.
101   *
102   * @return  A negative integer if the first object should be ordered before
103   *          the second, a positive integer if the first object should be
104   *          ordered after the second, or zero if there is no difference in
105   *          their relative orders.
106   */
107  @SuppressWarnings("unchecked")
108  @Override()
109  public int compare(@NotNull final T o1, @NotNull final T o2)
110  {
111    final int baseValue;
112    if (baseComparator == null)
113    {
114      baseValue = ((Comparable<? super T>) o1).compareTo(o2);
115    }
116    else
117    {
118      baseValue = baseComparator.compare(o1, o2);
119    }
120
121    if (baseValue < 0)
122    {
123      return 1;
124    }
125    else if (baseValue > 0)
126    {
127      return -1;
128    }
129    else
130    {
131      return 0;
132    }
133  }
134
135
136
137  /**
138   * Retrieves a hash code for this class.
139   *
140   * @return  A hash code for this class.
141   */
142  @Override()
143  public int hashCode()
144  {
145    if (baseComparator == null)
146    {
147      return 0;
148    }
149    else
150    {
151      return baseComparator.hashCode();
152    }
153  }
154
155
156
157  /**
158   * Indicates whether the provided object may be considered equal to this
159   * comparator.
160   *
161   * @param  o  The object for which to make the determination.
162   *
163   * @return  {@code true} if the provided object may be considered equal to
164   *          this comparator, or {@code false} if not.
165   */
166  @Override()
167  @SuppressWarnings("unchecked")
168  public boolean equals(@Nullable final Object o)
169  {
170    if (o == null)
171    {
172      return false;
173    }
174
175    if (o == this)
176    {
177      return true;
178    }
179
180    if (! (o.getClass().equals(ReverseComparator.class)))
181    {
182      return false;
183    }
184
185    final ReverseComparator<T> c = (ReverseComparator<T>) o;
186    if (baseComparator == null)
187    {
188      return (c.baseComparator == null);
189    }
190    else
191    {
192      return baseComparator.equals(c.baseComparator);
193    }
194  }
195}