Frames | No Frames |
1: /* NamingMap.java -- 2: Copyright (C) 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 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) 9: any later version. 10: 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. 15: 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 19: 02110-1301 USA. 20: 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 24: combination. 25: 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. */ 37: 38: 39: package gnu.CORBA.NamingService; 40: 41: import org.omg.CosNaming.NameComponent; 42: import org.omg.CosNaming.NamingContextPackage.AlreadyBound; 43: import org.omg.CosNaming.NamingContextPackage.InvalidName; 44: 45: import java.util.Iterator; 46: import java.util.Map; 47: import java.util.Set; 48: import java.util.TreeMap; 49: 50: /** 51: * The Naming Map maps the single names components into associated objects or 52: * naming contexts. 53: * 54: * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) 55: */ 56: public class NamingMap 57: { 58: /** 59: * The actual map. 60: */ 61: private final TreeMap map; 62: 63: /** 64: * Creates an instance of the naming map, intialising the comparator 65: * to the {@link cmpNameComparator}. 66: */ 67: public NamingMap() 68: { 69: map = new TreeMap(NameComponentComparator.singleton); 70: } 71: 72: /** 73: * Put the given CORBA 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. 77: * 78: * @param name the name 79: * @param object the object 80: */ 81: public void bind(NameComponent name, org.omg.CORBA.Object object) 82: throws AlreadyBound, InvalidName 83: { 84: if (containsKey(name)) 85: { 86: Object x = get(name); 87: 88: // Do not throw an exception if the same object is named by 89: // the same name. 90: if (x.equals(object)) 91: throw new AlreadyBound("The name is in use for another object"); 92: } 93: else 94: { 95: if (containsValue(object)) 96: throw new AlreadyBound("Tha object has another name"); 97: } 98: } 99: 100: /** 101: * Checks if this map contains the definition of the given name. 102: * 103: * @param key the name to check. 104: */ 105: public boolean containsKey(NameComponent key) 106: { 107: return map.containsKey(key); 108: } 109: 110: /** 111: * Checks if this map contains the definition of the given object. 112: * 113: * @param object the object to check. 114: */ 115: public boolean containsValue(org.omg.CORBA.Object object) 116: { 117: return map.containsValue(object); 118: } 119: 120: /** 121: * Returns the map entry set. 122: * 123: * @return the map entry set, containing the instances of the 124: * Map.Entry. 125: */ 126: public Set entries() 127: { 128: return map.entrySet(); 129: } 130: 131: /** 132: * Get the CORBA object, associated with the given name. 133: * 134: * @param name the name. 135: * 136: * @return the associated object, null if none. 137: */ 138: public org.omg.CORBA.Object get(NameComponent name) 139: { 140: return (org.omg.CORBA.Object) map.get(name); 141: } 142: 143: /** 144: * Put the given CORBA object, specifying the given name as a key. 145: * Remove all pre - existing mappings for the given name and object. 146: * 147: * @param name the name. 148: * @param object 149: */ 150: public void rebind(NameComponent name, org.omg.CORBA.Object object) 151: throws InvalidName 152: { 153: // Remove the existing mapping for the given name, if present. 154: remove(name); 155: 156: Iterator iter = entries().iterator(); 157: Map.Entry item; 158: 159: // Remove the existing mapping for the given object, if present. 160: while (iter.hasNext()) 161: { 162: item = (Map.Entry) iter.next(); 163: if (item.getValue().equals(object)) 164: iter.remove(); 165: } 166: 167: map.put(name, object); 168: } 169: 170: /** 171: * Removes the given name, if present. 172: * 173: * @param name a name to remove. 174: */ 175: public void remove(NameComponent name) 176: { 177: map.remove(name); 178: } 179: 180: /** 181: * Get the size of the map. 182: */ 183: public int size() 184: { 185: return map.size(); 186: } 187: }