1 24 package javax.jcr; 25 26 import java.util.Calendar ; 27 import java.util.Date ; 28 29 35 public class DoubleValue extends BaseValue { 36 37 public static final int TYPE = PropertyType.DOUBLE; 38 39 private final Double dblNumber; 40 41 46 public DoubleValue(Double dblNumber) { 47 super(TYPE); 48 this.dblNumber = dblNumber; 49 } 50 51 56 public DoubleValue(double dbl) { 57 super(TYPE); 58 this.dblNumber = new Double (dbl); 59 } 60 61 71 public static DoubleValue valueOf(String s) throws ValueFormatException { 72 try { 73 return new DoubleValue(Double.parseDouble(s)); 74 } catch (NumberFormatException e) { 75 throw new ValueFormatException("invalid format", e); 76 } 77 } 78 79 90 public boolean equals(Object obj) { 91 if (this == obj) { 92 return true; 93 } 94 if (obj instanceof DoubleValue) { 95 DoubleValue other = (DoubleValue) obj; 96 if (dblNumber == other.dblNumber) { 97 return true; 98 } else if (dblNumber != null && other.dblNumber != null) { 99 return dblNumber.equals(other.dblNumber); 100 } 101 } 102 return false; 103 } 104 105 109 public Calendar getDate() throws ValueFormatException, IllegalStateException , RepositoryException { 110 setValueConsumed(); 111 112 if (dblNumber != null) { 113 Calendar cal = Calendar.getInstance(); 115 cal.setTime(new Date (dblNumber.longValue())); 116 return cal; 117 } else { 118 throw new ValueFormatException("empty value"); 119 } 120 } 121 122 125 public long getLong() throws ValueFormatException, IllegalStateException , RepositoryException { 126 setValueConsumed(); 127 128 if (dblNumber != null) { 129 return dblNumber.longValue(); 130 } else { 131 throw new ValueFormatException("empty value"); 132 } 133 } 134 135 138 public boolean getBoolean() throws ValueFormatException, IllegalStateException , RepositoryException { 139 setValueConsumed(); 140 141 throw new ValueFormatException("conversion to boolean failed: inconvertible types"); 142 } 143 144 147 public double getDouble() throws ValueFormatException, IllegalStateException , RepositoryException { 148 setValueConsumed(); 149 150 if (dblNumber != null) { 151 return dblNumber.doubleValue(); 152 } else { 153 throw new ValueFormatException("empty value"); 154 } 155 } 156 157 160 public String getString() throws ValueFormatException, IllegalStateException , RepositoryException { 161 setValueConsumed(); 162 163 if (dblNumber != null) { 164 return dblNumber.toString(); 165 } else { 166 throw new ValueFormatException("empty value"); 167 } 168 } 169 } 170 | Popular Tags |