1 11 package org.eclipse.ui.internal.preferences; 12 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import org.eclipse.ui.internal.util.Util; 18 19 22 public class PropertyMapUnion implements IPropertyMap { 23 24 private Map values; 25 26 private final static class PropertyInfo { 27 Object value; 28 boolean commonAttribute; 29 30 PropertyInfo(Object value, boolean commonAttribute) { 31 this.value = value; 32 this.commonAttribute = commonAttribute; 33 } 34 } 35 36 39 public Set keySet() { 40 return values.keySet(); 41 } 42 43 46 public Object getValue(String propertyId, Class propertyType) { 47 PropertyInfo info = (PropertyInfo)values.get(propertyId); 48 49 if (info == null) { 50 return null; 51 } 52 53 Object value = info.value; 54 55 if (propertyType.isInstance(value)) { 56 return value; 57 } 58 59 return null; 60 } 61 62 65 public boolean isCommonProperty(String propertyId) { 66 PropertyInfo info = (PropertyInfo)values.get(propertyId); 67 68 if (info == null) { 69 return false; 70 } 71 72 return info.commonAttribute; 73 } 74 75 78 public boolean propertyExists(String propertyId) { 79 return values.get(propertyId) != null; 80 } 81 82 85 public void setValue(String propertyId, Object newValue) { 86 PropertyInfo info = new PropertyInfo(newValue, true); 87 88 values.put(propertyId, info); 89 } 90 91 public void addMap(IPropertyMap toAdd) { 92 Set keySet = toAdd.keySet(); 93 94 for (Iterator iter = keySet().iterator(); iter.hasNext();) { 96 String key = (String ) iter.next(); 97 98 PropertyInfo localInfo = (PropertyInfo)values.get(key); 99 if (localInfo != null) { 101 if (toAdd.propertyExists(key)) { 103 Object value = toAdd.getValue(key, Object .class); 105 106 if (!Util.equals(value, toAdd.getValue(key, Object .class))) { 107 localInfo.value = null; 109 } 110 111 localInfo.commonAttribute = localInfo.commonAttribute && toAdd.isCommonProperty(key); 114 } else { 115 localInfo.commonAttribute = false; 117 } 118 } 119 } 120 121 for (Iterator iter = keySet.iterator(); iter.hasNext();) { 123 String element = (String ) iter.next(); 124 125 PropertyInfo localInfo = (PropertyInfo)values.get(element); 126 if (localInfo == null) { 127 Object value = toAdd.getValue(element, Object .class); 128 129 boolean isCommon = toAdd.isCommonProperty(element); 130 131 localInfo = new PropertyInfo(value, isCommon); 132 values.put(element, localInfo); 133 } 134 } 135 } 136 137 public void removeValue(String propertyId) { 138 values.remove(propertyId); 139 } 140 } 141 | Popular Tags |