/** * * 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 extends TypeImpl { public static final Type BOOLEAN = new BooleanDataType(Type.BOOLEAN, Boolean.TYPE); public static final Type BOOLEAN_OBJECT = new BooleanDataType(Type.BOOLEAN_OBJECT, Boolean.class); public static final Type CHARACTER = new CharacterDataType(Type.CHARACTER, Character.TYPE); public static final Type 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 = new DataTypeImpl(Type.BYTE, byte.class) { public Byte fromString(String s) { return Byte.valueOf(s); } }; public static final Type BYTES = new DataTypeImpl(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 = new DataTypeImpl(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 = new DataTypeImpl(Type.DOUBLE, double.class) { public Double fromString(String s) { return Double.valueOf(s); } }; public static final Type FLOAT = new DataTypeImpl(Type.FLOAT, float.class) { public Float fromString(String s) { return Float.valueOf(s); } }; public static final Type INT = new DataTypeImpl(Type.INT, int.class) { public Integer fromString(String s) { return Integer.valueOf(s); } }; public static final Type LONG = new DataTypeImpl(Type.LONG, long.class) { public Long fromString(String s) { return Long.valueOf(s); } }; public static final Type SHORT = new DataTypeImpl(Type.SHORT, short.class) { public Short fromString(String s) { return Short.valueOf(s); } }; public static final Type STRING = new DataTypeImpl(Type.STRING, String.class) { public String fromString(String s) { return s; } }; public DataTypeImpl(QName name, Class instanceClass) { super(name, instanceClass, Collections.EMPTY_LIST, new POJOInstanceFactory(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'}; }