View Javadoc
1   /*
2    * Copyright 2007 Keith Visco, 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:float XML Schema type.
22   *
23   * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
24   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
25   * @version $Revision$ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
26   */
27  public final class XSFloat extends AbstractRangeFacet {
28    // --------------------------------------------------------------------------
29  
30    /** Name of this XSType. */
31    public static final String NAME = "float";
32  
33    /** Type number of this XSType. */
34    public static final short TYPE = XSType.FLOAT_TYPE;
35  
36    /** A constant holding the minimum value an xsd:float can have. */
37    public static final String MIN_VALUE = Float.toString(-Float.MAX_VALUE);
38  
39    /** A constant holding the maximum value an xsd:float can have. */
40    public static final String MAX_VALUE = Float.toString(Float.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 XSFloat() {
56      this(false);
57    }
58  
59    /**
60     * Constructs a new XSFloat.
61     *
62     * @param asWrapper If true, use the java.lang wrapper class.
63     */
64    public XSFloat(final boolean asWrapper) {
65      super();
66  
67      _asWrapper = asWrapper;
68      if (_asWrapper) {
69        _jType = new JClass("java.lang.Float");
70      } else {
71        _jType = JType.FLOAT;
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.Float(0.0);";
120   }
121 
122   /**
123    * {@inheritDoc}
124    */
125   public String createToJavaObjectCode(final String variableName) {
126     if (_asWrapper) {
127       return variableName;
128     }
129     return "new java.lang.Float(" + variableName + ")";
130   }
131 
132   /**
133    * {@inheritDoc}
134    */
135   public String createFromJavaObjectCode(final String variableName) {
136     if (_asWrapper) {
137       return "((java.lang.Float) " + variableName + ")";
138     }
139     return "((java.lang.Float) " + variableName + ").floatValue()";
140   }
141 
142   // --------------------------------------------------------------------------
143 
144   /**
145    * {@inheritDoc}
146    */
147   public void validationCode(final JSourceCode jsc, final String fixedValue,
148       final String validatorInstanceName) {
149     jsc.add("org.exolab.castor.xml.validators.FloatValidator typeValidator;\n"
150         + "typeValidator = new org.exolab.castor.xml.validators.FloatValidator();\n"
151         + "{0}.setValidator(typeValidator);", validatorInstanceName);
152 
153     if (fixedValue != null) {
154       jsc.add("typeValidator.setFixed((float) " + fixedValue + ");");
155     }
156 
157     codePatternFacet(jsc, "typeValidator");
158     codeWhiteSpaceFacet(jsc, "typeValidator");
159 
160     if (getMinExclusive() != null) {
161       jsc.add("typeValidator.setMinExclusive((float) " + getMinExclusive() + ");");
162     } else if (getMinInclusive() != null) {
163       jsc.add("typeValidator.setMinInclusive((float) " + getMinInclusive() + ");");
164     }
165 
166     if (getMaxExclusive() != null) {
167       jsc.add("typeValidator.setMaxExclusive((float) " + getMaxExclusive() + ");");
168     } else if (getMaxInclusive() != null) {
169       jsc.add("typeValidator.setMaxInclusive((float) " + getMaxInclusive() + ");");
170     }
171   }
172 
173   /**
174    * {@inheritDoc}
175    */
176   public String createDefaultValueWithString(final String variableName) {
177     if (_asWrapper) {
178       return "java.lang.Float.valueOf(" + variableName + ")";
179     }
180     return "java.lang.Float.valueOf(" + variableName + ").floatValue()";
181   }
182 
183   // --------------------------------------------------------------------------
184 }