KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > ValueType


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository;
17
18 /**
19  * Enumeration of the possible kinds of values that a field can have.
20  *
21  * <p>Each {@link Field} is based on a {@link org.outerj.daisy.repository.schema.FieldType}
22  * which defines the ValueType for the field.
23  *
24  */

25 public final class ValueType {
26     private final String JavaDoc name;
27     private final int code;
28     private final Class JavaDoc clazz;
29
30     private ValueType(String JavaDoc name, int code, Class JavaDoc clazz) {
31         this.name = name;
32         this.code = code;
33         this.clazz = clazz;
34     }
35
36     public String JavaDoc toString() {
37         return name;
38     }
39
40     public int getCode() {
41         return code;
42     }
43
44     public Class JavaDoc getTypeClass() {
45         return clazz;
46     }
47
48     public static ValueType fromString(String JavaDoc name) {
49         if (name.equals("string"))
50             return ValueType.STRING;
51         else if (name.equals("date"))
52             return ValueType.DATE;
53         else if (name.equals("datetime"))
54             return ValueType.DATETIME;
55         else if (name.equals("long"))
56             return ValueType.LONG;
57         else if (name.equals("double"))
58             return ValueType.DOUBLE;
59         else if (name.equals("decimal"))
60             return ValueType.DECIMAL;
61         else if (name.equals("boolean"))
62             return ValueType.BOOLEAN;
63         else if (name.equals("link"))
64             return ValueType.LINK;
65         else
66             throw new RuntimeException JavaDoc("Unrecognized ValueType name: " + name);
67     }
68
69     public static ValueType getByCode(int code) {
70         switch (code) {
71             case 1:
72                 return STRING;
73             case 2:
74                 return DATE;
75             case 3:
76                 return DATETIME;
77             case 4:
78                 return LONG;
79             case 5:
80                 return DOUBLE;
81             case 6:
82                 return DECIMAL;
83             case 7:
84                 return BOOLEAN;
85             case 8:
86                 return LINK;
87             default:
88                 throw new RuntimeException JavaDoc("Non-existing ValueType code: " + code);
89         }
90     }
91
92     public static final ValueType STRING = new ValueType("string", 1, String JavaDoc.class);
93     public static final ValueType DATE = new ValueType("date", 2, java.util.Date JavaDoc.class);
94     public static final ValueType DATETIME = new ValueType("datetime", 3, java.util.Date JavaDoc.class);
95     public static final ValueType LONG = new ValueType("long", 4, Long JavaDoc.class);
96     public static final ValueType DOUBLE = new ValueType("double", 5, Double JavaDoc.class);
97     public static final ValueType DECIMAL = new ValueType("decimal", 6, java.math.BigDecimal JavaDoc.class);
98     public static final ValueType BOOLEAN = new ValueType("boolean", 7, java.lang.Boolean JavaDoc.class);
99     public static final ValueType LINK = new ValueType("link", 8, VariantKey.class);
100 }
101
Popular Tags