KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > database > Money


1 /* =============================================================
2  * SmallSQL : a free Java DBMS library for the Java(tm) platform
3  * =============================================================
4  *
5  * (C) Copyright 2004-2006, by Volker Berlin.
6  *
7  * Project Info: http://www.smallsql.de/
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------
28  * Money.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  */

33 package smallsql.database;
34
35 import java.math.*;
36
37 public class Money extends Number JavaDoc implements Mutable{
38
39     /**
40      *
41      */

42     private static final long serialVersionUID = -620300937494609089L;
43     long value;
44
45     
46     /**
47      * Is use from factory methods only.
48      */

49     private Money(){/* should be empty */}
50
51     
52     public Money(double value){
53         this.value = (long)(value * 10000);
54     }
55
56     public Money(float value){
57         this.value = (long)(value * 10000);
58     }
59     
60
61     public static Money createFromUnscaledValue(long value){
62         Money money = new Money();
63         money.value = value;
64         return money;
65     }
66
67     public static Money createFromUnscaledValue(int value){
68         Money money = new Money();
69         money.value = value;
70         return money;
71     }
72
73     public int intValue() {
74         return (int)(value / 10000.0);
75     }
76     public float floatValue() {
77         return value / 10000.0F;
78     }
79     public double doubleValue() {
80         return value / 10000.0;
81     }
82     public long longValue() {
83         return (long)(value / 10000.0);
84     }
85
86     public String JavaDoc toString(){
87         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
88         buffer.append(longValue()).append('.');
89         final long v = Math.abs(value);
90         buffer.append( (char)((v % 10000) / 1000 + '0') );
91         buffer.append( (char)((v % 1000) / 100 + '0') );
92         buffer.append( (char)((v % 100) / 10 + '0') );
93         buffer.append( (char)((v % 10) + '0') );
94         
95         return buffer.toString();
96     }
97
98     public boolean equals(Object JavaDoc obj){
99         return (obj instanceof Money && ((Money)obj).value == value);
100     }
101
102     public int hashCode(){
103         return (int)(value ^ (value >>> 32));
104     }
105
106     public long unscaledValue(){
107         return value;
108     }
109
110     public static long parseMoney( String JavaDoc str ){
111         // FIXME ohne Umweg über double implementieren
112
return Utils.doubleToMoney(Double.parseDouble( str ));
113     }
114     
115     private byte[] toByteArray(){
116         byte[] bytes = new byte[8];
117         
118         int offset = 0;
119         bytes[offset++] = (byte)(value >> 56);
120         bytes[offset++] = (byte)(value >> 48);
121         bytes[offset++] = (byte)(value >> 40);
122         bytes[offset++] = (byte)(value >> 32);
123         bytes[offset++] = (byte)(value >> 24);
124         bytes[offset++] = (byte)(value >> 16);
125         bytes[offset++] = (byte)(value >> 8);
126         bytes[offset++] = (byte)(value);
127         return bytes;
128     }
129     
130     public BigDecimal toBigDecimal(){
131         if(value == 0) return ZERO;
132         return new BigDecimal( new BigInteger( toByteArray() ), 4 );
133     }
134
135
136     public Object JavaDoc getImmutableObject(){
137         return toBigDecimal();
138     }
139     
140     static private final BigDecimal ZERO = new BigDecimal("0.0000");
141 }
Popular Tags