1 19 20 package org.apache.cayenne.util; 21 22 import java.util.Comparator ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.SortedMap ; 26 27 import org.apache.commons.collections.FastTreeMap; 28 29 41 public class CayenneMap extends FastTreeMap { 45 46 protected Object parent; 47 48 51 public CayenneMap(Object parent) { 52 this.parent = parent; 53 } 54 55 60 public CayenneMap(Object parent, Comparator c) { 61 super(c); 62 this.parent = parent; 63 } 64 65 70 public CayenneMap(Object parent, Map m) { 71 this.parent = parent; 73 putAll(m); 74 } 75 76 81 public CayenneMap(Object parent, SortedMap m) { 82 this.parent = parent; 84 putAll(m); 85 } 86 87 93 public Object put(Object key, Object value) { 94 95 if (containsKey(key) && get(key) != value) { 96 StringBuffer message = new StringBuffer (); 98 message.append("Attempt to insert duplicate key. [key '"); 99 message.append(key); 100 message.append("'"); 101 102 if (parent instanceof CayenneMapEntry) { 103 message 104 .append(", parent '") 105 .append(((CayenneMapEntry) parent).getName()) 106 .append("'"); 107 } 108 109 if (value instanceof CayenneMapEntry) { 110 message 111 .append(", child '") 112 .append(((CayenneMapEntry) value).getName()) 113 .append("'"); 114 } 115 message.append("]"); 116 117 throw new IllegalArgumentException (message.toString()); 118 } 119 120 if (value instanceof CayenneMapEntry) { 121 ((CayenneMapEntry) value).setParent(parent); 122 } 123 124 super.put(key, value); 125 return null; 126 } 127 128 131 public void putAll(Map t) { 132 Iterator it = t.entrySet().iterator(); 133 while (it.hasNext()) { 134 Map.Entry entry = (Map.Entry ) it.next(); 135 put(entry.getKey(), entry.getValue()); 136 } 137 } 138 139 144 public Object getParent() { 145 return parent; 146 } 147 148 public void setParent(Object mapParent) { 149 this.parent = mapParent; 150 } 151 } 152 | Popular Tags |