001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 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.data.store;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.geotools.data.FeatureListener;
022: import org.geotools.feature.FeatureType;
023:
024: import com.vividsolutions.jts.geom.Envelope;
025:
026: /**
027: * State for a content entry.
028: * <p>
029: * This class maintains a cache of certain aspects of a feature type which are
030: * subject to the state of a dataset modified in a transaction. Examples of
031: * such content:
032: * <ul>
033: * <li>
034: * <li>Type FeatureType (per FeatureTypeFactory )
035: * <li>key: "bounds"; value: Envelope of dataset
036: * <li>key: "count"; value: Number of features in dataset
037: * </ul>
038: * </p>
039: *
040: * @author Jody Garnett, Refractions Research Inc.
041: * @author Justin Deoliveira, The Open Planning Project
042: */
043: public class ContentState {
044:
045: /**
046: * cached feature type
047: */
048: protected FeatureType memberType;
049: /**
050: * cached feature collection type
051: */
052: protected FeatureType collectionType;
053: /**
054: * cached number of features
055: */
056: protected int count = -1;
057: /**
058: * cached bounds of features
059: */
060: protected Envelope bounds;
061:
062: /**
063: * entry maintaining the state
064: */
065: protected ContentEntry entry;
066:
067: /**
068: * observers
069: */
070: private List listeners = new ArrayList(2);
071:
072: /**
073: * Creates a new state.
074: *
075: * @param entry The entry for the state.
076: */
077: public ContentState(ContentEntry entry) {
078: this .entry = entry;
079: this .listeners = new ArrayList(2);
080: }
081:
082: protected ContentState(ContentState state) {
083: this (state.getEntry());
084:
085: memberType = state.memberType;
086: collectionType = state.collectionType;
087: count = state.count;
088: bounds = state.bounds == null ? null : new Envelope(
089: state.bounds);
090: }
091:
092: public FeatureType getMemberType() {
093: return memberType;
094: }
095:
096: public void setMemberType(FeatureType memberType) {
097: this .memberType = memberType;
098: }
099:
100: public FeatureType getCollectionType() {
101: return collectionType;
102: }
103:
104: public void setCollectionType(FeatureType featureType) {
105: collectionType = featureType;
106: }
107:
108: public int getCount() {
109: return count;
110: }
111:
112: public void setCount(int count) {
113: this .count = count;
114: }
115:
116: public Envelope getBounds() {
117: return bounds;
118: }
119:
120: public void setBounds(Envelope bounds) {
121: this .bounds = bounds;
122: }
123:
124: /**
125: * Flushes the cache.
126: */
127: public void flush() {
128: memberType = null;
129: collectionType = null;
130: count = -1;
131: bounds = null;
132: }
133:
134: /**
135: * Access to content entry (containing content definition )
136: */
137: public ContentEntry getEntry() {
138: return entry;
139: }
140:
141: /**
142: * Cleans up the state object by clearing cache and listeners.
143: */
144: public void close() {
145: memberType = null;
146: collectionType = null;
147: if (listeners != null) {
148: listeners.clear();
149: listeners = null;
150: }
151: }
152:
153: /**
154: * Adds a listener for collection events.
155: *
156: * @param listener The listener to add
157: */
158: public void addListener(FeatureListener listener) {
159: listeners.add(listener);
160: }
161:
162: /**
163: * Removes a listener for collection events.
164: *
165: * @param listener The listener to remove
166: */
167: public void removeListener(FeatureListener listener) {
168: listeners.remove(listener);
169: }
170:
171: /**
172: * Copies the state.
173: *
174: * @return A copy of the state.
175: */
176: public ContentState copy() {
177: return new ContentState(this);
178: }
179: }
|