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