View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.common.xml;
21  
22  import java.sql.Date;
23  import java.sql.Timestamp;
24  
25  /**
26   * Type of Classes supported in Enum type
27   */
28  public enum XmlType {
29    BOOLEAN(Boolean.TYPE), INTEGER(Integer.TYPE), FLOAT(Float.TYPE),
30    CHARACTER(Character.TYPE), BYTE(Byte.TYPE), LONG(Long.TYPE),
31    DOUBLE(Double.TYPE), SHORT(Short.TYPE), SQLDATE(Date.class),
32    TIMESTAMP(Timestamp.class), STRING(String.class), XVAL(XmlValue.class),
33    EMPTY(XmlType.class);
34  
35    final Class<?> classType;
36  
37    /**
38     * @param classType
39     */
40    XmlType(final Class<?> classType) {
41      this.classType = classType;
42    }
43  
44    /**
45     * @return the associated Native Java class
46     */
47    public Class<?> getClassType() {
48      return classType;
49    }
50  
51    /**
52     * @param value
53     *
54     * @return True if the Object is natively compatible with the internal Type
55     */
56    public final boolean isNativelyCompatible(final Object value) {
57      final Class<?> type = value.getClass();
58      switch (this) {
59        case BOOLEAN:
60          return type.equals(Boolean.TYPE) ||
61                 Boolean.class.isAssignableFrom(type);
62        case INTEGER:
63          return type.equals(Integer.TYPE) ||
64                 Integer.class.isAssignableFrom(type);
65        case FLOAT:
66          return type.equals(Float.TYPE) || Float.class.isAssignableFrom(type);
67        case CHARACTER:
68          return type.equals(Character.TYPE) ||
69                 Character.class.isAssignableFrom(type);
70        case BYTE:
71          return type.equals(Byte.TYPE) || Byte.class.isAssignableFrom(type);
72        case LONG:
73          return type.equals(Long.TYPE) || Long.class.isAssignableFrom(type);
74        case DOUBLE:
75          return type.equals(Double.TYPE) || Double.class.isAssignableFrom(type);
76        case SHORT:
77          return type.equals(Short.TYPE) || Short.class.isAssignableFrom(type);
78        case SQLDATE:
79          return Date.class.isAssignableFrom(type);
80        case TIMESTAMP:
81          return Timestamp.class.isAssignableFrom(type);
82        case STRING:
83          return String.class.isAssignableFrom(type);
84        case XVAL:
85          return type.isArray() &&
86                 type.getName().contains(XmlValue.class.getName());
87        case EMPTY:
88        default:
89          return false;
90      }
91    }
92  
93    public final boolean isBoolean() {
94      return this == BOOLEAN;
95    }
96  
97    public final boolean isInteger() {
98      return this == INTEGER;
99    }
100 
101   public final boolean isFloat() {
102     return this == FLOAT;
103   }
104 
105   public final boolean isCharacter() {
106     return this == CHARACTER;
107   }
108 
109   public final boolean isByte() {
110     return this == BYTE;
111   }
112 
113   public boolean isLong() {
114     return this == LONG;
115   }
116 
117   public final boolean isDouble() {
118     return this == DOUBLE;
119   }
120 
121   public final boolean isShort() {
122     return this == SHORT;
123   }
124 
125   public final boolean isDate() {
126     return this == SQLDATE;
127   }
128 
129   public final boolean isTimestamp() {
130     return this == TIMESTAMP;
131   }
132 
133   public final boolean isString() {
134     return this == STRING;
135   }
136 
137   public final boolean isXmlValue() {
138     return this == XVAL;
139   }
140 
141   public final boolean isEmpty() {
142     return this == EMPTY;
143   }
144 }