1 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 ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 37 41 public class BasicPropagatedField extends BasicQueryTreeField 42 implements PropagatedField { 43 44 protected HashMap ancestors = null; 45 protected int index = -1; 46 47 public BasicPropagatedField() { 48 } 49 50 public BasicPropagatedField(String 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 (_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 clone(Object clone, 74 Map obj2clone) throws CloneNotSupportedException { 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 (ancestors.size()); 80 for(Iterator it = bpf.ancestors.entrySet().iterator(); it.hasNext();) { 81 Map.Entry me = (Map.Entry ) 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 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 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 ()) 125 .toArray(new Field[0]); 126 } 127 128 protected ArrayList getOriginFields(ArrayList src, ArrayList dst) { 129 ArrayList 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 getPrevious() { 146 ArrayList res = new ArrayList (ancestors.size()); 147 Iterator 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 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 |