Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / type / impl / DefaultGeometryType.java @ 29097

History | View | Annotate | Download (4.91 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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 2
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
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 {Iver T.I.}   {Task}
26
*/
27
 
28
package org.gvsig.fmap.geom.type.impl;
29

    
30
import java.lang.reflect.InvocationTargetException;
31
import java.util.ArrayList;
32
import java.util.List;
33

    
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.GeometryManager;
36
import org.gvsig.fmap.geom.exception.CreateGeometryException;
37
import org.gvsig.fmap.geom.operation.GeometryOperation;
38
import org.gvsig.fmap.geom.type.GeometryType;
39

    
40
/**
41
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
42
 */
43
public class DefaultGeometryType implements GeometryType {
44
        /** 
45
         * The geometry identifier
46
         */
47
        private int id;
48
        
49
        /** 
50
         * Geometry type name
51
         */
52
        private String name;
53
                
54
        /** Class that implements this class type */
55
        private Class geometryClass;
56
        
57
        /**
58
         * The type of the geometry. The type is an abstract representation
59
         * of the object (Point, Curve...) but it is not a concrete 
60
         * representation (Point2D, Point3D...). To do that comparation
61
         * the id field is used.
62
         */
63
        private int type;
64
        
65
        /**
66
         * The subtype of the geometry. The subtype represents a set of 
67
         * geometries with a dimensional relationship (2D, 3D, 2DM...)
68
         */
69
        private int subType;
70
        
71
        
72
                
73
        /** Registered operations for a concrete geometry type */
74
        private List geometryOperations = new ArrayList();
75
        
76
        /**
77
         * This constructor is used by the {@link GeometryManager} when it
78
         * register a new GeometryType. It has not be used from other
79
         * parts.
80
         * @param geomClass
81
         * Geometry class (e.g: Point2D.class)
82
         * @param name
83
         * Symbolic Geometry name that is used to persist the geometry type. In some
84
         * cases, it is better to use this name because the id can change for different
85
         * application executions.                           
86
         * @param id
87
         * Geometry id        
88
         * @param typeName
89
         * The geometry type name
90
         * @param type
91
         * The geometry abstract type                        
92
         */
93
        public DefaultGeometryType(Class geomClass, String name, int id, int type, int subType) {
94
                this.geometryClass = geomClass;
95
                if (name == null) {
96
                        this.name = geomClass.getName();
97
                } else {
98
                        this.name = name;
99
                }
100
                this.id = id;                
101
                this.type = type;        
102
                this.subType = subType;
103
        }
104
        
105
        /**
106
         * This method creates a {@link Geometry} with the type specified 
107
         * by this GeometryType. The geometry has to have a constructor
108
         * without arguments.
109
         * 
110
         * @return A new geometry
111
         * @throws CreateGeometryException 
112
         */
113
        public Geometry create() throws CreateGeometryException{
114
                try {
115
                        Class[] parameterTypes = {GeometryType.class};
116
                        Object[] parameters = {this};
117
                        return (Geometry)geometryClass.getConstructor(parameterTypes).newInstance(parameters);
118
                } catch (Exception e) {
119
                        throw new CreateGeometryException(type, subType, e);
120
                } 
121
        }
122
        
123
        /**
124
         * Guardamos una referencia a una instancia de la operación en el índice que se pasa como parámetro.
125
         * Si el índice ya está ocupado se sobrescribe.
126
         * 
127
         * @param index
128
         * @param geomOp
129
         */
130
        public void setGeometryOperation(int index, GeometryOperation geomOp) {
131
                
132
                while (index > geometryOperations.size()) {
133
                        geometryOperations.add(null);
134
                }
135
                
136
                if (index == geometryOperations.size()) {
137
                        geometryOperations.add(geomOp);
138
                } else {                                
139
                        geometryOperations.set(index, geomOp);
140
                }
141
        }
142
        
143
        public GeometryOperation getGeometryOperation(int index) {
144
                return (GeometryOperation) geometryOperations.get(index);
145
        }
146
        
147
        public Class getGeometryClass() {
148
                return geometryClass;
149
        }
150
        
151
        public String toString() {
152
                StringBuffer sb = new StringBuffer("[")
153
                .append(geometryClass.getName())
154
                .append(",[")
155
                .append(geometryOperations.toString())
156
                .append("]");
157
                
158
                return sb.toString();
159
        }
160

    
161
        /* (non-Javadoc)
162
         * @see org.gvsig.fmap.geom.type.GeometryType#getId()
163
         */
164
        public int getId() {
165
                return id;
166
        }
167

    
168
        /* (non-Javadoc)
169
         * @see org.gvsig.fmap.geom.type.GeometryType#getName()
170
         */
171
        public String getName() {
172
                return name;
173
        }
174

    
175
        /* (non-Javadoc)
176
         * @see org.gvsig.fmap.geom.type.GeometryType#getType()
177
         */
178
        public int getType() {
179
                return type;
180
        }
181

    
182
        /* (non-Javadoc)
183
         * @see org.gvsig.fmap.geom.type.GeometryType#getSubType()
184
         */
185
        public int getSubType() {
186
                return subType;
187
        }
188
}
189