001package com.hfg.math; 002 003 004//------------------------------------------------------------------------------ 005 006import java.util.List; 007 008import com.hfg.util.collection.CollectionUtil; 009 010/** 011 * General numeric utility methods. 012 * 013 * @author J. Alex Taylor, hairyfatguy.com 014 */ 015//------------------------------------------------------------------------------ 016// com.hfg XML/HTML Coding 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 NumUtil 037{ 038 039 //************************************************************************** 040 // PUBLIC FUNCTIONS 041 //************************************************************************** 042 043 //-------------------------------------------------------------------------- 044 /** 045 Compares the absolute value of two ints. The equivalent of Integer's compareTo() 046 but for int primitives. 047 */ 048 public static int compare(int inValue1, int inValue2) 049 { 050 int returnValue = 0; 051 052 if (inValue1 < inValue2) 053 { 054 returnValue = -1; 055 } 056 else if (inValue1 > inValue2) 057 { 058 returnValue = 1; 059 } 060 061 return returnValue; 062 } 063 064 //-------------------------------------------------------------------------- 065 /** 066 Compares the absolute value of two ints. 067 */ 068 public static int compareAbs(int inValue1, int inValue2) 069 { 070 return compare(Math.abs(inValue1), Math.abs(inValue2)); 071 } 072 073 074 //-------------------------------------------------------------------------- 075 public static double sum(List<? extends Number> inValues) 076 { 077 double sum = 0; 078 079 if (CollectionUtil.hasValues(inValues)) 080 { 081 for (Number value : inValues) 082 { 083 sum += value.doubleValue(); 084 } 085 } 086 087 return sum; 088 } 089 090 091 //-------------------------------------------------------------------------- 092 /** 093 A null-safe version of min() function in Integer, Float, Double, etc. 094 * @param inValue1 first number 095 * @param inValue2 second number 096 * @param <N> the type of Number object 097 * @return the minimum of the first and second numbers 098 */ 099 public static <N extends Number> N min(N inValue1, N inValue2) 100 { 101 N min = null; 102 103 if(inValue1 != null) 104 { 105 if(inValue2 != null) 106 { 107 min = (inValue1.doubleValue() <= inValue2.doubleValue() ? inValue1 : inValue2); 108 } 109 else 110 { 111 min = inValue1; 112 } 113 } 114 else if (inValue2 != null) 115 { 116 min = inValue2; 117 } 118 119 return min; 120 } 121 122 //-------------------------------------------------------------------------- 123 /** 124 A null-safe version of max() function in Integer, Float, Double, etc. 125 * @param inValue1 first number 126 * @param inValue2 second number 127 * @param <N> the type of Number object 128 * @return the maximum of the first and second numbers 129 */ 130 public static <N extends Number> N max(N inValue1, N inValue2) 131 { 132 N max = null; 133 134 if(inValue1 != null) 135 { 136 if(inValue2 != null) 137 { 138 max = (inValue1.doubleValue() >= inValue2.doubleValue() ? inValue1 : inValue2); 139 } 140 else 141 { 142 max = inValue1; 143 } 144 } 145 else if (inValue2 != null) 146 { 147 max = inValue2; 148 } 149 150 return max; 151 } 152}