2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.CORBA
.NamingService
;
41 import org
.omg
.CosNaming
.NameComponent
;
42 import org
.omg
.CosNaming
.NamingContextPackage
.AlreadyBound
;
43 import org
.omg
.CosNaming
.NamingContextPackage
.InvalidName
;
45 import java
.util
.Iterator
;
48 import java
.util
.TreeMap
;
51 * The Naming Map maps the single names components into associated objects or
54 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
56 public class NamingMap
61 protected final TreeMap map
;
64 * Creates an instance of the naming map, intialising the comparator
65 * to the {@link NameComponentComparator}.
69 map
= new TreeMap(NameComponentComparator
.singleton
);
73 * Put the given GIOP object, specifying the given name as a key.
74 * If the entry with the given name already exists, or if the given
75 * object is already mapped under another name, the
76 * {@link AlreadyBound} exception will be thrown.
78 * @param name the name
79 * @param object the object
81 public void bind(NameComponent name
, org
.omg
.CORBA
.Object object
)
82 throws AlreadyBound
, InvalidName
84 if (containsKey(name
))
88 // Do not throw an exception if the same object is named by
91 throw new AlreadyBound("The name is in use for another object");
95 if (containsValue(object
))
96 throw new AlreadyBound("The object has another name");
99 // There are no restrictions in binding the object.
100 rebind(name
, object
);
104 * Checks if this map contains the definition of the given name.
106 * @param key the name to check.
108 public boolean containsKey(NameComponent key
)
110 return map
.containsKey(key
);
114 * Checks if this map contains the definition of the given object.
116 * @param object the object to check.
118 public boolean containsValue(org
.omg
.CORBA
.Object object
)
120 return map
.containsValue(object
);
124 * Returns the map entry set.
126 * @return the map entry set, containing the instances of the
131 return map
.entrySet();
135 * Get the CORBA object, associated with the given name.
137 * @param name the name.
139 * @return the associated object, null if none.
141 public org
.omg
.CORBA
.Object
get(NameComponent name
)
143 return (org
.omg
.CORBA
.Object
) map
.get(name
);
147 * Put the given GIOP object, specifying the given name as a key.
148 * Remove all pre - existing mappings for the given name and object.
150 * @param name the name.
153 public void rebind(NameComponent name
, org
.omg
.CORBA
.Object object
)
156 // Remove the existing mapping for the given name, if present.
159 Iterator iter
= entries().iterator();
162 // Remove the existing mapping for the given object, if present.
163 while (iter
.hasNext())
165 item
= (Map
.Entry
) iter
.next();
166 if (item
.getValue().equals(object
))
170 map
.put(name
, object
);
174 * Removes the given name, if present.
176 * @param name a name to remove.
178 public void remove(NameComponent name
)
184 * Get the size of the map.