Revision 42170 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.mapcontext/org.gvsig.fmap.mapcontext.api/src/main/java/org/gvsig/fmap/mapcontext/ExtentHistory.java

View differences:

ExtentHistory.java
34 34
import org.gvsig.tools.util.Callable;
35 35

  
36 36
/**
37
 * <p><code>ExtentHistory</code> is designed for managing a history of extents.</p>
37
 * <p>
38
 * <code>ExtentHistory</code> is designed for managing a history of extents.
39
 * </p>
40
 * <p>
41
 * Note: An <i>extent</i> is a rectangular area, with information of its
42
 * top-left 2D corner.
43
 * </p>
38 44
 *
39
 * <p>Note: An <i>extent</i> is a re.setMandatory(true)ctangular area, with information of its top-left 2D corner.</p>
40
 *
41 45
 * @author Vicente Caballero Navarro
42 46
 */
43 47
public class ExtentHistory implements Persistent {
44
	
45
	/**
46
	 * <p>Maximum number of extents that can store.</p>
47
	 */
48
	private int NUMREC;
49 48

  
50
	/**
51
	 * <p>Array with the extents.</p>
52
	 *
53
	 * @see #hasPrevious()
54
	 * @see #put(Rectangle2D)
55
	 * @see #get()
56
	 * @see #removePrev()
57
	 */
58
	private Rectangle2D[] extents;
49
  /**
50
   * <p>
51
   * Maximum number of extents that can store.
52
   * </p>
53
   */
54
  private int NUMREC;
59 55

  
60
	/**
61
	 * <p>Number of extents stored.</p>
62
	 *
63
	 * @see #hasPrevious()
64
	 * @see #put(Rectangle2D)
65
	 * @see #get()
66
	 * @see #removePrev()
67
	 */
68
	private int num = 0;
56
  /**
57
   * <p>
58
   * Array with the extents.
59
   * </p>
60
   *
61
   * @see #hasPrevious()
62
   * @see #put(Rectangle2D)
63
   * @see #get()
64
   * @see #removePrev()
65
   */
66
  private Rectangle2D[] extents;
69 67

  
70
	
71
	/**
72
	 * <p>Creates a new instance of <code>ExtentsHistory</code> with an history of 10 extents.</p>
73
	 */
74
	public ExtentHistory() {
75
		this(10);
76
	}
68
  private Rectangle2D[] extentsNext;
77 69

  
78
	
79
	/**
80
	 * <p>Creates a new instance of <code>ExtentsHistory</code> with an history of <code>numEntries</code> extents.</p>
81
	 *
82
	 * @param numEntries the maximum number of extents that will store the instance
83
	 */
84
	public ExtentHistory(int numEntries) {
85
		NUMREC = numEntries;
86
		extents = new Rectangle2D[NUMREC];
87
	}
70
  /**
71
   * <p>
72
   * Number of extents stored.
73
   * </p>
74
   *
75
   * @see #hasPrevious()
76
   * @see #put(Rectangle2D)
77
   * @see #get()
78
   * @see #removePrev()
79
   */
80
  private int num = 0;
88 81

  
89
	/**
90
	 * <p>Appends the specified extent to the end of this history.</p>
91
	 *
92
	 * @param ext the new extent
93
	 *
94
	 * @see #get()
95
	 * @see #hasPrevious()
96
	 */
97
	public void put(Rectangle2D ext) {
98
		if ((ext != null) && ((num < 1) || (ext != extents[num - 1]))) {
99
			if (num < (NUMREC)) {
100
				extents[num] = ext;
101
				num = num + 1;
102
			} else {
103
				for (int i = 0; i < (NUMREC - 1); i++) {
104
					extents[i] = extents[i + 1];
105
				}
82
  private int numNext = 0;
106 83

  
107
				extents[num - 1] = ext;
108
			}
109
		}
110
	}
84
  /**
85
   * <p>
86
   * Creates a new instance of <code>ExtentsHistory</code> with an history of 10
87
   * extents.
88
   * </p>
89
   */
90
  public ExtentHistory() {
91
    this(10);
92
  }
111 93

  
112
	/**
113
	 * <p>Returns <code>true</code> if there are extents registered.</p>
114
	 *
115
	 * @return <code>true</code> if there are extents registered; <code>false</code> otherwise
116
	 *
117
	 * @see #put(Rectangle2D)
118
	 * @see #removePrev()
119
	 * @see #get()
120
	 */
121
	public boolean hasPrevious() {
122
		return num > 0;
123
	}
94
  /**
95
   * <p>
96
   * Creates a new instance of <code>ExtentsHistory</code> with an history of
97
   * <code>numEntries</code> extents.
98
   * </p>
99
   *
100
   * @param numEntries the maximum number of extents that will store the
101
   *          instance
102
   */
103
  public ExtentHistory(int numEntries) {
104
    NUMREC = numEntries;
105
    extents = new Rectangle2D[NUMREC];
106
    extentsNext = new Rectangle2D[NUMREC];
107
  }
124 108

  
125
	/**
126
	 * <p>Returns the last extent in the history.</p>
127
	 *
128
	 * @return the last extent in the history
129
	 *
130
	 * @see #put(Rectangle2D)
131
	 * @see #getXMLEntity()
132
	 */
133
	public Rectangle2D get() {
134
		if (num <= 0) {
135
			return null;
136
		}
137
		Rectangle2D ext = extents[num - 1];
109
  /**
110
   * <p>
111
   * Appends the specified extent to the end of this history.
112
   * </p>
113
   *
114
   * @param ext the new extent
115
   * @see #get()
116
   * @see #hasPrevious()
117
   */
118
  public void put(Rectangle2D ext) {
119
    if ((ext != null) && ((num < 1) || (ext != extents[num - 1]))) {
120
      if (num < (NUMREC)) {
121
        extents[num] = ext;
122
        num = num + 1;
123
      }
124
      else {
125
        for (int i = 0; i < (NUMREC - 1); i++) {
126
          extents[i] = extents[i + 1];
127
        }
128
        extents[num - 1] = ext;
129
      }
130
    }
131
  }
138 132

  
139
		return ext;
140
	}
133
  /**
134
   * <p>
135
   * Returns <code>true</code> if there are extents registered.
136
   * </p>
137
   *
138
   * @return <code>true</code> if there are extents registered;
139
   *         <code>false</code> otherwise
140
   * @see #put(Rectangle2D)
141
   * @see #removePrev()
142
   * @see #get()
143
   */
144
  public boolean hasPrevious() {
145
    return num > 0;
146
  }
141 147

  
142
	/**
143
	 * <p>Extracts (removing) the last extent from the history.</p>
144
	 *
145
	 * @return last extent in the history
146
	 *
147
	 * @see #hasPrevious()
148
	 */
149
	public Rectangle2D removePrev() {
150
		if (num <= 0) {
151
			return null;
152
		}
153
		Rectangle2D ext = extents[--num];
154
		return ext;
155
	}
148
  public boolean hasNext() {
149
    return numNext > 0;
150
  }
156 151

  
157
	public void loadFromState(PersistentState state)
158
			throws PersistenceException {
159
		
160
		num = state.getInt("num");
161
		NUMREC = state.getInt("numrec");
162
		extents = (Rectangle2D[]) state.getArray("extents", Rectangle2D.class);
163
	}
152
  /**
153
   * <p>
154
   * Returns the last extent in the history.
155
   * </p>
156
   *
157
   * @return the last extent in the history
158
   * @see #put(Rectangle2D)
159
   * @see #getXMLEntity()
160
   */
161
  public Rectangle2D get() {
162
    if (num <= 0) {
163
      return null;
164
    }
165
    Rectangle2D ext = extents[num - 1];
164 166

  
165
	/**
166
	 * <p>
167
	 * Returns information of this object. All information is stored as
168
	 * properties:<br>
169
	 * </p>
170
	 * <p>
171
	 * <b>Properties:</b>
172
	 * <ul>
173
	 * <li><i>className</i>: name of this class.
174
	 * <li><i>num</i>: number of extents registered.
175
	 * <li><i>numrec</i>: maximum number of extents that can register.
176
	 * <li><i>extents</i>: .
177
	 * </ul>
178
	 * </p>
179
	 * 
180
	 */
181
	public void saveToState(PersistentState state) throws PersistenceException {
182
		
183
		state.set("num", num);
184
		state.set("numrec", NUMREC);
185
		state.set("extents", extents);
186
	}
167
    return ext;
168
  }
169
  
170
  public Rectangle2D getNext() {
171
    if (numNext <= 0) {
172
      return null;
173
    }
174
    
175
    Rectangle2D ext = extentsNext[numNext - 1];
176
    
177
    return ext;
178
  }
187 179

  
180
  /**
181
   * <p>
182
   * Extracts (removing) the last extent from the history.
183
   * </p>
184
   *
185
   * @return last extent in the history
186
   * @see #hasPrevious()
187
   */
188
  public Rectangle2D removePrev() {
189
    if (num <= 0) {
190
      return null;
191
    }
192
    if (numNext < (NUMREC)) {
193
      extentsNext[numNext] = extents[num - 1];
194
      numNext++;
195
    }
196
    else {
197
      for (int i = 0; i < (NUMREC - 1); i++) {
198
        extentsNext[i] = extentsNext[i + 1];
199
      }
200
      extentsNext[numNext - 1] = extents[num - 1];
201
    }
188 202

  
189
    public static class RegisterPersistence implements Callable {
203
    Rectangle2D ext = extents[--num];
204
    return ext;
205
  }
190 206

  
191
        public Object call() {
192
		PersistenceManager manager = ToolsLocator.getPersistenceManager();
193
		DynStruct definition = manager.addDefinition(
194
				ExtentHistory.class,
195
				"ExtentHistory",
196
				"ExtentHistory Persistence definition",
197
				null, 
198
				null
199
		);
200
		definition.addDynFieldInt("num").setMandatory(true);
201
		definition.addDynFieldInt("numrec").setMandatory(true);
202
		definition.addDynFieldArray("extents").setClassOfItems(Rectangle2D.class).setMandatory(true);
203
            return Boolean.TRUE;
204
        }
207
  /**
208
   * <p>
209
   * Extracts (removing) the last extent from the history.
210
   * </p>
211
   *
212
   * @return last extent in the history
213
   * @see #hasNext()
214
   */
215
  public Rectangle2D removeNext() {
216
    if (numNext <= 0) {
217
      return null;
205 218
    }
219

  
220
    if (num < (NUMREC)) {
221
      extents[num] = extentsNext[numNext - 1];
222
    }
223
    else {
224
      for (int i = 0; i < (NUMREC - 1); i++) {
225
        extents[i] = extents[i + 1];
226
      }
227
      extents[num - 1] = extentsNext[numNext - 1];
228
    }
229

  
230
    Rectangle2D ext = extentsNext[--numNext];
231
    return ext;
232
  }
233

  
234
  
235
  public void clear() {
236
    num = 0;
237
    numNext = 0;
238
  }
239

  
240
  
241
  public void loadFromState(PersistentState state) throws PersistenceException {
242

  
243
    num = state.getInt("num");
244
    NUMREC = state.getInt("numrec");
245
    extents = (Rectangle2D[]) state.getArray("extents", Rectangle2D.class);
246
    extentsNext = (Rectangle2D[]) state.getArray("extentsNext",
247
        Rectangle2D.class);
248
  }
249

  
250
  /**
251
   * <p>
252
   * Returns information of this object. All information is stored as
253
   * properties:<br>
254
   * </p>
255
   * <p>
256
   * <b>Properties:</b>
257
   * <ul>
258
   * <li><i>className</i>: name of this class.
259
   * <li><i>num</i>: number of extents registered.
260
   * <li><i>numrec</i>: maximum number of extents that can register.
261
   * <li><i>extents</i>: .
262
   * </ul>
263
   * </p>
264
   */
265
  public void saveToState(PersistentState state) throws PersistenceException {
266

  
267
    state.set("num", num);
268
    state.set("numrec", NUMREC);
269
    state.set("extents", extents);
270
    state.set("extentsNext", extentsNext);
271
  }
272

  
273
  public static class RegisterPersistence implements Callable {
274

  
275
    public Object call() {
276
      PersistenceManager manager = ToolsLocator.getPersistenceManager();
277
      DynStruct definition = manager.addDefinition(ExtentHistory.class,
278
          "ExtentHistory", "ExtentHistory Persistence definition", null, null);
279
      definition.addDynFieldInt("num").setMandatory(true);
280
      definition.addDynFieldInt("numrec").setMandatory(true);
281
      definition.addDynFieldArray("extents").setClassOfItems(Rectangle2D.class)
282
          .setMandatory(true);
283
      definition.addDynFieldArray("extentsNext")
284
          .setClassOfItems(Rectangle2D.class).setMandatory(true);
285
      return Boolean.TRUE;
286
    }
287
  }
206 288
}

Also available in: Unified diff