001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.feature.type;
017:
018: import org.geotools.feature.DefaultAttributeType;
019: import org.geotools.feature.IllegalAttributeException;
020: import org.geotools.feature.PrimativeAttributeType;
021: import org.geotools.util.Converters;
022: import org.opengis.filter.Filter;
023:
024: import java.util.Date;
025:
026: /**
027: * A Default class that represents a Temporal attribute.
028: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/feature/type/TemporalAttributeType.java $
029: */
030: public class TemporalAttributeType extends DefaultAttributeType
031: implements PrimativeAttributeType {
032: // this might be right, maybe not, but anyway, its a default formatting
033: static java.text.DateFormat format = java.text.DateFormat
034: .getInstance();
035:
036: public TemporalAttributeType(String name, boolean nillable,
037: int min, int max, Object defaultValue, Filter filter) {
038: super (name, java.util.Date.class, nillable, min, max,
039: defaultValue);
040: this .filter = filter;
041: }
042:
043: public TemporalAttributeType(String name, Class type,
044: boolean nillable, int min, int max, Object defaultValue,
045: Filter filter) {
046: super (name, type, nillable, min, max, defaultValue);
047: this .filter = filter;
048: }
049:
050: private Filter filter;
051:
052: public Object parse(Object value) throws IllegalArgumentException {
053: if (value == null) {
054: return value;
055: }
056:
057: if (type.isAssignableFrom(value.getClass())) {
058: return value;
059: }
060:
061: if (value instanceof Number) {
062: return new Date(((Number) value).longValue());
063: }
064:
065: if (value instanceof java.util.Calendar) {
066: return ((java.util.Calendar) value).getTime();
067: }
068:
069: //second to last restort, try the converters inteface
070: //TODO: this single call should replace this entire routine
071: Object converted = Converters.convert(value, type);
072: if (converted != null) {
073: return converted;
074: }
075:
076: //last resort
077: try {
078: return format.parse(value.toString());
079: } catch (java.text.ParseException pe) {
080: throw new IllegalArgumentException("unable to parse "
081: + value + " as Date");
082: }
083: }
084:
085: public Object duplicate(Object o) throws IllegalAttributeException {
086: if (o == null) {
087: return null;
088: }
089:
090: if (o instanceof Date) {
091: Date d = (Date) o;
092:
093: return new Date(d.getTime());
094: }
095:
096: throw new IllegalAttributeException("Cannot duplicate "
097: + o.getClass().getName());
098: }
099:
100: /* (non-Javadoc)
101: * @see org.geotools.feature.PrimativeAttributeType#getRestriction()
102: */
103: public Filter getRestriction() {
104: return filter;
105: }
106: }
|