001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-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.visitor;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collection;
021: import java.util.HashSet;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import com.vividsolutions.jts.geom.Envelope;
027: import com.vividsolutions.jts.geom.Geometry;
028: import com.vividsolutions.jts.geom.Point;
029:
030: /**
031: * An abstract implementation for CalcResults. Each subclass should implement
032: * its own getValue(), merge(), and constructor methods.
033: *
034: * @author Cory Horner, Refractions
035: *
036: * @since 2.2.M2
037: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/feature/visitor/AbstractCalcResult.java $
038: */
039: public class AbstractCalcResult implements CalcResult {
040: public boolean isCompatible(CalcResult targetResults) {
041: return false;
042: }
043:
044: public CalcResult merge(CalcResult resultsToAdd) {
045: return null;
046: }
047:
048: public Object getValue() {
049: return null;
050: }
051:
052: public int toInt() {
053: Object value = getValue();
054: if (value instanceof Number) {
055: Number number = (Number) value;
056: return number.intValue();
057: } else {
058: return (int) 0;
059: }
060: }
061:
062: public double toDouble() {
063: Object value = getValue();
064: if (value instanceof Number) {
065: Number number = (Number) value;
066: return number.doubleValue();
067: } else {
068: return (double) 0;
069: }
070: }
071:
072: public long toLong() {
073: Object value = getValue();
074: if (value instanceof Number) {
075: Number number = (Number) value;
076: return number.longValue();
077: } else {
078: return (long) 0;
079: }
080: }
081:
082: public float toFloat() {
083: Object value = getValue();
084: if (value instanceof Number) {
085: Number number = (Number) value;
086: return number.floatValue();
087: } else {
088: return (float) 0;
089: }
090: }
091:
092: public Geometry toGeometry() {
093: Object value = getValue();
094: if (value instanceof Geometry)
095: return (Geometry) getValue();
096: else
097: return null;
098: }
099:
100: public Envelope toEnvelope() {
101: Object value = getValue();
102: if (value instanceof Envelope)
103: return (Envelope) value;
104: else
105: return null;
106: }
107:
108: public Point toPoint() {
109: Geometry geometry = toGeometry();
110: return geometry.getCentroid();
111: }
112:
113: public Set toSet() {
114: Object value = getValue();
115:
116: if (value == null) {
117: return null;
118: }
119:
120: if (value instanceof Set) {
121: Set set = (Set) value;
122:
123: return set;
124: }
125:
126: if (value.getClass().isArray()) {
127: Set set = new HashSet(Arrays.asList((Object[]) value));
128:
129: return set;
130: }
131:
132: if (value instanceof Collection) {
133: Set set = new HashSet((Collection) value);
134:
135: return set;
136: }
137:
138: return null;
139: }
140:
141: public List toList() {
142: Object value = getValue();
143:
144: if (value == null) {
145: return null;
146: }
147:
148: if (value instanceof List) {
149: List list = (List) value;
150:
151: return list;
152: }
153:
154: if (value.getClass().isArray()) {
155: return Arrays.asList((Object[]) value);
156: }
157:
158: if (value instanceof HashSet) {
159: Set set = (HashSet) value;
160: // Object[] values = set.toArray();
161: return Arrays.asList(set.toArray());
162: // List list = new ArrayList();
163: // for (int i = 0; i < values.length; i++)
164: // list.add(values[i]);
165: // return list;
166: }
167:
168: if (value instanceof Collection) {
169: return new ArrayList((Collection) value);
170: }
171:
172: return null;
173: }
174:
175: public Object[] toArray() {
176: List list = toList();
177:
178: if (list == null) {
179: return null;
180: }
181:
182: return list.toArray();
183: }
184:
185: public String[] toStringArray() {
186: List list = toList();
187:
188: if (list == null) {
189: return null;
190: }
191:
192: String[] strings = new String[list.size()];
193:
194: return (String[]) list.toArray(strings);
195: }
196:
197: public Map toMap() {
198: return (Map) getValue();
199: }
200:
201: public String toString() {
202: return getValue().toString();
203: }
204: }
|