summaryrefslogtreecommitdiffstats
path: root/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataTypeImpl.java
blob: f62c90292dd719407fb60bbc0dcb8c289fd0286c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 *
 *  Copyright 2005 BEA Systems Inc.
 *  Copyright 2005 International Business Machines Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.sdo.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.TimeZone;
import javax.xml.namespace.QName;

import org.osoa.sdo.Type;

/**
 * @version $Rev$ $Date$
 */
abstract class DataTypeImpl<T> extends TypeImpl<T> {
    public static final Type<Boolean> BOOLEAN = new BooleanDataType(Type.BOOLEAN, Boolean.TYPE);
    public static final Type<Boolean> BOOLEAN_OBJECT = new BooleanDataType(Type.BOOLEAN_OBJECT, Boolean.class);
    public static final Type<Character> CHARACTER = new CharacterDataType(Type.CHARACTER, Character.TYPE);
    public static final Type<Character> CHARACTER_OBJECT = new CharacterDataType(Type.CHARACTER_OBJECT, Character.class);

    private static class BooleanDataType extends DataTypeImpl {
        public BooleanDataType(QName name, Class<?> instanceClass) {
            super(name, instanceClass);
        }

        public Boolean fromString(String s) {
            return Boolean.valueOf(s);
        }
    }

    private static class CharacterDataType extends DataTypeImpl {
        public CharacterDataType(QName name, Class<?> instanceClass) {
            super(name, instanceClass);
        }

        public Character fromString(String s) {
            return s.charAt(0);
        }
    }

    public static final Type<Byte> BYTE = new DataTypeImpl<Byte>(Type.BYTE, byte.class) {
        public Byte fromString(String s) {
            return Byte.valueOf(s);
        }
    };
    public static final Type<byte[]> BYTES = new DataTypeImpl<byte[]>(Type.BYTES, byte[].class) {
        public byte[] fromString(String s) {
            byte[] value = new byte[s.length() >> 1];
            for (int i = 0; i < value.length; i++) {
                value[i] = (byte) (Character.digit(s.charAt(i << 1), 16) << 4 | Character.digit(s.charAt((i << 1) + 1), 16));
            }
            return value;
        }

        public String toString(byte[] value) {
            StringBuilder buf = new StringBuilder(value.length << 1);
            for (byte b : value) {
                buf.append(HEX[b >>> 4]);
                buf.append(HEX[b & 0xf]);
            }
            return buf.toString();

        }
    };

    public static final Type<Date> DATE = new DataTypeImpl<Date>(Type.DATE, Date.class) {
        public Date fromString(String s) {
            try {
                DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'SSSz");
                f.setTimeZone(TimeZone.getTimeZone("GMT"));
                return f.parse(s);
            } catch (ParseException e) {
                throw (IllegalArgumentException) new IllegalArgumentException().initCause(e);
            }
        }

        public String toString(Date value) {
            DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'SSSz");
            f.setTimeZone(TimeZone.getTimeZone("GMT"));
            return f.format(value);

        }
    };
    public static final Type<Double> DOUBLE = new DataTypeImpl<Double>(Type.DOUBLE, double.class) {
        public Double fromString(String s) {
            return Double.valueOf(s);
        }
    };
    public static final Type<Float> FLOAT = new DataTypeImpl<Float>(Type.FLOAT, float.class) {
        public Float fromString(String s) {
            return Float.valueOf(s);
        }
    };
    public static final Type<Integer> INT = new DataTypeImpl<Integer>(Type.INT, int.class) {
        public Integer fromString(String s) {
            return Integer.valueOf(s);
        }
    };
    public static final Type<Long> LONG = new DataTypeImpl<Long>(Type.LONG, long.class) {
        public Long fromString(String s) {
            return Long.valueOf(s);
        }
    };
    public static final Type<Short> SHORT = new DataTypeImpl<Short>(Type.SHORT, short.class) {
        public Short fromString(String s) {
            return Short.valueOf(s);
        }
    };
    public static final Type<String> STRING = new DataTypeImpl<String>(Type.STRING, String.class) {
        public String fromString(String s) {
            return s;
        }
    };

    public DataTypeImpl(QName name, Class<T> instanceClass) {
        super(name, instanceClass, Collections.EMPTY_LIST, new POJOInstanceFactory<T>(instanceClass));
    }

    public boolean isDataType() {
        return true;
    }

    public abstract T fromString(String s);

    public String toString(T value) {
        return String.valueOf(value);
    }

    private static final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
}