View Javadoc

1   /*	
2   	Copyright 2007-2014 Fraunhofer IGD, http://www.igd.fraunhofer.de
3   	Fraunhofer-Gesellschaft - Institute for Computer Graphics Research
4   	
5   	See the NOTICE file distributed with this work for additional 
6   	information regarding copyright ownership
7   	
8   	Licensed under the Apache License, Version 2.0 (the "License");
9   	you may not use this file except in compliance with the License.
10  	You may obtain a copy of the License at
11  	
12  	  http://www.apache.org/licenses/LICENSE-2.0
13  	
14  	Unless required by applicable law or agreed to in writing, software
15  	distributed under the License is distributed on an "AS IS" BASIS,
16  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  	See the License for the specific language governing permissions and
18  	limitations under the License.
19   */
20  package org.universAAL.middleware.xsd;
21  
22  import org.universAAL.middleware.rdf.TypeMapper;
23  
24  /**
25   * Support for XSD data type nonNegativeInteger. Currently, only int values are
26   * supported (in the range 0 - 2^31).
27   * 
28   * @see <a href="http://www.w3.org/TR/xmlschema-2/#nonNegativeInteger">XML
29   *      Schema</a>
30   */
31  public final class NonNegativeInteger {
32      public static final String MY_URI = TypeMapper.XSD_NAMESPACE
33  	    + "nonNegativeInteger";
34  
35      private boolean isInt;
36  
37      private int intval;
38  
39      // private BigInteger bigintval;
40  
41      public NonNegativeInteger(int val) {
42  	if (val < 0)
43  	    throw new IllegalArgumentException(
44  		    "NonNegativeInteger must be non-negative: " + val);
45  	isInt = true;
46  	intval = val;
47      }
48  
49      public NonNegativeInteger(String val) {
50  	int intval = Integer.valueOf(val).intValue();
51  	if (intval < 0)
52  	    throw new IllegalArgumentException(
53  		    "NonNegativeInteger must be non-negative: " + val);
54  
55  	isInt = true;
56  	this.intval = intval;
57  	// TODO: if the value is too big, use BigInteger
58      }
59  
60      public int intValue() {
61  	if (isInt)
62  	    return intval;
63  	else
64  	    return Integer.MAX_VALUE;
65      }
66  
67      public String toString() {
68  	return Integer.toString(intval);
69      }
70  
71      public boolean equals(Object obj) {
72  	if (this == obj)
73  	    return true;
74  	if (obj instanceof NonNegativeInteger)
75  	    return this.intval == ((NonNegativeInteger) obj).intval;
76  	if (obj instanceof Integer)
77  	    return this.intval == ((Integer) obj).intValue();
78  	return false;
79      }
80  }