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 @ 45673

History | View | Annotate | Download (8.8 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.Objects;
28

    
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.GeometryManager;
31
import org.gvsig.fmap.geom.exception.CreateGeometryException;
32
import org.gvsig.fmap.geom.type.AbstractGeometryType;
33
import org.gvsig.fmap.geom.type.GeometryType;
34

    
35
/**
36
 * @author gvSIG Team
37
 */
38
@SuppressWarnings("UseSpecificCatch")
39
public class DefaultGeometryType extends AbstractGeometryType {
40

    
41
    /**
42
     * Geometry type name
43
     */
44
    private String name;
45

    
46
    /**
47
     * Class that implements this class type
48
     */
49
    private Class geometryClass;
50

    
51
    /**
52
     * The type of the geometry. The type is an abstract representation of the
53
     * object (Point, Curve...) but it is not a concrete representation
54
     * (Point2D, Point3D...). To do that comparation the id field is used.
55
     */
56
    private int type;
57

    
58
    /**
59
     * The subtype of the geometry. The subtype represents a set of geometries
60
     * with a dimensional relationship (2D, 3D, 2DM...)
61
     */
62
    private int subType;
63

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

    
74
    /**
75
     * Super SubTypes of a geometry. e.g: the super SubType of a geometry 3D is
76
     * a geometry 2D, because the 3D extends the behavior of a geometry 2D
77
     */
78
    private int[] superSubTypes = null;
79

    
80
    private Constructor constructorWithGeometryType = null;
81
    private Constructor constructorWithoutParameters = null;
82
    private final Object[] parameters = {this};
83

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

    
110
        Class[] parameterTypes = {GeometryType.class};
111
        try {
112
            constructorWithGeometryType = geometryClass.getConstructor(parameterTypes);
113
        } catch (NoSuchMethodException e) {
114
            try {
115
                constructorWithoutParameters = geometryClass.getConstructor();
116
            } catch (NoSuchMethodException e1) {
117
                throw new RuntimeException(
118
                        "Error constructor of the geometry class " + geomClass
119
                        + " with one parameter of type GeometryType not found", e);
120
            }
121
        }
122
    }
123

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

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

    
159
    @Override
160
    public Class getGeometryClass() {
161
        return geometryClass;
162
    }
163

    
164
    @Override
165
    public String getName() {
166
        return name;
167
    }
168

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

    
198
    @Override
199
    public int getType() {
200
        return type;
201
    }
202

    
203
    @Override
204
    public int getSubType() {
205
        return subType;
206
    }
207

    
208
    @Override
209
    public boolean isTypeOf(int geometryType) {
210
        if (type == geometryType) {
211
            return true;
212
        }
213
        for (int i = 0; i < superTypes.length; i++) {
214
            if (superTypes[i] == geometryType) {
215
                return true;
216
            }
217
        }
218
        return Geometry.TYPES.GEOMETRY == geometryType;
219
    }
220

    
221
    @Override
222
    public boolean isSubTypeOf(int geometrySubType) {
223
        if (subType == geometrySubType) {
224
            return true;
225
        }
226
        for (int i = 0; i < superSubTypes.length; i++) {
227
            if (superSubTypes[i] == geometrySubType) {
228
                return true;
229
            }
230
        }
231
        return false;
232
    }
233

    
234
    @Override
235
    public int getDimension() {
236
        switch (this.subType) {
237
            case Geometry.SUBTYPES.GEOM2D:
238
                return 2;
239
            case Geometry.SUBTYPES.GEOM3D:
240
            case Geometry.SUBTYPES.GEOM2DM:
241
                return 3;
242
            case Geometry.SUBTYPES.GEOM3DM:
243
                return 4;
244
            default:
245
                return -1;
246
        }
247
    }
248

    
249
    @Override
250
    public boolean equals(Object obj) {
251
        if (!(obj instanceof GeometryType)) {
252
            return false;
253
        }
254
        GeometryType other = (GeometryType) obj;
255
        if (!this.name.equals(other.getName())) {
256
            return false;
257
        }
258
        if (!this.geometryClass.getName().equals(other.getGeometryClass().getName())) {
259
            return false;
260
        }
261
        if (this.type != other.getType()) {
262
            return false;
263
        }
264
        if (this.subType != other.getSubType()) {
265
            return false;
266
        }
267
        return true;
268
    }
269

    
270
    @Override
271
    public int hashCode() {
272
        int hash = 7;
273
        hash = 79 * hash + Objects.hashCode(this.name);
274
        hash = 79 * hash + Objects.hashCode(this.geometryClass.getName());
275
        hash = 79 * hash + this.type;
276
        hash = 79 * hash + this.subType;
277
        return hash;
278
    }
279

    
280
}