1 56 package org.objectstyle.cayenne.util; 57 58 import java.util.Comparator ; 59 import java.util.Iterator ; 60 import java.util.Map ; 61 import java.util.SortedMap ; 62 63 import org.apache.commons.collections.FastTreeMap; 64 65 79 public class CayenneMap extends FastTreeMap { 80 protected Object parent; 81 82 85 public CayenneMap(Object parent) { 86 this.parent = parent; 87 } 88 89 93 public CayenneMap(Object parent, Comparator c) { 94 super(c); 95 this.parent = parent; 96 } 97 98 102 public CayenneMap(Object parent, Map m) { 103 this.parent = parent; 105 putAll(m); 106 } 107 108 112 public CayenneMap(Object parent, SortedMap m) { 113 this.parent = parent; 115 putAll(m); 116 } 117 118 124 public Object put(Object key, Object value) { 125 126 if (containsKey(key) && get(key) != value) { 127 StringBuffer message = new StringBuffer (); 129 message.append("Attempt to insert duplicate key. [key '"); 130 message.append(key); 131 message.append("'"); 132 133 if (parent instanceof CayenneMapEntry) { 134 message.append(", parent '").append( 135 ((CayenneMapEntry) parent).getName()).append( 136 "'"); 137 } 138 139 if (value instanceof CayenneMapEntry) { 140 message.append(", child '").append( 141 ((CayenneMapEntry) value).getName()).append( 142 "'"); 143 } 144 message.append("]"); 145 146 throw new IllegalArgumentException (message.toString()); 147 } 148 149 if (value instanceof CayenneMapEntry) { 150 ((CayenneMapEntry) value).setParent(parent); 151 } 152 153 super.put(key, value); 154 return null; 155 } 156 157 160 public void putAll(Map t) { 161 Iterator it = t.keySet().iterator(); 162 while (it.hasNext()) { 163 Object key = it.next(); 164 put(key, t.get(key)); 165 } 166 } 167 168 172 public Object getParent() { 173 return parent; 174 } 175 176 public void setParent(Object mapParent) { 177 this.parent = mapParent; 178 } 179 } 180 | Popular Tags |