01: /*
02: * Copyright 2001-2006 C:1 Financial Services GmbH
03: *
04: * This software is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License Version 2.1, as published by the Free Software Foundation.
07: *
08: * This software is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11: * Lesser General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public
14: * License along with this library; if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
16: */
17:
18: package de.finix.contelligent.client.base;
19:
20: import java.util.Collection;
21: import java.util.HashMap;
22: import java.util.Iterator;
23: import java.util.Map;
24: import java.util.TreeMap;
25:
26: /**
27: * Abstract base that allows objects to register a mapping from a symbolic to an
28: * implementation name.
29: */
30: public abstract class AbstractRegistry {
31: protected Map<String, String> entries;
32:
33: protected Map<String, String> aliases;
34:
35: protected AbstractRegistry() {
36: entries = new TreeMap<String, String>();
37: aliases = new HashMap<String, String>();
38: }
39:
40: public void register(String symbolicName, String impl) {
41: entries.put(symbolicName, impl);
42: }
43:
44: public void registerAlias(String aliasName, String symbolicName) {
45: aliases.put(aliasName, symbolicName);
46: }
47:
48: public boolean isImplRegistered(String impl) {
49: return (entries.containsValue(impl));
50: }
51:
52: public Collection getSymbolicNames() {
53: return entries.keySet();
54: }
55:
56: public Collection getImpls() {
57: return entries.values();
58: }
59:
60: public String getImpl(String symbolicName) {
61: String impl = (String) entries.get(symbolicName);
62: if (impl == null) {
63: // Only check aliases if none of the primary mappings matches
64: String newName = (String) aliases.get(symbolicName);
65: impl = (String) entries.get(newName);
66: if (impl == null) {
67: throw new NullPointerException();
68: }
69: }
70: return impl;
71: }
72:
73: public String resolveAlias(String aliasName) {
74: String result = (String) aliases.get(aliasName);
75: if (result == null) {
76: return aliasName;
77: } else {
78: return result;
79: }
80: }
81:
82: /** Override this method to return your default implementation */
83: abstract public String getDefaultImpl();
84:
85: public String getSymbolicName(String impl) {
86: for (Iterator i = entries.entrySet().iterator(); i.hasNext();) {
87: Map.Entry entry = (Map.Entry) i.next();
88: if (entry.getValue().equals(impl)) {
89: return (String) entry.getKey();
90: }
91: }
92: return null;
93: }
94: }
|