001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository;
017:
018: /**
019: * Enumeration of the possible kinds of values that a field can have.
020: *
021: * <p>Each {@link Field} is based on a {@link org.outerj.daisy.repository.schema.FieldType}
022: * which defines the ValueType for the field.
023: *
024: */
025: public enum ValueType {
026: STRING("string", 1, String.class), DATE("date", 2,
027: java.util.Date.class), DATETIME("datetime", 3,
028: java.util.Date.class), LONG("long", 4, Long.class), DOUBLE(
029: "double", 5, Double.class), DECIMAL("decimal", 6,
030: java.math.BigDecimal.class), BOOLEAN("boolean", 7,
031: java.lang.Boolean.class), LINK("link", 8, VariantKey.class);
032:
033: private final String name;
034: private final int code;
035: private final Class clazz;
036:
037: private ValueType(String name, int code, Class clazz) {
038: this .name = name;
039: this .code = code;
040: this .clazz = clazz;
041: }
042:
043: public String toString() {
044: return name;
045: }
046:
047: public int getCode() {
048: return code;
049: }
050:
051: public Class getTypeClass() {
052: return clazz;
053: }
054:
055: public static ValueType fromString(String name) {
056: if (name.equals("string"))
057: return ValueType.STRING;
058: else if (name.equals("date"))
059: return ValueType.DATE;
060: else if (name.equals("datetime"))
061: return ValueType.DATETIME;
062: else if (name.equals("long"))
063: return ValueType.LONG;
064: else if (name.equals("double"))
065: return ValueType.DOUBLE;
066: else if (name.equals("decimal"))
067: return ValueType.DECIMAL;
068: else if (name.equals("boolean"))
069: return ValueType.BOOLEAN;
070: else if (name.equals("link"))
071: return ValueType.LINK;
072: else
073: throw new RuntimeException("Unrecognized ValueType name: "
074: + name);
075: }
076:
077: public static ValueType getByCode(int code) {
078: switch (code) {
079: case 1:
080: return STRING;
081: case 2:
082: return DATE;
083: case 3:
084: return DATETIME;
085: case 4:
086: return LONG;
087: case 5:
088: return DOUBLE;
089: case 6:
090: return DECIMAL;
091: case 7:
092: return BOOLEAN;
093: case 8:
094: return LINK;
095: default:
096: throw new RuntimeException("Non-existing ValueType code: "
097: + code);
098: }
099: }
100: }
|