01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2005-2006, 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: package org.geotools.feature.visitor;
17:
18: import org.geotools.feature.Feature;
19: import org.geotools.feature.visitor.SumVisitor.SumResult;
20:
21: /**
22: * Determines the number of features in the collection
23: *
24: * @author Cory Horner, Refractions
25: *
26: * @since 2.2.M2
27: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/feature/visitor/CountVisitor.java $
28: */
29: public class CountVisitor implements FeatureCalc {
30: int count = 0;
31:
32: public void visit(Feature feature) {
33: count++;
34: }
35:
36: public int getCount() {
37: return count;
38: }
39:
40: public void setValue(int count) {
41: this .count = count;
42: }
43:
44: public void reset() {
45: this .count = 0;
46: }
47:
48: public CalcResult getResult() {
49: return new CountResult(count);
50: }
51:
52: public static class CountResult extends AbstractCalcResult {
53: private int count;
54:
55: public CountResult(int newcount) {
56: count = newcount;
57: }
58:
59: public Object getValue() {
60: return new Integer(count);
61: }
62:
63: public boolean isCompatible(CalcResult targetResults) {
64: if (targetResults instanceof CountResult)
65: return true;
66: if (targetResults instanceof SumResult)
67: return true;
68: return false;
69: }
70:
71: public CalcResult merge(CalcResult resultsToAdd) {
72: if (!isCompatible(resultsToAdd)) {
73: throw new IllegalArgumentException(
74: "Parameter is not a compatible type");
75: }
76:
77: if (resultsToAdd instanceof CountResult) {
78: //add the two counts
79: int toAdd = resultsToAdd.toInt();
80: return new CountResult(count + toAdd);
81: } else if (resultsToAdd instanceof SumResult) {
82: // we don't want to implement this twice, so we'll call the
83: // SumResult version of this function
84: return resultsToAdd.merge(this );
85: } else {
86: throw new IllegalArgumentException(
87: "The CalcResults claim to be compatible, but the appropriate merge method has not been implemented.");
88: }
89: }
90: }
91: }
|