Revision 25791 branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/dynobject/impl/DefaultDynClass.java

View differences:

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

  
3
import java.util.*;
3
import java.util.Collection;
4
import java.util.HashMap;
5
import java.util.Iterator;
6
import java.util.LinkedHashMap;
7
import java.util.LinkedHashSet;
8
import java.util.Map;
9
import java.util.Set;
4 10

  
5
import org.gvsig.tools.dynobject.*;
11
import org.gvsig.tools.dynobject.DynClass;
12
import org.gvsig.tools.dynobject.DynField;
13
import org.gvsig.tools.dynobject.DynMethod;
14
import org.gvsig.tools.dynobject.DynObject;
15
import org.gvsig.tools.dynobject.DynObjectManager;
16
import org.gvsig.tools.dynobject.exception.DynMethodException;
6 17

  
7 18
public class DefaultDynClass implements DynClass {
8 19

  
......
22 33
		}
23 34
	}
24 35

  
25
    private DynObjectManager manager;
36
	public class MethodAndIndex {
37
		DynMethod method;
38
		int index;
26 39

  
40
		MethodAndIndex(DynMethod method, int index) {
41
			this.method = method;
42
			this.index = index;
43
		}
44
		public int getIndex() {
45
			return this.index;
46
		}
47
		public DynMethod getDynMethod() {
48
			return this.method;
49
		}
50
	}
51

  
52
    DynObjectManager manager;
53

  
27 54
    private String name;
28 55
    private String description;
29 56

  
......
31 58

  
32 59
    private Map classes;
33 60
    private Map declaredFieldsMap;
61
    private Map declaredMethodsMap;
34 62

  
35 63
    // This attributes are calculated by consolide method
36 64
    private DefaultDynClass[] superClasses;
65
	private Map superClassesMap;
37 66
    private DynField[] declaredFields;
38 67
	private Map fieldsMap;
39 68
	private DynField[] fields;
40
	private Map superClassesMap;
69
    private DynMethod[] declaredMethods;
70
	private Map methodsMap;
71
	private DynMethod[] methods;
41 72

  
42 73

  
43 74
    public DefaultDynClass(DynObjectManager manager, String name, String description) {
44 75
    	this.isAnonymous = false;
45 76
    	this.classes = new LinkedHashMap();
46 77
    	this.declaredFieldsMap = new HashMap();
78
    	this.declaredMethodsMap = new HashMap();
47 79

  
48 80
    	this.forceConsolide();
49 81

  
......
75 107

  
76 108
    	// Actualize declaredFields array
77 109
    	this.declaredFields = (DynField[]) this.declaredFieldsMap.values()
78
				.toArray(new DynField[] {});
110
		.toArray(new DynField[] {});
79 111

  
112
    	// Actualize declaredMethods array
113
    	this.declaredMethods = (DynMethod[]) this.declaredMethodsMap.values()
114
		.toArray(new DynMethod[] {});
115

  
80 116
    	// Actualize fieldsMap
81 117
    	this.fieldsMap = new LinkedHashMap();
82 118
    	int index = 0;
......
93 129
        	}
94 130
    	}
95 131

  
96
    	// Actualize fields array
132
    	// Actualize methodsMap
133
    	this.methodsMap = new LinkedHashMap();
134
    	index = 0;
135
    	for( int i=0; i<this.declaredMethods.length; i++) {
136
    		this.methodsMap.put(this.declaredMethods[i].getName(), new MethodAndIndex(this.declaredMethods[i],index++));
137
    	}
138
    	for( int i=0; i<this.superClasses.length ; i++ ) {
139
        	Iterator it = this.superClasses[i].declaredMethodsMap.values().iterator();
140
        	while( it.hasNext() ) {
141
        		DynMethod method = (DynMethod) it.next();
142
	    		if( !this.methodsMap.containsKey(method.getName()) ) {
143
	    			this.methodsMap.put(method.getName(), new MethodAndIndex(method,index++));
144
	    		}
145
        	}
146
    	}
147

  
148

  
149
		// Actualize fields array
97 150
		this.fields = new DynField[ this.fieldsMap.size() ];
98 151
		int i=0;
99 152
		Iterator it = this.fieldsMap.values().iterator();
......
103 156
			fields[i++] = field;
104 157
		}
105 158

  
159
		// Actualize methods array
160
		this.methods = new DynMethod[ this.methodsMap.size() ];
161
		i=0;
162
		it = this.methodsMap.values().iterator();
163
		while( it.hasNext() ) {
164
		    MethodAndIndex mindex = (MethodAndIndex) it.next();
165
            DynMethod method = mindex.getDynMethod();
166
			methods[i++] = method;
167
		}
168

  
106 169
		// Actualize superClassesMap
107 170
		this.superClassesMap = new HashMap();
108 171
		for( i=0 ; i<this.superClasses.length ; i++) {
......
112 175

  
113 176
    private void forceConsolide() {
114 177
        this.superClasses = null;
178
        this.superClassesMap = null;
115 179
        this.declaredFields = null;
116 180
        this.fieldsMap = null;
117 181
        this.fields = null;
118
        this.superClassesMap = null;
182
        this.declaredMethods = null;
183
        this.methodsMap = null;
184
        this.methods = null;
119 185
    }
120 186

  
121 187
    private Set buildSuperDynClassSet() {
......
264 330
	public boolean isAnonymous() {
265 331
		return isAnonymous;
266 332
	}
267
	
333

  
268 334
	public int hashCode() {
269 335
        return name.hashCode();
270 336
    }
......
279 345
        return false;
280 346
    }
281 347

  
348
	public void addDynMethod(DynMethod dynMethod) {
349
		this.manager.registerDynMethod(this, dynMethod);
350
	}
351

  
352
	void addMethod(DynMethod dynMethod) {
353
		declaredMethodsMap.put(name, dynMethod);
354
		this.forceConsolide();
355
	}
356

  
357
	public DynMethod getDeclaredDynMethod(String name) {
358
		return (DynMethod)this.declaredMethodsMap.get(name);
359
	}
360

  
361
	public DynMethod[] getDeclaredDynMethods() {
362
		return this.declaredMethods;
363
	}
364

  
365
	public DynMethod getDynMethod(String name) throws DynMethodException {
366
    	if( this.methodsMap == null ) {
367
    		consolide();
368
    	}
369
    	MethodAndIndex mindex = (MethodAndIndex) methodsMap.get(name);
370
        return mindex == null ? null : mindex.getDynMethod();
371
	}
372

  
373
	public DynMethod getDynMethod(int code) throws DynMethodException {
374
		return this.manager.getDynMethod(code);
375
	}
376

  
377
	public DynMethod[] getDynMethods() {
378
    	if( this.methods == null ) {
379
    		consolide();
380
    	}
381
		return this.methods;
382
	}
383

  
384
	public void removeDynMethod(String name) {
385
		// TODO Auto-generated method stub
386

  
387
	}
388

  
282 389
}

Also available in: Unified diff