KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > util > CayenneMap


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.util;
21
22 import java.util.Comparator JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.SortedMap JavaDoc;
26
27 import org.apache.commons.collections.FastTreeMap;
28
29 /**
30  * A <code>CayenneMap</code> is a specialized double-linked sorted map class. Attempts
31  * to add objects using an already existing keys will result in IllegalArgumentExceptions.
32  * Any added entries that implement CayenneMapEntry interface will have their parent set
33  * to the parent of this map.
34  * <p>
35  * CayenneMap is not subclassed directly, but is rather used as an instance variable
36  * within another class. Enclosing instance would set itself as a parent of this map.
37  * </p>
38  *
39  * @author Andrus Adamchik
40  */

41 // WARNING: CayenneMap is not serializable via Hessian serialization mechanism used by
42
// CayenneConnector implementation.
43
// TODO: deprecate this ugly map. it is only used in Configuration
44
public class CayenneMap extends FastTreeMap {
45
46     protected Object JavaDoc parent;
47
48     /**
49      * Constructor for CayenneMap.
50      */

51     public CayenneMap(Object JavaDoc parent) {
52         this.parent = parent;
53     }
54
55     /**
56      * Constructor for CayenneMap.
57      *
58      * @param c
59      */

60     public CayenneMap(Object JavaDoc parent, Comparator JavaDoc c) {
61         super(c);
62         this.parent = parent;
63     }
64
65     /**
66      * Constructor for CayenneMap.
67      *
68      * @param m
69      */

70     public CayenneMap(Object JavaDoc parent, Map JavaDoc m) {
71         // !IMPORTANT - set parent before populating the map
72
this.parent = parent;
73         putAll(m);
74     }
75
76     /**
77      * Constructor for CayenneMap.
78      *
79      * @param m
80      */

81     public CayenneMap(Object JavaDoc parent, SortedMap JavaDoc m) {
82         // !IMPORTANT - set parent before populating the map
83
this.parent = parent;
84         putAll(m);
85     }
86
87     /**
88      * Maps specified key-value pair. If value is a CayenneMapEntry, sets its parent to
89      * this map.
90      *
91      * @see java.util.Map#put(Object, Object)
92      */

93     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
94
95         if (containsKey(key) && get(key) != value) {
96             // build descriptive failure message
97
StringBuffer JavaDoc message = new StringBuffer JavaDoc();
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 JavaDoc(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     /**
129      * @see java.util.Map#putAll(Map)
130      */

131     public void putAll(Map JavaDoc t) {
132         Iterator JavaDoc it = t.entrySet().iterator();
133         while (it.hasNext()) {
134             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
135             put(entry.getKey(), entry.getValue());
136         }
137     }
138
139     /**
140      * Returns the parent.
141      *
142      * @return Object
143      */

144     public Object JavaDoc getParent() {
145         return parent;
146     }
147
148     public void setParent(Object JavaDoc mapParent) {
149         this.parent = mapParent;
150     }
151 }
152
Popular Tags