BasicSorter.java
/*
** Module : BasicSorter.java
** Abstract : Directional comparator for Comparables
**
** Copyright (c) 2004-2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- -T- --JPRM-- ----------------Description-----------------
** 001 ECF 20080708 ADD @39078 Created initial version. Abstracted as a base
** class from DataSorter. Needed to handle non-
** BaseDataType Comparables.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.persist;
import java.io.*;
import java.util.*;
/**
* An implementation of <code>Comparator</code>, used to sort instances of
* <code>Comparable</code>s in ascending or descending order. Direction of
* the sort is specified upon construction.
*
* @param <T>
* A type which extends/implements Comparable
*/
public class BasicSorter<T extends Comparable>
implements Comparator<T>,
Serializable
{
/** Multiplier used to adjust (possibly invert) sorting */
private final int direction;
/**
* Constructor which stores the sort direction.
*
* @param ascending
* <code>true</code> if sort should be ascending;
* <code>false</code> if it should be descending.
*/
protected BasicSorter(boolean ascending)
{
this.direction = (ascending ? 1 : -1);
}
/**
* Compare two <code>Comparable</code> instances to determine relative
* sort order. The comparison uses the natural order of the objects,
* except that unknown value always sorts high in an ascending sort. The
* result is inverted for descending sorts (meaning unknown value sorts low
* in a descending sort.
*
* @param d1
* First value to compare. Must not be <code>null</code>.
* @param d2
* Second value to compare. Must not be <code>null</code>.
*
* @return Positive value if <code>d1</code> > <code>d2</code>;
* negative value if <code>d1</code> < <code>d2</code>;
* <code>0</code> if <code>d1</code> and <code>d2</code> sort
* equivalently.
*/
public int compare(T d1, T d2)
{
int result = d1.compareTo(d2);
return (result * direction);
}
/**
* Calculate a hash code for this object.
*
* @return Hash code.
*/
public int hashCode()
{
int result = 17;
result = 37 * result + direction;
return result;
}
/**
* Test for equality between this comparator and another. Equality means
* either both comparators represent the same object instance in memory,
* or that both comparators are instances of this class and sort in the
* same direction.
*
* @return <code>true</code> if objects are equal, else
* <code>false</code>.
*/
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (!(o instanceof BasicSorter))
{
return false;
}
BasicSorter<T> that = (BasicSorter<T>) o;
return (this.direction == that.direction);
}
/**
* Report the direction in which this sorter sorts its data.
*
* @return 1 if ascending or -1 if descending.
*/
protected int getDirection()
{
return direction;
}
}