View Javadoc
1   /*
2    * Copyright 2006-2007 Edward Kuns, Ralf Joachim
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.castor.builder.types;
15  
16  import org.exolab.javasource.JClass;
17  import org.exolab.javasource.JSourceCode;
18  import org.exolab.javasource.JType;
19  
20  /**
21   * The xsd:long XML Schema type.
22   * 
23   * @author <a href="mailto:edward DOT kuns AT aspect DOT com">Edward Kuns</a>
24   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
25   * @version $Revision$ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
26   */
27  public final class XSLong extends AbstractDigitsFacet {
28    // --------------------------------------------------------------------------
29  
30    /** Name of this XSType. */
31    public static final String NAME = "long";
32  
33    /** Type number of this XSType. */
34    public static final short TYPE = XSType.LONG_TYPE;
35  
36    /** A constant holding the minimum value an xsd:long can have, -2<sup>63</sup>. */
37    public static final String MIN_VALUE = Long.toString(Long.MIN_VALUE);
38  
39    /** A constant holding the maximum value an xsd:long can have, 2<sup>63</sup>-1. */
40    public static final String MAX_VALUE = Long.toString(Long.MAX_VALUE);
41  
42    // --------------------------------------------------------------------------
43  
44    /** True if this type is implemented using the wrapper class. */
45    private final boolean _asWrapper;
46  
47    /** The JType represented by this XSType. */
48    private final JType _jType;
49  
50    // --------------------------------------------------------------------------
51  
52    /**
53     * No-arg constructor.
54     */
55    public XSLong() {
56      this(false);
57    }
58  
59    /**
60     * Construct a new XSLong optionally using the wrapper class implementation.
61     *
62     * @param asWrapper If true, this class will be implemented using a wrapper.
63     */
64    public XSLong(final boolean asWrapper) {
65      super();
66  
67      _asWrapper = asWrapper;
68      if (_asWrapper) {
69        _jType = new JClass("java.lang.Long");
70      } else {
71        _jType = JType.LONG;
72      }
73  
74      setMinInclusive(MIN_VALUE);
75      setMaxInclusive(MAX_VALUE);
76    }
77  
78    // --------------------------------------------------------------------------
79  
80    /**
81     * {@inheritDoc}
82     */
83    public String getName() {
84      return NAME;
85    }
86  
87    /**
88     * {@inheritDoc}
89     */
90    public short getType() {
91      return TYPE;
92    }
93  
94    /**
95     * {@inheritDoc}
96     */
97    public boolean isPrimitive() {
98      return true;
99    }
100 
101   /**
102    * {@inheritDoc}
103    */
104   public boolean isDateTime() {
105     return false;
106   }
107 
108   /**
109    * {@inheritDoc}
110    */
111   public JType getJType() {
112     return _jType;
113   }
114 
115   /**
116    * {@inheritDoc}
117    */
118   public String newInstanceCode() {
119     return "new java.lang.Long(0);";
120   }
121 
122   /**
123    * {@inheritDoc}
124    */
125   public String createDefaultValueWithString(final String variableName) {
126     if (_asWrapper) {
127       return "new java.lang.Long(" + variableName + ")";
128     }
129     return "new java.lang.Long(" + variableName + ").longValue()";
130   }
131 
132   /**
133    * {@inheritDoc}
134    */
135   public String createToJavaObjectCode(final String variableName) {
136     if (_asWrapper) {
137       return variableName;
138     }
139     return "new java.lang.Long(" + variableName + ")";
140   }
141 
142   /**
143    * {@inheritDoc}
144    */
145   public String createFromJavaObjectCode(final String variableName) {
146     if (_asWrapper) {
147       return "((java.lang.Long) " + variableName + ")";
148     }
149     return "((java.lang.Long) " + variableName + ").longValue()";
150   }
151 
152   // --------------------------------------------------------------------------
153 
154   /**
155    * {@inheritDoc}
156    */
157   public void validationCode(final JSourceCode jsc, final String fixedValue,
158       final String validatorInstanceName) {
159     jsc.add("org.exolab.castor.xml.validators.LongValidator typeValidator;\n"
160         + "typeValidator = new org.exolab.castor.xml.validators.LongValidator();\n"
161         + "{0}.setValidator(typeValidator);", validatorInstanceName);
162 
163     if (fixedValue != null) {
164       jsc.add("typeValidator.setFixed(" + fixedValue + ");");
165     }
166 
167     codePatternFacet(jsc, "typeValidator");
168     codeWhiteSpaceFacet(jsc, "typeValidator");
169 
170     if (getMinExclusive() != null) {
171       jsc.add("typeValidator.setMinExclusive(" + getMinExclusive() + "L);");
172     } else if (getMinInclusive() != null) {
173       jsc.add("typeValidator.setMinInclusive(" + getMinInclusive() + "L);");
174     }
175 
176     if (getMaxExclusive() != null) {
177       jsc.add("typeValidator.setMaxExclusive(" + getMaxExclusive() + "L);");
178     } else if (getMaxInclusive() != null) {
179       jsc.add("typeValidator.setMaxInclusive(" + getMaxInclusive() + "L);");
180     }
181 
182     codeDigitsFacet(jsc, "typeValidator");
183   }
184 
185   // --------------------------------------------------------------------------
186 }