Statistics
| Revision:

svn-gvsig-desktop / 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 @ 42170

History | View | Annotate | Download (7.12 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program 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
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontext;
25

    
26
import java.awt.geom.Rectangle2D;
27

    
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.dynobject.DynStruct;
30
import org.gvsig.tools.persistence.PersistenceManager;
31
import org.gvsig.tools.persistence.Persistent;
32
import org.gvsig.tools.persistence.PersistentState;
33
import org.gvsig.tools.persistence.exception.PersistenceException;
34
import org.gvsig.tools.util.Callable;
35

    
36
/**
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>
44
 *
45
 * @author Vicente Caballero Navarro
46
 */
47
public class ExtentHistory implements Persistent {
48

    
49
  /**
50
   * <p>
51
   * Maximum number of extents that can store.
52
   * </p>
53
   */
54
  private int NUMREC;
55

    
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;
67

    
68
  private Rectangle2D[] extentsNext;
69

    
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;
81

    
82
  private int numNext = 0;
83

    
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
  }
93

    
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
  }
108

    
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
  }
132

    
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
  }
147

    
148
  public boolean hasNext() {
149
    return numNext > 0;
150
  }
151

    
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];
166

    
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
  }
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
    }
202

    
203
    Rectangle2D ext = extents[--num];
204
    return ext;
205
  }
206

    
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;
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
  }
288
}