001package com.hfg.bio.seq.alignment;
002
003
004import com.hfg.html.HTMLTag;
005import com.hfg.html.Span;
006import com.hfg.util.CompareUtil;
007
008//------------------------------------------------------------------------------
009/**
010 Container for holding percent identity components.
011 <div>
012 @author J. Alex Taylor, hairyfatguy.com
013 </div>
014 */
015//------------------------------------------------------------------------------
016// com.hfg Library
017//
018// This library is free software; you can redistribute it and/or
019// modify it under the terms of the GNU Lesser General Public
020// License as published by the Free Software Foundation; either
021// version 2.1 of the License, or (at your option) any later version.
022//
023// This library is distributed in the hope that it will be useful,
024// but WITHOUT ANY WARRANTY; without even the implied warranty of
025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
026// Lesser General Public License for more details.
027//
028// You should have received a copy of the GNU Lesser General Public
029// License along with this library; if not, write to the Free Software
030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031//
032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
033// jataylor@hairyfatguy.com
034//------------------------------------------------------------------------------
035
036public class PctId implements Comparable<PctId>
037{
038   private Integer mNumIdentities;
039   private Integer mComparisonLength;
040   private String  mFormatString = "%.1f";
041
042   //##########################################################################
043   // CONSTRUCTORS
044   //##########################################################################
045
046   //--------------------------------------------------------------------------
047   public PctId()
048   {
049   }
050
051   //--------------------------------------------------------------------------
052   public PctId(int inNumIdenties, int inComparisonLength)
053   {
054      mNumIdentities = inNumIdenties;
055      mComparisonLength = inComparisonLength;
056   }
057
058   //##########################################################################
059   // PUBLIC METHODS
060   //##########################################################################
061
062   //--------------------------------------------------------------------------
063   public int getNumIdentities()
064   {
065      return mNumIdentities;
066   }
067
068   //--------------------------------------------------------------------------
069   public PctId addIdentities(int inValue)
070   {
071      if (mNumIdentities != null)
072      {
073         mNumIdentities += inValue;
074      }
075      else
076      {
077         mNumIdentities = inValue;
078      }
079
080      return this;
081   }
082
083   //--------------------------------------------------------------------------
084   public int getComparisonLength()
085   {
086      return mComparisonLength;
087   }
088
089   //--------------------------------------------------------------------------
090   public PctId addComparisonLength(int inValue)
091   {
092      if (mComparisonLength != null)
093      {
094         mComparisonLength += inValue;
095      }
096      else
097      {
098         mComparisonLength = inValue;
099      }
100
101      return this;
102   }
103
104   //--------------------------------------------------------------------------
105   public PctId setFormatString(String inValue)
106   {
107      mFormatString = inValue;
108      return this;
109   }
110
111   //--------------------------------------------------------------------------
112   public float floatValue()
113   {
114      float result = 0.0f;
115      if (mNumIdentities != null
116            && mComparisonLength != null)
117      {
118         result = (mComparisonLength > 0 ? 100f * mNumIdentities.floatValue() / mComparisonLength.floatValue() : 0.0f);
119      }
120
121      return result;
122   }
123
124   //--------------------------------------------------------------------------
125   public int intValue()
126   {
127      return (int) floatValue();
128   }
129
130   //--------------------------------------------------------------------------
131   @Override
132   public String toString()
133   {
134      return String.format(mFormatString, floatValue());
135   }
136
137   //--------------------------------------------------------------------------
138   public HTMLTag toHTMLTag()
139   {
140      return new Span(toString()).setTitle(getNumIdentities() + " / " + getComparisonLength());
141   }
142
143   //--------------------------------------------------------------------------
144   @Override
145   public int hashCode()
146   {
147      return intValue();
148   }
149
150   //---------------------------------------------------------------------------
151   @Override
152   public boolean equals(Object inObj2)
153   {
154      return (inObj2 != null
155            && inObj2 instanceof PctId
156            && 0 == compareTo((PctId) inObj2));
157   }
158
159   //--------------------------------------------------------------------------
160   @Override
161   public int compareTo(PctId inObj2)
162   {
163      int result = -1;
164
165      if (inObj2 != null)
166      {
167         result = CompareUtil.compare(floatValue(), inObj2.floatValue());
168      }
169
170      return result;
171   }
172}