Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / DefaultGeometryType.java @ 47762

History | View | Annotate | Download (9.39 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.geom.jts;
25

    
26
import java.lang.reflect.Constructor;
27
import java.util.Collection;
28
import java.util.Collections;
29
import java.util.HashSet;
30
import java.util.Objects;
31
import java.util.Set;
32

    
33
import org.gvsig.fmap.geom.Geometry;
34
import org.gvsig.fmap.geom.GeometryManager;
35
import org.gvsig.fmap.geom.exception.CreateGeometryException;
36
import org.gvsig.fmap.geom.type.AbstractGeometryType;
37
import org.gvsig.fmap.geom.type.GeometryType;
38
import org.gvsig.tools.dataTypes.DataType;
39

    
40
/**
41
 * @author gvSIG Team
42
 */
43
@SuppressWarnings("UseSpecificCatch")
44
public class DefaultGeometryType extends AbstractGeometryType {
45

    
46
    /**
47
     * Geometry type name
48
     */
49
    private String name;
50

    
51
    /**
52
     * Class that implements this class type
53
     */
54
    private Class geometryClass;
55

    
56
    /**
57
     * The type of the geometry. The type is an abstract representation of the
58
     * object (Point, Curve...) but it is not a concrete representation
59
     * (Point2D, Point3D...). To do that comparation the id field is used.
60
     */
61
    private int type;
62

    
63
    /**
64
     * The subtype of the geometry. The subtype represents a set of geometries
65
     * with a dimensional relationship (2D, 3D, 2DM...)
66
     */
67
    private int subType;
68

    
69
    /**
70
     * Super types of a geometry. e.g: the super type of an arc is a curve, the
71
     * super type of a circle is a surface... The supertypes are defined in the
72
     * ISO 19107
73
     *
74
     * @see <a href="http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=26012"
75
     * >ISO 19107< /a>
76
     */
77
    private int[] superTypes = null;
78

    
79
    /**
80
     * Super SubTypes of a geometry. e.g: the super SubType of a geometry 3D is
81
     * a geometry 2D, because the 3D extends the behavior of a geometry 2D
82
     */
83
    private int[] superSubTypes = null;
84

    
85
    private Constructor constructorWithGeometryType = null;
86
    private Constructor constructorWithoutParameters = null;
87
    private final Object[] parameters = {this};
88
    
89
    private Set<String> alias;
90

    
91
    /**
92
     * This constructor is used by the {@link GeometryManager} when it register
93
     * a new GeometryType.It has not be used from other parts.
94
     *
95
     * @param geomClass Geometry class (e.g: Point2D.class)
96
     * @param name Symbolic Geometry name that is used to persist the geometry
97
     * type. In some cases, it is better to use this name because the id can
98
     * change for different application executions.
99
     * @param type The geometry abstract type
100
     * @param subType
101
     * @param superTypes The superTypes of the geometry type
102
     * @param superSubTypes The superSubtypes of the geometry type
103
     */
104
    public DefaultGeometryType(Class geomClass, String name, int type, int subType,
105
            int[] superTypes, int[] superSubTypes) {
106
        this.geometryClass = geomClass;
107
        if (name == null) {
108
            this.name = geomClass.getName();
109
        } else {
110
            this.name = name;
111
        }
112
        this.type = type;
113
        this.subType = subType;
114
        this.superTypes = superTypes;
115
        this.superSubTypes = superSubTypes;
116

    
117
        Class[] parameterTypes = {GeometryType.class};
118
        try {
119
            constructorWithGeometryType = geometryClass.getConstructor(parameterTypes);
120
        } catch (NoSuchMethodException e) {
121
            try {
122
                constructorWithoutParameters = geometryClass.getConstructor();
123
            } catch (NoSuchMethodException e1) {
124
                throw new RuntimeException(
125
                        "Error constructor of the geometry class " + geomClass
126
                        + " with one parameter of type GeometryType not found", e);
127
            }
128
        }
129
    }
130

    
131
    /**
132
     * This constructor is used by the {@link GeometryManager} when it register
133
     * a new GeometryType.It has not be used from other parts.
134
     *
135
     * @param geomClass Geometry class (e.g: Point2D.class)
136
     * @param name Symbolic Geometry name that is used to persist the geometry
137
     * type. In some cases, it is better to use this name because the id can
138
     * change for different application executions.
139
     * @param type The geometry abstract type
140
     * @param subType
141
     */
142
    public DefaultGeometryType(Class geomClass, String name, int type, int subType) {
143
        this(geomClass, name, type, subType, new int[0], new int[0]);
144
    }
145

    
146
    /**
147
     * This method creates a {@link Geometry} with the type specified by this
148
     * GeometryType. The geometry has to have a constructor without arguments.
149
     *
150
     * @return A new geometry
151
     * @throws CreateGeometryException
152
     */
153
    @Override
154
    public Geometry create() throws CreateGeometryException {
155
        try {
156
            if (constructorWithGeometryType != null) {
157
                return (Geometry) constructorWithGeometryType.newInstance(parameters);
158
            } else {
159
                return (Geometry) constructorWithoutParameters.newInstance();
160
            }
161
        } catch (Exception e) {
162
            throw new CreateGeometryException(type, subType, e);
163
        }
164
    }
165

    
166
    @Override
167
    public Class getGeometryClass() {
168
        return geometryClass;
169
    }
170

    
171
    @Override
172
    public String getName() {
173
        return name;
174
    }
175

    
176
    @Override
177
    public String getFullName() {
178
        String subtypename;
179
        switch (this.subType) {
180
            case Geometry.SUBTYPES.GEOM2D:
181
                subtypename = "2D";
182
                break;
183
            case Geometry.SUBTYPES.GEOM2DM:
184
                subtypename = "2DM";
185
                break;
186
            case Geometry.SUBTYPES.GEOM3D:
187
                subtypename = "3D";
188
                break;
189
            case Geometry.SUBTYPES.GEOM3DM:
190
                subtypename = "3DM";
191
                break;
192
            default:
193
                subtypename = "subtype" + this.subType;
194
        }
195
        if (name.endsWith(subtypename)) {
196
            try {
197
                return name.substring(0, name.length() - subtypename.length()) + ":" + subtypename;
198
            } catch (Throwable th) {
199
                // 
200
            }
201
        }
202
        return name + ":" + subtypename;
203
    }
204

    
205
    @Override
206
    public int getType() {
207
        return type;
208
    }
209

    
210
    @Override
211
    public int getSubType() {
212
        return subType;
213
    }
214

    
215
    @Override
216
    public boolean isTypeOf(int geometryType) {
217
        if (type == geometryType) {
218
            return true;
219
        }
220
        for (int i = 0; i < superTypes.length; i++) {
221
            if (superTypes[i] == geometryType) {
222
                return true;
223
            }
224
        }
225
        return Geometry.TYPES.GEOMETRY == geometryType;
226
    }
227

    
228
    @Override
229
    public boolean isSubTypeOf(int geometrySubType) {
230
        if (subType == geometrySubType) {
231
            return true;
232
        }
233
        for (int i = 0; i < superSubTypes.length; i++) {
234
            if (superSubTypes[i] == geometrySubType) {
235
                return true;
236
            }
237
        }
238
        return false;
239
    }
240

    
241
    @Override
242
    public int getDimension() {
243
        switch (this.subType) {
244
            case Geometry.SUBTYPES.GEOM2D:
245
                return 2;
246
            case Geometry.SUBTYPES.GEOM3D:
247
            case Geometry.SUBTYPES.GEOM2DM:
248
                return 3;
249
            case Geometry.SUBTYPES.GEOM3DM:
250
                return 4;
251
            default:
252
                return -1;
253
        }
254
    }
255

    
256
    @Override
257
    public boolean equals(Object obj) {
258
        if (!(obj instanceof GeometryType)) {
259
            return false;
260
        }
261
        GeometryType other = (GeometryType) obj;
262
        if (!this.name.equals(other.getName())) {
263
            return false;
264
        }
265
        if (!this.geometryClass.getName().equals(other.getGeometryClass().getName())) {
266
            return false;
267
        }
268
        if (this.type != other.getType()) {
269
            return false;
270
        }
271
        if (this.subType != other.getSubType()) {
272
            return false;
273
        }
274
        return true;
275
    }
276

    
277
    @Override
278
    public int hashCode() {
279
        int hash = 7;
280
        hash = 79 * hash + Objects.hashCode(this.name);
281
        hash = 79 * hash + Objects.hashCode(this.geometryClass.getName());
282
        hash = 79 * hash + this.type;
283
        hash = 79 * hash + this.subType;
284
        return hash;
285
    }
286

    
287
    @Override
288
    public GeometryType addAlias(String alias) {
289
        if (this.alias == null) {
290
            this.alias = new HashSet<>();
291
        }
292
        this.alias.add(alias);
293
        return this;
294
    }
295

    
296
    @Override
297
    public Collection<String> getAlias() {
298
        if (this.alias == null) {
299
            return Collections.EMPTY_LIST;
300
        }
301
        return Collections.unmodifiableCollection(this.alias);
302
    }
303

    
304

    
305
}