KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > query > lib > BasicPropagatedField


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23
24 package org.objectweb.medor.query.lib;
25
26 import org.objectweb.jorm.type.api.PType;
27 import org.objectweb.medor.api.Field;
28 import org.objectweb.medor.api.MedorException;
29 import org.objectweb.medor.query.api.PropagatedField;
30 import org.objectweb.medor.query.api.QueryTree;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36
37 /**
38  *
39  * @author Sebastien Chassande-Barrioz
40  */

41 public class BasicPropagatedField extends BasicQueryTreeField
42     implements PropagatedField {
43
44     protected HashMap JavaDoc ancestors = null;
45     protected int index = -1;
46
47     public BasicPropagatedField() {
48     }
49
50     public BasicPropagatedField(String JavaDoc name,
51                                 PType type,
52                                 QueryTree qt,
53                                 Field[] _ancestors) throws MedorException {
54         super(name, type, qt);
55         if (_ancestors != null && _ancestors.length > 0) {
56             ancestors = new HashMap JavaDoc(_ancestors.length);
57             for (int i = 0; i < _ancestors.length; i++) {
58                 Field[] el = (Field[]) ancestors.get(_ancestors[i].getName());
59                 if (el == null) {
60                     el = new Field[1];
61                     el[0] = _ancestors[i];
62                     ancestors.put(_ancestors[i].getName(), el);
63                 } else {
64                     Field[] neo = new Field[el.length + 1];
65                     System.arraycopy(el, 0, neo, 0, el.length);
66                     neo[el.length] = _ancestors[i];
67                     ancestors.put(_ancestors[i].getName(), neo);
68                 }
69             }
70         }
71     }
72
73     public Object JavaDoc clone(Object JavaDoc clone,
74                         Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
75         clone = super.clone(clone, obj2clone);
76         ((BasicPropagatedField) clone).index = index;
77         if (ancestors != null) {
78             BasicPropagatedField bpf = (BasicPropagatedField) clone;
79             bpf.ancestors = new HashMap JavaDoc(ancestors.size());
80             for(Iterator JavaDoc it = bpf.ancestors.entrySet().iterator(); it.hasNext();) {
81                 Map.Entry JavaDoc me = (Map.Entry JavaDoc) it.next();
82                 Field[] as = (Field[]) me.getValue();
83                 if (as == null) {
84                     me.setValue(null);
85                 } else {
86                     Field[] newAncestors = new Field[as.length];
87                     for(int i=0; i<as.length; i++) {
88                         newAncestors[i] = (Field) getClone(as[i], obj2clone);
89                     }
90                     me.setValue(newAncestors);
91                 }
92             }
93         }
94
95         return clone;
96     }
97
98     // IMPLEMENTATION OF THE PropagatedField INTERFACE //
99
//-------------------------------------------------//
100
public int getIndex() {
101         return index;
102     }
103
104     public void setIndex(int idx) {
105         index = idx;
106     }
107
108     public Field[] getPreviousFields() {
109         return (Field[]) (ancestors == null
110                 ? new Field[0]
111                 : getPrevious().toArray(new Field[0]));
112     }
113
114     public Field[] getPreviousFields(String JavaDoc oldFieldName) {
115         return (Field[]) (ancestors == null
116                 ? new Field[0]
117                 : ancestors.get(oldFieldName));
118     }
119
120     public Field[] getOriginFields() {
121         if (ancestors == null)
122             return new Field[0];
123
124         return (Field[]) getOriginFields(getPrevious(), new ArrayList JavaDoc())
125                 .toArray(new Field[0]);
126     }
127
128     protected ArrayList JavaDoc getOriginFields(ArrayList JavaDoc src, ArrayList JavaDoc dst) {
129         ArrayList JavaDoc res = dst;
130         for (int i = 0; i < src.size(); i++) {
131             Field f = (Field) src.get(i);
132             if (f instanceof BasicPropagatedField) {
133                 res = ((BasicPropagatedField) f).getOriginFields(
134                         ((BasicPropagatedField) f).getPrevious(), res);
135             } else if (f instanceof BasicCalculatedField) {
136                 res = ((BasicCalculatedField) f).getOriginUsedFields(
137                         ((BasicCalculatedField) f).getUsed(), res);
138             } else {
139                 res.add(f);
140             }
141         }
142         return res;
143     }
144
145     protected ArrayList JavaDoc getPrevious() {
146         ArrayList JavaDoc res = new ArrayList JavaDoc(ancestors.size());
147         Iterator JavaDoc it = ancestors.values().iterator();
148         while (it.hasNext()) {
149             Field[] current = (Field[]) it.next();
150             for (int i = 0; i < current.length; i++) {
151                 res.add(current[i]);
152             }
153         }
154         return res;
155     }
156
157     public short getNullStatus() {
158         // TO DO
159
return 99;
160     }
161
162     public void replacePreviousField(Field[] _ancestors) {
163         ancestors.clear();
164         for (int i = 0; i < _ancestors.length; i++) {
165             Field[] el = (Field[]) ancestors.get(_ancestors[i].getName());
166             if (el == null) {
167                 el = new Field[1];
168                 el[0] = _ancestors[i];
169                 ancestors.put(_ancestors[i].getName(), el);
170             } else {
171                 Field[] neo = new Field[el.length + 1];
172                 System.arraycopy(el, 0, neo, 0, el.length);
173                 neo[el.length] = _ancestors[i];
174                 ancestors.put(_ancestors[i].getName(), neo);
175             }
176         }
177     }
178 }
179
Popular Tags