001package com.hfg.util.collection;
002
003import java.util.Comparator;
004
005//------------------------------------------------------------------------------
006/**
007 Representation of a Matrix cell.
008 <div>
009  @author J. Alex Taylor, hairyfatguy.com
010 </div>
011 */
012//------------------------------------------------------------------------------
013// com.hfg Library
014//
015// This library is free software; you can redistribute it and/or
016// modify it under the terms of the GNU Lesser General Public
017// License as published by the Free Software Foundation; either
018// version 2.1 of the License, or (at your option) any later version.
019//
020// This library is distributed in the hope that it will be useful,
021// but WITHOUT ANY WARRANTY; without even the implied warranty of
022// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
023// Lesser General Public License for more details.
024//
025// You should have received a copy of the GNU Lesser General Public
026// License along with this library; if not, write to the Free Software
027// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
028//
029// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
030// jataylor@hairyfatguy.com
031//------------------------------------------------------------------------------
032
033public class MatrixCell<RK extends Comparable, CK extends Comparable, V extends Comparable> implements Cloneable, Comparable<MatrixCell<RK, CK, V>>
034{
035   private RK  mRowKey;
036   private CK  mColKey;
037   private V   mValue;
038   private int mSortedCellIndex = -1;
039
040   public static final MatrixCellValueComparator  VALUE_COMPARATOR = new MatrixCellValueComparator();
041
042   //##########################################################################
043   // CONSTRUCTORS
044   //##########################################################################
045
046   //--------------------------------------------------------------------------
047   private MatrixCell()
048   {
049   }
050
051   //--------------------------------------------------------------------------
052   public MatrixCell(RK inRowKey, CK inColKey)
053   {
054      mRowKey = inRowKey;
055      mColKey = inColKey;
056   }
057
058   //--------------------------------------------------------------------------
059   public MatrixCell(RK inRowKey, CK inColKey, V inValue)
060   {
061      this(inRowKey, inColKey);
062      mValue = inValue;
063   }
064
065   //##########################################################################
066   // PUBLIC METHODS
067   //##########################################################################
068
069   //--------------------------------------------------------------------------
070   @Override
071   public String toString()
072   {
073      return mRowKey + ", " + mColKey + ": " + mValue;
074   }
075
076   //--------------------------------------------------------------------------
077   public MatrixCell<RK, CK, V> setRowKey(RK inRowKey)
078   {
079      mRowKey = inRowKey;
080      return this;
081   }
082
083   //--------------------------------------------------------------------------
084   public RK getRowKey()
085   {
086      return mRowKey;
087   }
088
089   //--------------------------------------------------------------------------
090   public MatrixCell<RK, CK, V> setColKey(CK inColKey)
091   {
092      mColKey = inColKey;
093      return this;
094   }
095
096   //--------------------------------------------------------------------------
097   public CK getColKey()
098   {
099      return mColKey;
100   }
101
102   //--------------------------------------------------------------------------
103   public V getValue()
104   {
105      return mValue;
106   }
107
108   //--------------------------------------------------------------------------
109   public MatrixCell<RK, CK, V> setValue(V inValue)
110   {
111      mValue = inValue;
112      return this;
113   }
114
115   //--------------------------------------------------------------------------
116   public int getSortedCellIndex()
117   {
118      return mSortedCellIndex;
119   }
120
121   //--------------------------------------------------------------------------
122   public void setSortedCellIndex(int inValue)
123   {
124      mSortedCellIndex = inValue;
125   }
126
127   //--------------------------------------------------------------------------
128   @Override
129   public MatrixCell<RK, CK, V> clone()
130   {
131      MatrixCell<RK, CK, V> clone;
132      try
133      {
134         clone = (MatrixCell<RK, CK, V>) super.clone();
135      }
136      catch (CloneNotSupportedException e)
137      {
138         throw new RuntimeException(e);
139      }
140
141      return clone;
142   }
143
144   //--------------------------------------------------------------------------
145   @Override
146   public int compareTo(MatrixCell<RK, CK, V> inMatrixCell2)
147   {
148      int result = 0;
149      if (inMatrixCell2 != null)
150      {
151         if (getValue() != null)
152         {
153            if (inMatrixCell2.getValue() != null)
154            {
155               result = getValue().compareTo(inMatrixCell2.getValue());
156            }
157            else
158            {
159               result = 1;
160            }
161         }
162         else if (inMatrixCell2.getValue() != null)
163         {
164            result = -1;
165         }
166
167         if (0 == result)
168         {
169            if (getRowKey() != null)
170            {
171               if (inMatrixCell2.getRowKey() != null)
172               {
173                  result = getRowKey().compareTo(inMatrixCell2.getRowKey());
174               }
175               else
176               {
177                  result = 1;
178               }
179            }
180            else if (inMatrixCell2.getRowKey() != null)
181            {
182               result = -1;
183            }
184
185            if (0 == result)
186            {
187               if (getColKey() != null)
188               {
189                  if (inMatrixCell2.getColKey() != null)
190                  {
191                     result = getColKey().compareTo(inMatrixCell2.getColKey());
192                  }
193                  else
194                  {
195                     result = 1;
196                  }
197               }
198               else if (inMatrixCell2.getColKey() != null)
199               {
200                  result = -1;
201               }
202            }
203         }
204      }
205      else
206      {
207         result = 1;
208      }
209
210      return result;
211   }
212
213   //##########################################################################
214   // INNER CLASS
215   //##########################################################################
216
217   private static class MatrixCellValueComparator implements Comparator<MatrixCell>
218   {
219      public int compare(MatrixCell inCell1, MatrixCell inCell2)
220      {
221         int result = 0;
222         if (inCell1 != null)
223         {
224            if (inCell2 != null)
225            {
226               if (inCell1.getValue() != null)
227               {
228                  if (inCell2.getValue() != null)
229                  {
230                     result = inCell1.getValue().compareTo(inCell2.getValue());
231                  }
232                  else
233                  {
234                     result = 1;
235                  }
236               }
237               else if (inCell2.getValue() != null)
238               {
239                  result = -1;
240               }
241            }
242            else
243            {
244               result = 1;
245            }
246         }
247         else if (inCell2 != null)
248         {
249            result = -1;
250         }
251
252         return result;
253      }
254   }
255}