DataSorter.java

/*
** Module   : DataSorter.java
** Abstract : Directional comparator for BaseDataType
**
** Copyright (c) 2004-2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- -T- --JPRM-- ----------------Description-----------------
** 001 ECF 20080510 ADD  @38477  Created initial version. A Comparator
**                               implementation for BaseDataType with an
**                               adjustable direction.
** 002 ECF 20080708 CHG  @39079  Abstracted to extend BasicSorter. The base
**                               class handles Comparables in general. This
**                               class overrides compare() to handle
**                               BaseDataTypes specifically.
** 003 EVL 20160223              Javadoc fixes to make compatible with Oracle Java 8 for
**                               Solaris 10.
*/
/*
** 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 com.goldencode.p2j.util.*;

/**
 * An specialization of {@link BasicSorter}, used to sort instances of
 * <code>BaseDataType</code>.  Direction of the sort is specified upon
 * construction.  Unknown value sorts high in an ascending sort.
 * <p>
 * Instances of this class should be retrieved using the static method {@link
 * #getInstance(boolean)}.
 */
final class DataSorter
extends BasicSorter<BaseDataType>
{
   /** Shared instance of an ascending <code>DataSorter</code> */
   private static final DataSorter ASCENDER = new DataSorter(true);
 
   /** Shared instance of an descending <code>DataSorter</code> */
   private static final DataSorter DESCENDER = new DataSorter(false);
   
   /**
    * @param ascending
    */
   public DataSorter(boolean ascending)
   {
      super(ascending);
   }

   /**
    * Helper method to retrieve an instance of this class which sorts in the
    * specified direction.
    *
    * @param   ascending
    *          <code>true</code> if sort should be ascending;
    *          <code>false</code> if it should be descending.
    * 
    * @return  An instance of this class which sorts in the specified
    *          direction.
    */
   public static DataSorter getInstance(boolean ascending)
   {
      return (ascending ? ASCENDER : DESCENDER);
   }
   
   /**
    * Compare two <code>BaseDataType</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.  May be unknown, but must not be
    *          <code>null</code>.
    * @param   d2
    *          Second value to compare.  May be unknown, but must not be
    *          <code>null</code>.
    * 
    * @return  Positive value if <code>d1</code> &gt; <code>d2</code>;
    *          negative value if <code>d1</code> &lt; <code>d2</code>;
    *          <code>0</code> if <code>d1</code> and <code>d2</code> sort
    *          equivalently.
    */
   public int compare(BaseDataType d1, BaseDataType d2)
   {
      int result = 0;
      
      if (d1.isUnknown())
      {
         result = (d2.isUnknown() ? 0 : 1);
      }
      else if (d2.isUnknown())
      {
         result = (d1.isUnknown() ? 0 : -1);
      }
      else
      {
         result = d1.compareTo(d2);
      }
      
      return (result * getDirection());
   }
}