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

View differences:

DefaultGeometryType.java
21 21
 * For any additional information, do not hesitate to contact us
22 22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 23
 */
24

  
25 24
package org.gvsig.fmap.geom.jts;
26 25

  
27 26
import java.lang.reflect.Constructor;
27
import java.util.Objects;
28 28

  
29 29
import org.gvsig.fmap.geom.Geometry;
30 30
import org.gvsig.fmap.geom.GeometryManager;
......
33 33
import org.gvsig.fmap.geom.type.GeometryType;
34 34

  
35 35
/**
36
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
36
 * @author gvSIG Team
37 37
 */
38
@SuppressWarnings("UseSpecificCatch")
38 39
public class DefaultGeometryType extends AbstractGeometryType {
39 40

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

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

  
48
	/**
49
	 * The type of the geometry. The type is an abstract representation
50
	 * of the object (Point, Curve...) but it is not a concrete
51
	 * representation (Point2D, Point3D...). To do that comparation
52
	 * the id field is used.
53
	 */
54
	private int type;
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;
55 57

  
56
	/**
57
	 * The subtype of the geometry. The subtype represents a set of
58
	 * geometries with a dimensional relationship (2D, 3D, 2DM...)
59
	 */
60
	private int subType;
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;
61 63

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

  
72
	/**
73
     * Super SubTypes of a geometry. e.g: the super SubType of a
74
     * geometry 3D is a geometry 2D, because the 3D extends the
75
     * behavior of a geometry 2D
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
76 77
     */
77
	private int[] superSubTypes = null;
78
    private int[] superSubTypes = null;
78 79

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

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

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

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

  
152
	/**
153
	 * This method creates a {@link Geometry} with the type specified
154
	 * by this GeometryType. The geometry has to have a constructor
155
	 * without arguments.
156
	 *
157
	 * @return A new geometry
158
	 * @throws CreateGeometryException
159
	 */
160
	public Geometry create() throws CreateGeometryException{
161
		try {
162
		    if(constructorWithGeometryType!=null){
163
		        return (Geometry) constructorWithGeometryType.newInstance(parameters);
164
		    } else {
165
		        return (Geometry) constructorWithoutParameters.newInstance();
166
		    }
167
		} catch (Exception e) {
168
			throw new CreateGeometryException(type, subType, e);
169
		}
170
	}
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
    }
171 158

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

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

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

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

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

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

  
234
    @Override
240 235
    public int getDimension() {
241
        switch(this.subType) {
236
        switch (this.subType) {
242 237
            case Geometry.SUBTYPES.GEOM2D:
243 238
                return 2;
244 239
            case Geometry.SUBTYPES.GEOM3D:
......
250 245
                return -1;
251 246
        }
252 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

  
253 280
}
254

  

Also available in: Unified diff