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

History | View | Annotate | Download (6.35 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
  @Override
103
  public Coercion getCoercion() {
104
    return this.coercion;
105
  }
106

    
107
  @Override
108
  public Class getDefaultClass() {
109
    return this.defaultClass;
110
  }
111

    
112
  @Override
113
  public String getSubtype() {
114
    return this.subtype;
115
  }
116

    
117
  @Override
118
  public int getType() {
119
    return this.type;
120
  }
121

    
122
  @Override
123
  public String getName() {
124
    return this.name;
125
  }
126

    
127
  @Override
128
  public String getIconName() {
129
    return this.iconName;
130
  }
131

    
132
  @Override
133
  public boolean isContainer() {
134
    return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
135
  }
136

    
137
  @Override
138
  public boolean isObject() {
139
    return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
140
  }
141

    
142
  @Override
143
  public boolean isDynObject() {
144
    return type == DataTypes.DYNOBJECT;
145
  }
146

    
147
//  @Override
148
  public void setCoercion(Coercion coercion) {
149
    this.coercion = coercion;
150
    LOG.trace("Add coercion operation for data type {}.", new Object[]{name});
151
  }
152

    
153
  @Override
154
  public void addCoercion(Coercion coercion) {
155
    if (this.coercion == null) {
156
      this.setCoercion(coercion);
157
      return;
158
    }
159
    Coercions coercions;
160
    if (this.coercion instanceof Coercions) {
161
      coercions = (Coercions) this.coercion;
162
    } else {
163
      coercions = new Coercions();
164
      coercions.add(this.coercion);
165
      this.coercion = coercions;
166
    }
167
    coercions.add(coercion);
168
    LOG.trace("Add coercion operation for data type {}.", new Object[]{name});
169
  }
170

    
171
  @Override
172
  public String toString() {
173
    return MessageFormat.format(
174
            "type=0x{0};subtype={1};name={2};class={3};coercion={4}",
175
            new Object[]{
176
              Integer.toHexString(type).toUpperCase(),
177
              subtype,
178
              name,
179
              defaultClass == null ? null : defaultClass.getName(),
180
              coercion == null ? null : coercion.getClass().getName()
181
            }
182
    );
183
  }
184

    
185
  @Override
186
  public boolean isNumeric() {
187
    if (type == DataTypes.DOUBLE
188
            || type == DataTypes.FLOAT
189
            || type == DataTypes.INT
190
            || type == DataTypes.LONG
191
            || type == DataTypes.DECIMAL) {
192
      return true;
193
    }
194
    return false;
195
  }
196

    
197
  private static class Coercions
198
          implements Coercion, org.gvsig.tools.lang.Cloneable {
199

    
200
    List<Coercion> coercions;
201

    
202
    public Coercions() {
203
      this.coercions = new ArrayList<>();
204
    }
205

    
206
    public void add(Coercion coercion) {
207
      this.coercions.add(coercion);
208
    }
209

    
210
    @Override
211
    public Coercions clone() throws CloneNotSupportedException {
212
      Coercions other = (Coercions) super.clone();
213
      other.coercions = new ArrayList<>();
214
      other.coercions.addAll(this.coercions);
215
      return other;
216
    }
217

    
218
    @Override
219
    public Object coerce(Object value) throws CoercionException {
220
      return coerce(value, null);
221
    }
222

    
223
    @Override
224
    @SuppressWarnings("UnusedAssignment")
225
    public Object coerce(Object value, CoercionContext context) throws CoercionException {
226
      for (int i = this.coercions.size(); --i >= 0;) {
227
        Coercion coercion = this.coercions.get(i);
228
        try {
229
          return coercion.coerce(value, context);
230
        } catch (CoercionException e) {
231
          // Do nothing, go to next coercion
232
          coercion = null; // To allow set break point
233
        }
234
      }
235
      throw new CoercionException();
236
    }
237

    
238
  }
239

    
240
}