001 package org.shiftone.jrat.util; 002 003 004 import org.shiftone.jrat.util.log.Logger; 005 006 import java.text.DecimalFormat; 007 008 009 /** 010 * @author jeff@shiftone.org (Jeff Drost) 011 */ 012 public class Percent extends Number implements Comparable { 013 014 private static final Logger LOG = Logger.getLogger(Percent.class); 015 public static final Percent ZERO = new Percent(0); 016 public static final Percent HUNDRED = new Percent(100); 017 private static DecimalFormat pctDecimalFormat = new DecimalFormat("#,###.0"); 018 public static final double POSITIVE_INFINITY = 1.0 / 0.0; 019 public static final double NEGATIVE_INFINITY = -1.0 / 0.0; 020 public static final double NAN = 0.0d / 0.0d; 021 public static final double MAX_VALUE = 1.7976931348623157e+308; 022 public static final double MIN_VALUE = 4.9e-324; 023 private double value; 024 025 public Percent(double value) { 026 this.value = value; 027 } 028 029 030 public Percent(String s) throws NumberFormatException { 031 032 // REMIND: this is inefficient 033 this(valueOf(s).doubleValue()); 034 } 035 036 037 public static String toString(double d) { 038 return Double.toString(d); 039 } 040 041 042 public static Percent valueOf(String s) throws NumberFormatException { 043 044 Assert.assertNotNull("string value", s); 045 046 return new Percent(Double.parseDouble(s)); 047 } 048 049 050 public static double parseDouble(String s) throws NumberFormatException { 051 052 Assert.assertNotNull("string value", s); 053 054 return Double.parseDouble(s); 055 } 056 057 058 public static boolean isNaN(double v) { 059 return (v != v); 060 } 061 062 063 public static boolean isInfinite(double v) { 064 return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY); 065 } 066 067 068 public boolean isNaN() { 069 return isNaN(value); 070 } 071 072 073 public boolean isInfinite() { 074 return isInfinite(value); 075 } 076 077 078 public String toString() { 079 080 synchronized (pctDecimalFormat) { 081 return pctDecimalFormat.format(value); 082 } 083 084 // return String.valueOf(value); 085 } 086 087 088 public byte byteValue() { 089 return (byte) value; 090 } 091 092 093 public short shortValue() { 094 return (short) value; 095 } 096 097 098 public int intValue() { 099 return (int) value; 100 } 101 102 103 public long longValue() { 104 return (long) value; 105 } 106 107 108 public float floatValue() { 109 return (float) value; 110 } 111 112 113 public double doubleValue() { 114 return (double) value; 115 } 116 117 118 public int hashCode() { 119 120 long bits = Double.doubleToLongBits(value); 121 122 return (int) (bits ^ (bits >>> 32)); 123 } 124 125 126 public boolean equals(Object obj) { 127 return ((obj instanceof Number) && ((((Number) obj).doubleValue()) == value)); 128 } 129 130 131 public int compareTo(Percent anotherPercent) { 132 133 Assert.assertNotNull("anotherPercent", anotherPercent); 134 135 Double me = new Double(value); 136 Double other = new Double(anotherPercent.value); 137 138 return me.compareTo(other); 139 } 140 141 142 public int compareTo(Object o) { 143 return compareTo((Percent) o); 144 } 145 }