com.hfg.util.CompareUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.util;
//------------------------------------------------------------------------------
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.hfg.graphics.ColorUtil;
/**
Basic null-safe compare() methods.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class CompareUtil
{
//---------------------------------------------------------------------------
public static int compare(T inValue1, T inValue2)
{
int result = 0;
if (inValue1 != null)
{
if (inValue2 != null)
{
result = inValue1.compareTo(inValue2);
}
else
{
result = -1;
}
}
else if (inValue2 != null)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static int compare(T inValue1, T inValue2)
{
int result = 0;
if (inValue1 != null)
{
if (inValue2 != null)
{
result = CompareUtil.compare(inValue1.doubleValue(), inValue2.doubleValue());
}
else
{
result = -1;
}
}
else if (inValue2 != null)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static int compare(String inValue1, String inValue2)
{
int result = 0;
if (inValue1 != null)
{
if (inValue2 != null)
{
result = inValue1.compareTo(inValue2);
}
else
{
result = -1;
}
}
else if (inValue2 != null)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static int compare(double inValue1, double inValue2)
{
int result = 0;
if (inValue1 < inValue2)
{
result = -1;
}
else if (inValue1 > inValue2)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static int compare(int inValue1, int inValue2)
{
int result = 0;
if (inValue1 < inValue2)
{
result = -1;
}
else if (inValue1 > inValue2)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static int compare(Integer inValue1, Integer inValue2)
{
int result = 0;
if (inValue1 != null)
{
if (inValue2 != null)
{
if (inValue1 < inValue2)
{
result = -1;
}
else if (inValue1 > inValue2)
{
result = 1;
}
}
else
{
result = -1;
}
}
else if (inValue2 != null)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static int compare(char inValue1, char inValue2)
{
int result = 0;
if (inValue1 < inValue2)
{
result = -1;
}
else if (inValue1 > inValue2)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static int compare(Float inValue1, Float inValue2)
{
int result = 0;
if (inValue1 != null)
{
if (inValue2 != null)
{
if (inValue1 < inValue2)
{
result = -1;
}
else if (inValue1 > inValue2)
{
result = 1;
}
}
else
{
result = -1;
}
}
else if (inValue2 != null)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static int compare(Long inValue1, Long inValue2)
{
int result = 0;
if (inValue1 != null)
{
if (inValue2 != null)
{
if (inValue1 < inValue2)
{
result = -1;
}
else if (inValue1 > inValue2)
{
result = 1;
}
}
else
{
result = -1;
}
}
else if (inValue2 != null)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static int compare(Double inValue1, Double inValue2)
{
int result = 0;
if (inValue1 != null)
{
if (inValue2 != null)
{
if (inValue1 < inValue2)
{
result = -1;
}
else if (inValue1 > inValue2)
{
result = 1;
}
}
else
{
result = -1;
}
}
else if (inValue2 != null)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static > int compare(List inValue1, List inValue2)
{
int result = 0;
if (inValue1 != null)
{
if (inValue2 != null)
{
if (inValue1.size() < inValue2.size())
{
result = -1;
}
else if (inValue1.size() > inValue2.size())
{
result = 1;
}
else
{
for (int i = 0; i < inValue1.size(); i++)
{
result = CompareUtil.compare(inValue1.get(i), inValue2.get(i));
if (result != 0)
{
break;
}
}
}
}
else
{
result = -1;
}
}
else if (inValue2 != null)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static > int compare(Set inValue1, Set inValue2)
{
int result = 0;
if (inValue1 != null)
{
if (inValue2 != null)
{
if (inValue1.size() < inValue2.size())
{
result = -1;
}
else if (inValue1.size() > inValue2.size())
{
result = 1;
}
else
{
for (T value : inValue1)
{
if (! inValue2.contains(value))
{
// Comparing sorted lists to ensure consistency
List sorted1 = new ArrayList<>(inValue1);
Collections.sort(sorted1);
List sorted2 = new ArrayList<>(inValue2);
Collections.sort(sorted2);
result = CompareUtil.compare(sorted1, sorted2);
break;
}
}
}
}
else
{
result = -1;
}
}
else if (inValue2 != null)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static , V extends Comparable> int compare(Map inValue1, Map inValue2)
{
int result = 0;
if (inValue1 != null)
{
if (inValue2 != null)
{
if (inValue1.size() < inValue2.size())
{
result = -1;
}
else if (inValue1.size() > inValue2.size())
{
result = 1;
}
else
{
// The two maps are the same size.
// Compare the keys
List sortedKeys1 = new ArrayList<>(inValue1.keySet());
Collections.sort(sortedKeys1);
List sortedKeys2 = new ArrayList<>(inValue2.keySet());
Collections.sort(sortedKeys2);
result = CompareUtil.compare(sortedKeys1, sortedKeys2);
if (0 == result)
{
// Compare the values
List orderedValues1 = new ArrayList<>(sortedKeys1.size());
for (K key : sortedKeys1)
{
orderedValues1.add(inValue1.get(key));
}
List orderedValues2 = new ArrayList<>(sortedKeys2.size());
for (K key : sortedKeys2)
{
orderedValues2.add(inValue2.get(key));
}
result = CompareUtil.compare(orderedValues1, orderedValues2);
}
}
}
else
{
result = -1;
}
}
else if (inValue2 != null)
{
result = 1;
}
return result;
}
//---------------------------------------------------------------------------
public static int compare(Color inValue1, Color inValue2)
{
int result = 0;
if (inValue1 != null)
{
if (inValue2 != null)
{
result = CompareUtil.compare(ColorUtil.colorToHex(inValue1), ColorUtil.colorToHex(inValue2));
}
else
{
result = -1;
}
}
else if (inValue2 != null)
{
result = 1;
}
return result;
}
}