Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / ValueType.java @ 1118

History | View | Annotate | Download (4.02 KB)

1
package org.gvsig.tools.dynobject.impl;
2

    
3
import org.gvsig.tools.ToolsLocator;
4
import org.gvsig.tools.dataTypes.DataType;
5
import org.gvsig.tools.dataTypes.DataTypes;
6
import org.gvsig.tools.dynobject.DynStruct;
7

    
8
public class ValueType {
9

    
10
    DataType type;
11
    Class theClass;
12
    DynStruct theDynClass;
13

    
14
    public ValueType(int type) {
15
        this.setType(type);
16
    }
17

    
18
    public void setType(int type) {
19
        if ( type == DataTypes.UNKNOWN ) {
20
            this.type = null;
21
        } else {
22
            DataType t = ToolsLocator.getDataTypesManager().get(type);
23
            setType(t);
24
        }
25
    }
26

    
27
    public void setType(DataType type) {
28
        if ( this.theDynClass != null ) {
29
            if ( type.getType() == DataTypes.DYNOBJECT ) {
30
                this.type = type;
31
            } else if ( type.getType() == DataTypes.UNKNOWN ) {
32
                this.type = ToolsLocator.getDataTypesManager().get(DataTypes.DYNOBJECT);
33
            } else {
34
                throw new IllegalArgumentException("Can't set type to non DYNOBJECT when have a dynclass assigned.");
35
            }
36
        } else {
37
            this.type = type;
38
        }
39
    }
40

    
41
    public String getSubtype() {
42
        if ( this.type == null ) {
43
            return null;
44
        }
45
        return this.type.getSubtype();
46
    }
47

    
48
    public int getType() {
49
        if( this.type == null ) {
50
            return DataTypes.UNKNOWN;
51
        }
52
        return type.getType();
53
    }
54

    
55
    public DataType getDataType() {
56
        return type;
57
    }
58

    
59
    public void setClassOfValue(Class theClass) {
60
        if ( this.type == null || type.getType() == DataTypes.UNKNOWN ) {
61
            this.type = ToolsLocator.getDataTypesManager().get(DataTypes.OBJECT);
62
        }
63
        if ( type.getType() == DataTypes.DYNOBJECT && theClass!=null) {
64
            throw new IllegalArgumentException("Can't assign a Class '"+theClass.getCanonicalName()+"' when type is '"+type.getName()+"'.");
65
        }
66
        this.theClass = theClass;
67
    }
68

    
69
    public void setClassOfValue(DynStruct dynStrct) {
70
        if ( this.type == null || type.getType() == DataTypes.UNKNOWN ) {
71
            this.type = ToolsLocator.getDataTypesManager().get(DataTypes.DYNOBJECT);
72
        }
73
        if ( type.getType() != DataTypes.DYNOBJECT && dynStrct!=null) {
74
            throw new IllegalArgumentException("Can't assign a DynClass '"+dynStrct.getFullName()+"' when type is '"+type.getName()+"'.");
75
        }
76
        this.theDynClass = dynStrct;
77
    }
78

    
79
    public void setClassOfValue(String theClassNameOfValue) {
80
        if ( this.type == null ) {
81
            throw new IllegalStateException("Need to set type before assign classOfValue by name");
82
        }
83
        if ( type.getType() == DataTypes.DYNOBJECT ) {
84
            this.theDynClass = (DynStruct) ToolsLocator.getDynObjectManager().createDynClassName(theClassNameOfValue);
85
            if ( this.theDynClass == null ) {
86
                throw new IllegalArgumentException("DynClass of type '" + theClassNameOfValue + "' not found.");
87
            }
88
        } else {
89
            try {
90
                Class theClass = Class.forName(theClassNameOfValue);
91
                this.theClass = theClass;
92
            } catch (ClassNotFoundException ex) {
93
                throw new IllegalArgumentException("The class '" + theClassNameOfValue + "' not exists.");
94
            }
95
        }
96
    }
97

    
98
    public String getClassNameOfValue() {
99
        if ( type == null ) {
100
            return null;
101
        }
102
        if ( type.getType() == DataTypes.DYNOBJECT ) {
103
            if ( this.theDynClass == null ) {
104
                return null;
105
            }
106
            return this.theDynClass.getFullName();
107
        }
108
        if ( this.theClass == null ) {
109
            return null;
110
        }
111
        return this.theClass.getName();
112
    }
113

    
114
    public DynStruct getDynClassOfValue() {
115
        return this.theDynClass;
116
    }
117

    
118
    public Class getClassOfValue() {
119
        return this.theClass;
120
    }
121

    
122
    public String toString() {
123
        if ( this.type == null ) {
124
            return "(null)";
125
        }
126
        return this.type.toString();
127
    }
128

    
129
}