KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > types > UnsignedLong


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.types;
17
18 import org.apache.axis.utils.Messages;
19
20 import java.math.BigInteger JavaDoc;
21
22 /**
23  * Custom class for supporting primitive XSD data type UnsignedLong
24  *
25  * @author Chris Haddad <chaddad@cobia.net>
26  * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#unsignedLong">XML Schema 3.3.21</a>
27  */

28 public class UnsignedLong extends java.lang.Number JavaDoc
29         implements java.lang.Comparable JavaDoc {
30
31     protected BigInteger JavaDoc lValue = BigInteger.ZERO;
32     private static BigInteger JavaDoc MAX = new BigInteger JavaDoc("18446744073709551615"); // max unsigned long
33

34     public UnsignedLong() {
35     }
36
37     public UnsignedLong(double value) throws NumberFormatException JavaDoc {
38         setValue(new BigInteger JavaDoc(Double.toString(value)));
39     }
40
41     public UnsignedLong(BigInteger JavaDoc value) throws NumberFormatException JavaDoc {
42         setValue(value);
43     }
44
45     public UnsignedLong(long lValue) throws NumberFormatException JavaDoc {
46         setValue(BigInteger.valueOf(lValue));
47     }
48
49     public UnsignedLong(String JavaDoc stValue) throws NumberFormatException JavaDoc {
50         setValue(new BigInteger JavaDoc(stValue));
51     }
52
53     private void setValue(BigInteger JavaDoc val) {
54         if (!UnsignedLong.isValid(val)) {
55             throw new NumberFormatException JavaDoc(Messages.getMessage(
56                     "badUnsignedLong00") +
57                     String.valueOf(val) + "]");
58         }
59         this.lValue = val;
60     }
61
62     public static boolean isValid(BigInteger JavaDoc value) {
63         if (value.compareTo(BigInteger.ZERO) == -1 || // less than zero
64
value.compareTo(MAX) == 1) {
65             return false;
66         }
67         return true;
68     }
69
70     public String JavaDoc toString() {
71         return lValue.toString();
72     }
73
74     public int hashCode() {
75         if (lValue != null)
76             return lValue.hashCode();
77         else
78             return 0;
79     }
80
81     private Object JavaDoc __equalsCalc = null;
82
83     public synchronized boolean equals(Object JavaDoc obj) {
84         if (!(obj instanceof UnsignedLong)) return false;
85         UnsignedLong other = (UnsignedLong) obj;
86         if (obj == null) return false;
87         if (this == obj) return true;
88         if (__equalsCalc != null) {
89             return (__equalsCalc == obj);
90         }
91         __equalsCalc = obj;
92         boolean _equals;
93         _equals = true &&
94                 ((lValue == null && other.lValue == null) ||
95                 (lValue != null &&
96                 lValue.equals(other.lValue)));
97         __equalsCalc = null;
98         return _equals;
99     }
100
101     // implement java.lang.comparable interface
102
public int compareTo(Object JavaDoc obj) {
103         if (lValue != null)
104             return lValue.compareTo(obj);
105         else if (equals(obj) == true)
106             return 0; // null == null
107
else
108             return 1; // object is greater
109
}
110
111     // Implement java.lang.Number interface
112
public byte byteValue() {
113         return lValue.byteValue();
114     }
115
116     public short shortValue() {
117         return lValue.shortValue();
118     }
119
120     public int intValue() {
121         return lValue.intValue();
122     }
123
124     public long longValue() {
125         return lValue.longValue();
126     }
127
128     public double doubleValue() {
129         return lValue.doubleValue();
130     }
131
132     public float floatValue() {
133         return lValue.floatValue();
134     }
135
136 }
137
Popular Tags