Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / dynobject / impl / ComposedDynClass.java @ 24616

History | View | Annotate | Download (3.44 KB)

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

    
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.Collection;
6
import java.util.Collections;
7
import java.util.HashSet;
8
import java.util.Iterator;
9
import java.util.LinkedHashSet;
10
import java.util.List;
11
import java.util.Set;
12

    
13
import org.gvsig.tools.dynobject.DynAttributeDescriptor;
14
import org.gvsig.tools.dynobject.DynClass;
15
import org.gvsig.tools.dynobject.DynObject;
16

    
17
public class ComposedDynClass implements DynClass {
18

    
19
        private DefaultDynObjectManager manager;
20
        private List clases;
21

    
22
        public ComposedDynClass(DefaultDynObjectManager manager,
23
                        Collection dynClases) {
24
                this.manager = manager;
25
                this.clases = new ArrayList();
26
                this.clases.addAll(dynClases);
27
        }
28

    
29
        private DynClass getBaseClass() {
30
                return (DynClass) this.clases.get(0);
31
        }
32

    
33
        public List getBaseClases() {
34
                return Collections.unmodifiableList(this.clases);
35
        }
36

    
37
        public Collection getBaseClassNames() {
38
                List names = new ArrayList();
39
                Iterator it = this.clases.iterator();
40
                while (it.hasNext()) {
41
                        names.add(((DynClass) it.next()).getName());
42
                }
43
                return names;
44
        }
45

    
46
        public int getAttributeCount() {
47
                int count = 0;
48
                Iterator it = this.clases.iterator();
49
                while (it.hasNext()) {
50
                        count += ((DynClass) it.next()).getAttributeCount();
51
                }
52
                return count;
53
        }
54

    
55
        public DynAttributeDescriptor getAttributeDescriptor(String name) {
56
                Iterator it = this.clases.iterator();
57
                while (it.hasNext()) {
58
                        DynClass dynClass = (DynClass) it.next();
59
                        DynAttributeDescriptor attr = dynClass.getAttributeDescriptor(name);
60
                        if (attr != null) {
61
                                return attr;
62
                        }
63
                }
64
                return null;
65
        }
66

    
67
        public List getAttributeNames() {
68
                if (this.clases.size() == 1) {
69
                        return this.getBaseClass().getAttributeNames();
70
                }
71
                String[] names = (String[]) this.getAttributeNamesSet().toArray();
72
                Arrays.sort(names);
73
                return Collections.unmodifiableList(Arrays.asList(names));
74
        }
75

    
76
        private Set getAttributeNamesSet() {
77
                Set names = new HashSet();
78

    
79
                Iterator it = this.clases.iterator();
80
                while (it.hasNext()) {
81
                        DefaultDynClass dynClass = (DefaultDynClass) it.next();
82
                        names.addAll(dynClass.getAttributes().keySet());
83
                }
84
                return names;
85
        }
86

    
87
        public String getDescription() {
88
                return this.getBaseClass().getDescription();
89
        }
90

    
91
        public String getName() {
92
                return this.getBaseClass().getName();
93
        }
94

    
95
        public Iterator iterator() {
96
                class AttributesIterator implements Iterator {
97
                        Iterator it;
98

    
99
                        AttributesIterator(Set attributeNames) {
100
                                this.it = attributeNames.iterator();
101
                        }
102

    
103
                        public boolean hasNext() {
104
                                return this.it.hasNext();
105
                        }
106

    
107
                        public Object next() {
108
                                return getAttributeDescriptor((String) it.next());
109
                        }
110

    
111
                        public void remove() {
112
                                throw new UnsupportedOperationException();
113
                        }
114

    
115
                }
116
                if (this.clases.size() == 1) {
117
                        return this.getBaseClassNames().iterator();
118
                }
119
                return new AttributesIterator(this.getAttributeNamesSet());
120
        }
121

    
122
        public DynObject newInstance() {
123
                return this.manager.createDynObject(this);
124
        }
125

    
126
        public void extendsDynClass(String dynClassName) {
127
                DynClass dynClass = manager.get(dynClassName);
128
                this.extendsDynClass(dynClass);
129
        }
130

    
131
        public void extendsDynClass(DynClass dynClass) {
132
                Set clases = new LinkedHashSet(this.clases);
133
                manager.buildBaseClasesList(clases, dynClass);
134
                this.clases = new ArrayList();
135
                this.clases.addAll(clases);
136
        }
137

    
138
        public DynAttributeDescriptor add(String name) {
139
                return null; // FIXME: Basta con retornar null ?
140
        }
141

    
142
        public void remove(String name) {
143
                throw new UnsupportedOperationException();
144
        }
145

    
146

    
147
}