001package com.hfg.math; 002 003 004import com.hfg.exception.ProgrammingException; 005import com.hfg.util.CompareUtil; 006 007//------------------------------------------------------------------------------ 008/** 009 * Simple count container useful in Maps. 010 * @author J. Alex Taylor, hairyfatguy.com 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 Counter extends Number implements Comparable<Counter>, Cloneable 034{ 035 private int mValue; 036 037 //--------------------------------------------------------------------------- 038 @Override 039 public Counter clone() 040 { 041 Counter cloneObj; 042 try 043 { 044 cloneObj = (Counter) super.clone(); 045 } 046 catch (CloneNotSupportedException e) 047 { 048 throw new ProgrammingException(e); 049 } 050 051 return cloneObj; 052 } 053 054 //--------------------------------------------------------------------------- 055 public Counter add(Integer inValue) 056 { 057 if (inValue != null) 058 { 059 add(inValue.intValue()); 060 } 061 062 return this; 063 } 064 065 //--------------------------------------------------------------------------- 066 public Counter add(int inValue) 067 { 068 mValue += inValue; 069 return this; 070 } 071 072 073 //--------------------------------------------------------------------------- 074 public Counter reset() 075 { 076 mValue = 0; 077 return this; 078 } 079 080 //--------------------------------------------------------------------------- 081 public void increment() 082 { 083 mValue++; 084 } 085 086 //--------------------------------------------------------------------------- 087 public void decrement() 088 { 089 mValue--; 090 } 091 092 //--------------------------------------------------------------------------- 093 public int intValue() 094 { 095 return mValue; 096 } 097 098 //--------------------------------------------------------------------------- 099 public long longValue() 100 { 101 return intValue(); 102 } 103 104 //--------------------------------------------------------------------------- 105 public float floatValue() 106 { 107 return (float) intValue(); 108 } 109 110 //--------------------------------------------------------------------------- 111 public double doubleValue() 112 { 113 return (double) intValue(); 114 } 115 116 117 //--------------------------------------------------------------------------- 118 @Override 119 public String toString() 120 { 121 return intValue() + ""; 122 } 123 124 125 //--------------------------------------------------------------------------- 126 @Override 127 public int hashCode() 128 { 129 return intValue(); 130 } 131 132 //--------------------------------------------------------------------------- 133 @Override 134 public int compareTo(Counter inObj2) 135 { 136 int result; 137 if (inObj2 != null) 138 { 139 result = CompareUtil.compare(intValue(), inObj2.intValue()); 140 } 141 else 142 { 143 result = -1; 144 } 145 146 return result; 147 } 148}