001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.vfs.metadata;
020:
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Set;
024:
025: /**
026: * The ResourceLookup class provides a store of objects which can be
027: * requested out by either their full path or their fully qualified name,
028: * i.e. their namespace#name.
029: *
030: * @author Matthew Large
031: * @version $Revision: 1.1 $
032: *
033: */
034: public class ResourceLookup {
035:
036: /**
037: * Map of fully qualified names to objects.
038: */
039: HashMap m_qnameLookup = null;
040:
041: /**
042: * Map of full paths to objects.
043: */
044: HashMap m_hrefLookup = null;
045:
046: /**
047: * Map of fully qualified names to full paths.
048: */
049: HashMap m_qname2href = null;
050:
051: /**
052: * Map of full paths to fully qualified names.
053: */
054: HashMap m_href2qname = null;
055:
056: /**
057: * Constructs a new ResoruceLookup
058: *
059: * @param nInitialSize Initial size
060: */
061: public ResourceLookup(int nInitialSize) {
062: super ();
063: m_qnameLookup = new HashMap(nInitialSize);
064: m_hrefLookup = new HashMap(nInitialSize);
065: m_qname2href = new HashMap(nInitialSize);
066: m_href2qname = new HashMap(nInitialSize);
067: }
068:
069: /**
070: * Adds a new object to the lookup.
071: *
072: * @param sQName Fully qualified name
073: * @param sHREF Full path
074: * @param obj Object
075: */
076: public void put(String sQName, String sHREF, Object obj) {
077: this .m_qnameLookup.put(sQName, obj);
078: this .m_hrefLookup.put(sHREF, obj);
079: this .m_qname2href.put(sQName, sHREF);
080: this .m_href2qname.put(sHREF, sQName);
081: }
082:
083: /**
084: * Returns a fully qualified name for a given full path.
085: *
086: * @param sHref Full path
087: * @return Fully qualified name or null if not found
088: */
089: public String getQName(String sHref) {
090: return (String) this .m_href2qname.get(sHref);
091: }
092:
093: /**
094: * Returns a full path for a given fully qualified name.
095: *
096: * @param sQName Fully qualified name
097: * @return Full path or null if not found
098: */
099: public String getHref(String sQName) {
100: return (String) this .m_qname2href.get(sQName);
101: }
102:
103: /**
104: * Returns an object for a given fully qualified name.
105: *
106: * @param sQName Fully qualified name
107: * @return Object or null if not found
108: */
109: public Object getByQName(String sQName) {
110: return this .m_qnameLookup.get(sQName);
111: }
112:
113: /**
114: * Returns an object for a given full path.
115: *
116: * @param sHREF Full path
117: * @return Object or null if not found
118: */
119: public Object getByHREF(String sHREF) {
120: return this .m_hrefLookup.get(sHREF);
121: }
122:
123: /**
124: * Removes an object for a given fully qualified name.
125: *
126: * @param sQName Fully qualified name
127: */
128: public void removeByQName(String sQName) {
129: Object obj = this .m_qnameLookup.get(sQName);
130: if (obj != null) {
131: this .m_qnameLookup.remove(obj);
132: this .m_hrefLookup.remove(obj);
133: }
134: }
135:
136: /**
137: * Remove an object for a given full path.
138: *
139: * @param sHREF Full path
140: */
141: public void removeByHREF(String sHREF) {
142: Object obj = this .m_hrefLookup.get(sHREF);
143: if (obj != null) {
144: Object objcheck = this .m_hrefLookup.remove(sHREF);
145: if (objcheck == null) {
146: System.err.println("Object removal for href[" + sHREF
147: + "] is null!!! " + this .m_hrefLookup.keySet());
148: }
149: objcheck = this .m_qnameLookup.remove(((Property) obj)
150: .getNamespace()
151: + "#" + ((Property) obj).getName());
152: if (objcheck == null) {
153: System.err.println("Object removal for href["
154: + ((Property) obj).getNamespace() + "#"
155: + ((Property) obj).getName() + "] is null!!! "
156: + this .m_qnameLookup.keySet());
157: }
158: }
159: }
160:
161: /**
162: * Removes a given object.
163: *
164: * @param obj Object
165: */
166: public void remove(Object obj) {
167: this .m_qnameLookup.remove(obj);
168: this .m_hrefLookup.remove(obj);
169: }
170:
171: /**
172: * Returns all the values in this ResourceLookup.
173: *
174: * @return Collection of objects
175: */
176: public Collection getValues() {
177: return this .m_hrefLookup.values();
178: }
179:
180: /**
181: * Returns all the fully qualified names in this ResourceLookup.
182: *
183: * @return Set of fully qualified names
184: */
185: public Set getQNameKeySet() {
186: return this .m_qnameLookup.keySet();
187: }
188:
189: /**
190: * Returns all the full paths in this ResoruceLookup.
191: *
192: * @return Set of full paths
193: */
194: public Set getHREFKeySet() {
195: return this.m_hrefLookup.keySet();
196: }
197:
198: }
|