KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ObjectStreamException JavaDoc;
21 import java.math.BigInteger JavaDoc;
22 import java.util.Random JavaDoc;
23
24 /**
25  * Custom class for supporting primitive XSD data type nonNegativeInteger
26  *
27  * @author Russell Butek <butek@us.ibm.com>
28  * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#nonNegativeInteger">XML Schema 3.3.20</a>
29  */

30 public class NonNegativeInteger extends BigInteger JavaDoc {
31
32     public NonNegativeInteger(byte[] val) {
33         super(val);
34         checkValidity();
35     } // ctor
36

37     public NonNegativeInteger(int signum, byte[] magnitude) {
38         super(signum, magnitude);
39         checkValidity();
40     } // ctor
41

42     public NonNegativeInteger(int bitLength, int certainty, Random JavaDoc rnd) {
43         super(bitLength, certainty, rnd);
44         checkValidity();
45     } // ctor
46

47     public NonNegativeInteger(int numBits, Random JavaDoc rnd) {
48         super(numBits, rnd);
49         checkValidity();
50     } // ctor
51

52     public NonNegativeInteger(String JavaDoc val) {
53         super(val);
54         checkValidity();
55     }
56
57     public NonNegativeInteger(String JavaDoc val, int radix) {
58         super(val, radix);
59         checkValidity();
60     } // ctor
61

62     /**
63      * validate the value against the xsd definition
64      */

65     private BigInteger JavaDoc zero = new BigInteger JavaDoc("0");
66     private void checkValidity() {
67         if (compareTo(zero) < 0) {
68             throw new NumberFormatException JavaDoc(
69                     Messages.getMessage("badNonNegInt00")
70                     + ": " + this);
71         }
72     } // checkValidity
73

74     /**
75      * Work-around for http://developer.java.sun.com/developer/bugParade/bugs/4378370.html
76      * @return BigIntegerRep
77      * @throws ObjectStreamException
78      */

79     public Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc {
80         return new BigIntegerRep(toByteArray());
81     }
82     
83     protected static class BigIntegerRep implements java.io.Serializable JavaDoc {
84         private byte[] array;
85         protected BigIntegerRep(byte[] array) {
86             this.array = array;
87         }
88         protected Object JavaDoc readResolve() throws java.io.ObjectStreamException JavaDoc {
89             return new NonNegativeInteger(array);
90         }
91     }
92 } // class NonNegativeInteger
93
Popular Tags