001package com.hfg.bio.seq.genomic.assembly;
002
003import java.io.Serializable;
004import java.util.Set;
005
006import com.hfg.util.CompareUtil;
007import com.hfg.util.collection.OrderedSet;
008
009//------------------------------------------------------------------------------
010/**
011 * Enumeration of genomic representation values.
012 * <div>
013 * @author J. Alex Taylor, hairyfatguy.com
014 * </div>
015 */
016//------------------------------------------------------------------------------
017// com.hfg XML/HTML Coding Library
018//
019// This library is free software; you can redistribute it and/or
020// modify it under the terms of the GNU Lesser General Public
021// License as published by the Free Software Foundation; either
022// version 2.1 of the License, or (at your option) any later version.
023//
024// This library is distributed in the hope that it will be useful,
025// but WITHOUT ANY WARRANTY; without even the implied warranty of
026// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
027// Lesser General Public License for more details.
028//
029// You should have received a copy of the GNU Lesser General Public
030// License along with this library; if not, write to the Free Software
031// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
032//
033// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
034// jataylor@hairyfatguy.com
035//------------------------------------------------------------------------------
036
037public final class GenomicRepresentation implements Serializable, Comparable<GenomicRepresentation>
038{
039
040   private final String mName;
041   private final int    mIndex;
042
043   private static final Set<GenomicRepresentation> sValues = new OrderedSet<>(2);
044
045   public static final GenomicRepresentation FULL    = new GenomicRepresentation("Full");
046   public static final GenomicRepresentation PARTIAL = new GenomicRepresentation("Partial");
047
048   //###########################################################################
049   // CONSTRUCTORS
050   //###########################################################################
051
052   //--------------------------------------------------------------------------
053   private GenomicRepresentation(String inName)
054   {
055      mName = inName;
056      mIndex = sValues.size();
057      sValues.add(this);
058   }
059
060   //###########################################################################
061   // PUBLIC METHODS
062   //###########################################################################
063
064   //--------------------------------------------------------------------------
065   @Override
066   public final String toString()
067   {
068      return mName;
069   }
070
071   //--------------------------------------------------------------------------
072   public static GenomicRepresentation valueOf(String inValue)
073   {
074      GenomicRepresentation outValue = null;
075
076      for (GenomicRepresentation value : sValues)
077      {
078         if (value.toString().equalsIgnoreCase(inValue))
079         {
080            outValue = value;
081            break;
082         }
083      }
084
085      return outValue;
086   }
087
088   //--------------------------------------------------------------------------
089   public static GenomicRepresentation[] values()
090   {
091      return sValues.toArray(new GenomicRepresentation[] {});
092   }
093
094
095   //--------------------------------------------------------------------------
096   public final String name()
097   {
098      return mName;
099   }
100
101   //--------------------------------------------------------------------------
102   @Override
103   public final int hashCode()
104   {
105      return mIndex;
106   }
107
108   //--------------------------------------------------------------------------
109   @Override
110   public final boolean equals(Object inObj)
111   {
112      return (inObj instanceof GenomicRepresentation
113              && mIndex == ((GenomicRepresentation) inObj).mIndex);
114   }
115
116   //--------------------------------------------------------------------------
117   /**
118    * This method is called after de-serialization, allowing the object
119    * to nominate a replacement object to be used in the output
120    * graph instead of this object. We don't want multiple objects of each type
121    * to exist in a target VM, so instances replace themselves with
122    * local objects.
123    */
124   private Object readResolve()
125   {
126      GenomicRepresentation outValue = null;
127
128      for (GenomicRepresentation value : sValues)
129      {
130         if (mIndex == value.mIndex)
131         {
132            outValue = value;
133            break;
134         }
135      }
136
137      return outValue;
138   }
139
140   //--------------------------------------------------------------------------
141   /**
142    * Compares this object with the specified object for order.  Returns a
143    * negative integer, zero, or a positive integer as this object is less
144    * than, equal to, or greater than the specified object.
145    *
146    * <p>The implementor must ensure
147    * {@code sgn(x.compareTo(y)) == -sgn(y.compareTo(x))}
148    * for all {@code x} and {@code y}.  (This
149    * implies that {@code x.compareTo(y)} must throw an exception iff
150    * {@code y.compareTo(x)} throws an exception.)
151    *
152    * <p>The implementor must also ensure that the relation is transitive:
153    * {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies
154    * {@code x.compareTo(z) > 0}.
155    *
156    * <p>Finally, the implementor must ensure that {@code x.compareTo(y)==0}
157    * implies that {@code sgn(x.compareTo(z)) == sgn(y.compareTo(z))}, for
158    * all {@code z}.
159    *
160    * <p>It is strongly recommended, but <i>not</i> strictly required that
161    * {@code (x.compareTo(y)==0) == (x.equals(y))}.  Generally speaking, any
162    * class that implements the {@code Comparable} interface and violates
163    * this condition should clearly indicate this fact.  The recommended
164    * language is "Note: this class has a natural ordering that is
165    * inconsistent with equals."
166    *
167    * <p>In the foregoing description, the notation
168    * {@code sgn(}<i>expression</i>{@code )} designates the mathematical
169    * <i>signum</i> function, which is defined to return one of {@code -1},
170    * {@code 0}, or {@code 1} according to whether the value of
171    * <i>expression</i> is negative, zero, or positive, respectively.
172    *
173    * @param inObj2 the object to be compared.
174    * @return a negative integer, zero, or a positive integer as this object
175    * is less than, equal to, or greater than the specified object.
176    * @throws ClassCastException   if the specified object's type prevents it
177    *                              from being compared to this object.
178    */
179   @Override
180   public int compareTo(GenomicRepresentation inObj2)
181   {
182      int result = -1;
183      if (inObj2 != null)
184      {
185         result = CompareUtil.compare(mIndex, inObj2.mIndex);
186      }
187
188      return result;
189   }
190}