Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / DefaultDataType.java @ 2081

History | View | Annotate | Download (6.78 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 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
 * 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.tools.dataTypes.impl;
25

    
26
import java.text.MessageFormat;
27
import java.util.ArrayList;
28
import java.util.List;
29

    
30
import org.gvsig.tools.dataTypes.CoercionException;
31
import org.gvsig.tools.dataTypes.DataType;
32
import org.gvsig.tools.dataTypes.DataTypes;
33
import org.gvsig.tools.dataTypes.Coercion;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36
import org.gvsig.tools.dataTypes.CoercionContext;
37

    
38
public class DefaultDataType implements DataType {
39

    
40
  private static final Logger LOG = LoggerFactory.getLogger(DefaultDataTypesManager.class);
41

    
42
  private Coercion coercion;
43
  private Class defaultClass;
44
  private String subtype;
45
  private int type;
46
  private String name;
47
  private String iconName;
48

    
49
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion) {
50
    this(type, subtype, name, defaultClass, coercion, "datatype-any");
51
  }
52

    
53
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName) {
54

    
55
    if (name == null) {
56
      LOG.trace("Can't register null type name for type {}.", new Object[]{Integer.toHexString(type).toUpperCase()});
57
      throw new IllegalArgumentException();
58
    }
59
    this.type = type;
60
    this.subtype = subtype;
61
    this.name = name;
62
    this.defaultClass = defaultClass;
63
    this.coercion = coercion;
64
    this.iconName = iconName == null ? "datatype-any" : iconName;
65
  }
66

    
67
  @Override
68
  public DataType clone() throws CloneNotSupportedException {
69
    DefaultDataType other = (DefaultDataType) super.clone();
70
    if (other.coercion instanceof Coercions) {
71
      this.coercion = ((Coercions) this.coercion).clone();
72
    }
73
    return other;
74
  }
75

    
76
  @Override
77
  public String getLabel() {
78
    return this.getName();
79
  }
80

    
81
  @Override
82
  public DataType getValue() {
83
    return this;
84
  }
85

    
86
  @Override
87
  public Object coerce(Object value) throws CoercionException {
88
    // http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion
89

    
90
    if (this.coercion != null) {
91
      return this.coercion.coerce(value);
92
    }
93
    if (defaultClass == null) {
94
      return value; // ??
95
    }
96
    if (defaultClass.isInstance(value)) {
97
      return value;
98
    }
99
    throw new CoercionException();
100
  }
101

    
102

    
103
  @Override
104
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
105
    // http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion
106
    if (this.coercion != null) {
107
      return this.coercion.coerce(value, context);
108
    }
109
    if (defaultClass == null) {
110
      return value; // ??
111
    }
112
    if (defaultClass.isInstance(value)) {
113
      return value;
114
    }
115
    throw new CoercionException();
116
  }
117

    
118
  @Override
119
  public Coercion getCoercion() {
120
    return this.coercion;
121
  }
122

    
123
  @Override
124
  public Class getDefaultClass() {
125
    return this.defaultClass;
126
  }
127

    
128
  @Override
129
  public String getSubtype() {
130
    return this.subtype;
131
  }
132

    
133
  @Override
134
  public int getType() {
135
    return this.type;
136
  }
137

    
138
  @Override
139
  public String getName() {
140
    return this.name;
141
  }
142

    
143
  @Override
144
  public String getIconName() {
145
    return this.iconName;
146
  }
147

    
148
  @Override
149
  public boolean isContainer() {
150
    return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
151
  }
152

    
153
  @Override
154
  public boolean isObject() {
155
    return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
156
  }
157

    
158
  @Override
159
  public boolean isDynObject() {
160
    return type == DataTypes.DYNOBJECT;
161
  }
162

    
163
//  @Override
164
  public void setCoercion(Coercion coercion) {
165
    this.coercion = coercion;
166
    LOG.trace("Add coercion operation for data type {}.", new Object[]{name});
167
  }
168

    
169
  @Override
170
  public void addCoercion(Coercion coercion) {
171
    if (this.coercion == null) {
172
      this.setCoercion(coercion);
173
      return;
174
    }
175
    Coercions coercions;
176
    if (this.coercion instanceof Coercions) {
177
      coercions = (Coercions) this.coercion;
178
    } else {
179
      coercions = new Coercions();
180
      coercions.add(this.coercion);
181
      this.coercion = coercions;
182
    }
183
    coercions.add(coercion);
184
    LOG.trace("Add coercion operation for data type {}.", new Object[]{name});
185
  }
186

    
187
  @Override
188
  public String toString() {
189
    return MessageFormat.format(
190
            "type=0x{0};subtype={1};name={2};class={3};coercion={4}",
191
            new Object[]{
192
              Integer.toHexString(type).toUpperCase(),
193
              subtype,
194
              name,
195
              defaultClass == null ? null : defaultClass.getName(),
196
              coercion == null ? null : coercion.getClass().getName()
197
            }
198
    );
199
  }
200

    
201
  @Override
202
  public boolean isNumeric() {
203
    if (type == DataTypes.DOUBLE
204
            || type == DataTypes.FLOAT
205
            || type == DataTypes.INT
206
            || type == DataTypes.LONG
207
            || type == DataTypes.DECIMAL) {
208
      return true;
209
    }
210
    return false;
211
  }
212

    
213
  private static class Coercions
214
          implements Coercion, org.gvsig.tools.lang.Cloneable {
215

    
216
    List<Coercion> coercions;
217

    
218
    public Coercions() {
219
      this.coercions = new ArrayList<>();
220
    }
221

    
222
    public void add(Coercion coercion) {
223
      this.coercions.add(coercion);
224
    }
225

    
226
    @Override
227
    public Coercions clone() throws CloneNotSupportedException {
228
      Coercions other = (Coercions) super.clone();
229
      other.coercions = new ArrayList<>();
230
      other.coercions.addAll(this.coercions);
231
      return other;
232
    }
233

    
234
    @Override
235
    public Object coerce(Object value) throws CoercionException {
236
      return coerce(value, null);
237
    }
238

    
239
    @Override
240
    @SuppressWarnings("UnusedAssignment")
241
    public Object coerce(Object value, CoercionContext context) throws CoercionException {
242
      for (int i = this.coercions.size(); --i >= 0;) {
243
        Coercion coercion = this.coercions.get(i);
244
        try {
245
          return coercion.coerce(value, context);
246
        } catch (CoercionException e) {
247
          // Do nothing, go to next coercion
248
          coercion = null; // To allow set break point
249
        }
250
      }
251
      throw new CoercionException();
252
    }
253

    
254
  }
255

    
256
}