01: /*
02: * Geotools2 - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2002, Geotools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: */
17: package org.geotools.feature.iso.attribute;
18:
19: import java.util.Date;
20:
21: import org.geotools.feature.iso.AttributeImpl;
22: import org.opengis.feature.type.AttributeDescriptor;
23:
24: /**
25: * A Default class that represents a Temporal attribute.
26: */
27: public class TemporalAttribute extends AttributeImpl implements
28: org.opengis.feature.simple.TemporalAttribute {
29:
30: // this might be right, maybe not, but anyway, its a default formatting
31: static java.text.DateFormat format = java.text.DateFormat
32: .getInstance();
33:
34: public TemporalAttribute(Date value, AttributeDescriptor desc) {
35: super (value, desc, null);
36: }
37:
38: public Object parse(Object value) throws IllegalArgumentException {
39: if (value == null) {
40: return value;
41: }
42:
43: Class type = getType().getBinding();
44:
45: if (type.isAssignableFrom(value.getClass())) {
46: return value;
47: }
48:
49: if (value instanceof Number) {
50: return new Date(((Number) value).longValue());
51: }
52:
53: if (value instanceof java.util.Calendar) {
54: return ((java.util.Calendar) value).getTime();
55: }
56:
57: try {
58: return format.parse(value.toString());
59: } catch (java.text.ParseException pe) {
60: throw new IllegalArgumentException("unable to parse "
61: + value + " as Date");
62: }
63: }
64:
65: public Date getDate() {
66: return (Date) getValue();
67: }
68:
69: public void setDate(Date newValue) {
70: setValue(newValue);
71: }
72:
73: }
|