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:double 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 XSDouble extends AbstractRangeFacet {
28  
29    /** Name of this XSType. */
30    public static final String NAME = "double";
31  
32    /** Type number of this XSType. */
33    public static final short TYPE = XSType.DOUBLE_TYPE;
34  
35    /** A constant holding the minimum value an xsd:float can have. */
36    public static final String MIN_VALUE = Double.toString(-Double.MAX_VALUE);
37  
38    /** A constant holding the maximum value an xsd:float can have. */
39    public static final String MAX_VALUE = Double.toString(Double.MAX_VALUE);
40  
41    public static final String INFINITY_VALUE = "Double.POSITIVE_INFINITY";
42    public static final String NEGATIVE_INFINITY_VALUE = "Double.NEGATIVE_INFINITY";
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     * No-arg constructor.
52     */
53    public XSDouble() {
54      this(false);
55    }
56  
57    /**
58     * Constructs a new XSDouble.
59     *
60     * @param asWrapper If true, use the java.lang wrapper class.
61     */
62    public XSDouble(final boolean asWrapper) {
63      super();
64  
65      _asWrapper = asWrapper;
66      if (_asWrapper) {
67        _jType = new JClass("java.lang.Double");
68      } else {
69        _jType = JType.DOUBLE;
70      }
71  
72      setMinInclusive(MIN_VALUE);
73      setMaxInclusive(MAX_VALUE);
74  
75      setPositiveInfinity(INFINITY_VALUE);
76      setNegativeInfinity(NEGATIVE_INFINITY_VALUE);
77    }
78  
79    /**
80     * {@inheritDoc}
81     */
82    public String getName() {
83      return NAME;
84    }
85  
86    /**
87     * {@inheritDoc}
88     */
89    public short getType() {
90      return TYPE;
91    }
92  
93    /**
94     * {@inheritDoc}
95     */
96    public boolean isPrimitive() {
97      return true;
98    }
99  
100   /**
101    * {@inheritDoc}
102    */
103   public boolean isDateTime() {
104     return false;
105   }
106 
107   /**
108    * {@inheritDoc}
109    */
110   public JType getJType() {
111     return _jType;
112   }
113 
114   /**
115    * {@inheritDoc}
116    */
117   public String newInstanceCode() {
118     return "new java.lang.Double(0.0);";
119   }
120 
121   /**
122    * {@inheritDoc}
123    */
124   public String createToJavaObjectCode(final String variableName) {
125     if (_asWrapper) {
126       return variableName;
127     }
128     return "new java.lang.Double(" + variableName + ")";
129   }
130 
131   /**
132    * {@inheritDoc}
133    */
134   public String createFromJavaObjectCode(final String variableName) {
135     if (_asWrapper) {
136       return "((java.lang.Double) " + variableName + ")";
137     }
138     return "((java.lang.Double) " + variableName + ").doubleValue()";
139   }
140 
141   /**
142    * {@inheritDoc}
143    */
144   public void validationCode(final JSourceCode jsc, final String fixedValue,
145       final String validatorInstanceName) {
146     jsc.add("org.exolab.castor.xml.validators.DoubleValidator typeValidator;\n"
147         + "typeValidator = new org.exolab.castor.xml.validators.DoubleValidator();\n"
148         + "{0}.setValidator(typeValidator);", validatorInstanceName);
149 
150     if (fixedValue != null) {
151       jsc.add("typeValidator.setFixed(" + fixedValue + ");");
152     }
153 
154     codePatternFacet(jsc, "typeValidator");
155     codeWhiteSpaceFacet(jsc, "typeValidator");
156 
157     if (getMinExclusive() != null) {
158       jsc.add("typeValidator.setMinExclusive(" + getMinExclusive() + ");");
159     } else if (getMinInclusive() != null) {
160       jsc.add("typeValidator.setMinInclusive(" + getMinInclusive() + ");");
161     }
162 
163     if (getMaxExclusive() != null) {
164       jsc.add("typeValidator.setMaxExclusive(" + getMaxExclusive() + ");");
165     } else if (getMaxInclusive() != null) {
166       jsc.add("typeValidator.setMaxInclusive(" + getMaxInclusive() + ");");
167     }
168 
169     if (getPositiveInfinity() != null) {
170       jsc.add("typeValidator.setPositiveInfinity(java.lang." + getPositiveInfinity() + ");");
171     }
172 
173     if (getNegativeInfinity() != null) {
174       jsc.add("typeValidator.setNegativeInfinity(java.lang." + getNegativeInfinity() + ");");
175     }
176   }
177 
178   /**
179    * {@inheritDoc}
180    */
181   public String createDefaultValueWithString(final String variableName) {
182     if (_asWrapper) {
183       return "java.lang.Double.valueOf(" + variableName + ")";
184     }
185     return "java.lang.Double.valueOf(" + variableName + ").doubleValue()";
186   }
187 
188 }