001package com.hfg.util.collection;
002
003import java.util.Comparator;
004import java.util.Map;
005import javax.swing.SortOrder;
006
007import com.hfg.util.CompareUtil;
008
009//------------------------------------------------------------------------------
010/**
011 * Simple Comparator for sorting Map entries with Number values.
012 * @author J. Alex Taylor, hairyfatguy.com
013 */
014//------------------------------------------------------------------------------
015// com.hfg Library
016//
017// This library is free software; you can redistribute it and/or
018// modify it under the terms of the GNU Lesser General Public
019// License as published by the Free Software Foundation; either
020// version 2.1 of the License, or (at your option) any later version.
021//
022// This library is distributed in the hope that it will be useful,
023// but WITHOUT ANY WARRANTY; without even the implied warranty of
024// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
025// Lesser General Public License for more details.
026//
027// You should have received a copy of the GNU Lesser General Public
028// License along with this library; if not, write to the Free Software
029// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030//
031// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
032// jataylor@hairyfatguy.com
033//------------------------------------------------------------------------------
034
035public class MapEntryNumberValueComparator implements Comparator<Map.Entry<? extends Object, ? extends Number>>
036{
037   private SortOrder mSortOrder = SortOrder.ASCENDING;
038
039   //##########################################################################
040   // CONSTRUCTORS
041   //##########################################################################
042
043   //--------------------------------------------------------------------------
044   public MapEntryNumberValueComparator()
045   {
046   }
047
048   //--------------------------------------------------------------------------
049   public MapEntryNumberValueComparator(SortOrder inSortOrder)
050   {
051      mSortOrder = inSortOrder;
052   }
053
054   //##########################################################################
055   // PUBLIC METHODS
056   //##########################################################################
057
058   //--------------------------------------------------------------------------
059   public int compare(Map.Entry<? extends Object, ? extends Number> inObj1, Map.Entry<? extends Object, ? extends Number> inObj2)
060   {
061      int result = CompareUtil.compare(inObj1.getValue(), inObj2.getValue()) * (mSortOrder == SortOrder.DESCENDING ? -1 : 1);
062
063      if (0 == result)
064      {
065         if (inObj1.getKey() instanceof Comparable)
066         {
067            result = CompareUtil.compare((Comparable) inObj1.getKey(), (Comparable) inObj2.getKey());
068         }
069         else
070         {
071            // Compare the keys case-insensitive alphabetical
072            String str1 = (inObj1.getKey() != null ? inObj1.getKey().toString() : null);
073            String str2 = (inObj2.getKey() != null ? inObj2.getKey().toString() : null);
074            result = CompareUtil.compareIgnoreCase(str1, str2);
075         }
076      }
077
078      return result;
079   }
080
081}