Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / DefaultDynClassName.java @ 391

History | View | Annotate | Download (3.22 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
package org.gvsig.tools.dynobject.impl;
23

    
24
import org.gvsig.tools.dynobject.DynClassName;
25

    
26
/**
27
 * Default {@link DynClassName} implementation.
28
 * 
29
 * @author gvSIG Team
30
 * @version $Id$
31
 */
32
public class DefaultDynClassName implements DynClassName {
33
        private static final String SEPARATOR = ":";
34
        
35
        private String name;
36
        private String namespace;
37
        
38
        public DefaultDynClassName(String namespace, String name) {
39
                setNamespace(namespace);
40
                setName(name);
41
        }
42
        
43
        public DefaultDynClassName(String fullname) {
44
                int x=fullname.indexOf(SEPARATOR);
45
                int y=fullname.lastIndexOf(SEPARATOR);
46
                if( x>-1 ) {
47
                        // Separator should only be present once
48
                        if (x==y){
49
                                setNamespace(fullname.substring(0, x));
50
                                setName(fullname.substring(x+1));                                
51
                        }else{
52
                                throw new IllegalArgumentException("Fullname '" + fullname + "' not allowed. Only one '" + SEPARATOR + "' is possible.");                                
53
                        }
54
                } else {
55
                        this.namespace = null;
56
                        setName(fullname);
57
                }
58
        }
59
        
60
        public DefaultDynClassName(String[] name) {
61
                setNamespace(name[0]);
62
                setName(name[1]);
63
        }
64
        
65
        public String getName() {
66
                return name;
67
        }
68
        
69
        public String getNamespace() {
70
                return namespace;
71
        }
72
        
73
        public String getFullName() {
74
            if( namespace == null ) {
75
                    return name;
76
            }
77
            return namespace + SEPARATOR + name;
78
        }
79

    
80
        public void setNamespace(String namespace) {
81
                if(namespace!=null){
82
                        if (!namespace.contains(SEPARATOR)){
83
                                this.namespace = namespace;
84
                        }else{
85
                                throw new IllegalArgumentException("Namespace '" + namespace + "' not allowed. Character " + SEPARATOR + " forbidden whithin a dynClass' namespace'");
86
                        }
87
                }else{
88
                        this.namespace = null;
89
                }
90
        }
91

    
92
        public void setName(String name) {
93
                if(name!=null){
94
                        if (!name.contains(SEPARATOR)){
95
                                this.name = name;
96
                        }else{
97
                                throw new IllegalArgumentException("Name '" + name + "' not allowed. Character '" + SEPARATOR + "' is not valid within a dynClass' name.");
98
                        }
99
                }else{
100
                        throw new IllegalArgumentException("Null value unsupported for a dynClass' name.");
101
                }
102
        }
103

    
104
        public int hashCode() {
105
                return getFullName().hashCode();
106
        }
107

    
108
        public boolean equals(Object obj) {
109
                if (this == obj) {
110
                        return true;
111
                }
112
                if (obj instanceof DynClassName) {
113
                        DynClassName obj2 = (DynClassName) obj;
114
                        return getFullName().equalsIgnoreCase(obj2.getFullName());
115
                }
116
                return false;
117
        }
118
        
119
        public String toString() {
120
                return this.getFullName();
121
        }
122
}