diff options
Diffstat (limited to 'sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataTypeImpl.java')
-rw-r--r-- | sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataTypeImpl.java | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataTypeImpl.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataTypeImpl.java new file mode 100644 index 0000000000..f62c90292d --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataTypeImpl.java @@ -0,0 +1,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'}; +} |