1 package org.sapia.archie.impl; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 import java.util.Map ; 6 import java.util.Properties ; 7 8 import org.sapia.archie.NamePart; 9 import org.sapia.archie.NodeFactory; 10 import org.sapia.archie.ProcessingException; 11 12 20 public class AttributeNode extends MultiValueNode{ 21 22 protected AttributeNode(Map children, Map values, NodeFactory fac) throws ProcessingException { 23 super(new AttributeNameParser(), children, values, fac); 24 } 25 26 29 public final Object getValue(NamePart name) { 30 List values = (List ) _valueLists.get(name); 31 32 if (values == null) { 33 return null; 34 } 35 36 List matching = selectMatchingOffers(name, values); 38 39 if(matching.size() == 0){ 41 return null; 42 } 43 44 Offer selected = (Offer)onSelect(matching); 46 47 if(!isValid(selected)){ 50 Offer other; 51 for(int i = 0; i < values.size(); i++){ 52 other = (Offer)values.get(i); 53 if(other.getId().equals(selected.getId())){ 54 values.remove(i); 55 break; 56 } 57 } 58 return getValue(name); 59 } 60 61 Object toReturn = null; 63 if(selected != null){ 64 toReturn = onRead(name, selected.getObject()); 65 selected.select(); 66 } 67 return toReturn; 68 } 69 70 73 public final boolean putValue(NamePart name, Object value, boolean overwrite) { 74 List values = (List ) _valueLists.get(name); 75 76 if (values == null) { 77 values = new ArrayList (); 78 _valueLists.put(name, values); 79 } 80 81 if (!overwrite && (values.size() != 0)) { 82 return false; 83 } 84 85 Object toBind = onWrite(name, value); 86 if(toBind != null){ 87 if(name instanceof AttributeNamePart){ 88 values.add(new Offer(((AttributeNamePart)name).getAttributes(), toBind)); 89 } 90 else{ 91 values.add(new Offer(new Properties (), toBind)); 92 } 93 } 94 return true; 95 } 96 97 100 public final Object removeValue(NamePart name) { 101 Object val = getValue(name); 102 List values = (List ) _valueLists.get(name); 103 if (values == null) { 104 return val; 105 } 106 List matching = selectMatchingOffers(name, values); 108 109 if(matching.size() == 0){ 111 return val; 112 } 113 for(int i = 0; i < matching.size(); i++){ 114 removeOffer((Offer)matching.get(i), values); 115 } 116 if(values.size() == 0){ 117 _valueLists.remove(name); 118 } 119 return val; 120 } 121 122 127 protected final Object onSelect(List offers){ 128 return onSelectOffer(offers); 129 } 130 131 137 protected Offer onSelectOffer(List offers){ 138 return SelectionHelper.selectLeastUsed(offers); 139 } 140 141 150 protected boolean isValid(Offer offer){ 151 return true; 152 } 153 154 private void removeOffer(Offer offer , List offers){ 155 for(int i = 0; i < offers.size(); i++){ 156 if(((Offer)offers.get(i)).getId().equals(offer.getId())){ 157 offers.remove(i); 158 i--; 159 } 160 } 161 } 162 163 static List selectMatchingOffers(NamePart name, List offers){ 164 List matching = new ArrayList (); 165 Offer offer; 166 for(int i = 0; i < offers.size(); i++){ 167 offer = (Offer)offers.get(i); 168 if(name instanceof AttributeNamePart){ 169 if(offer.matches(((AttributeNamePart)name).getAttributes())){ 170 matching.add(offer); 171 } 172 } 173 else{ 174 matching.add(offer); 175 } 176 } 177 return matching; 178 } 179 180 181 182 } 183 | Popular Tags |