001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common.tagging;
009:
010: //base classes
011: import java.util.ArrayList;
012: import java.util.HashMap;
013:
014: //project specific classes
015: import org.jfolder.common.UnexpectedSystemException;
016:
017: //other classes
018:
019: public class ConceptTagFlags {
020:
021: private final static String MAIN_NAMESPACE = ConceptTagFlagsHelper.MAIN_NAMESPACE;
022: //
023:
024: private HashMap flagMap = null;
025:
026: protected ConceptTagFlags() {
027: initialize();
028: }
029:
030: protected ConceptTagFlags(ConceptTagFlags inCtf) {
031: this ();
032: duplicate(inCtf);
033: }
034:
035: //
036: protected void initialize() {
037: //
038: this .flagMap = new HashMap();
039: }
040:
041: //
042: public boolean isMainFlagPresent(String inName) {
043: return isFlagPresent(MAIN_NAMESPACE, inName);
044: }
045:
046: public boolean isFlagPresent(String inNamespace, String inName) {
047:
048: boolean outValue = true;
049:
050: if (isNamespacePresent(inNamespace)) {
051: HashMap ns = getNamespace(inNamespace);
052: outValue = ns.containsKey(inName);
053: } else {
054: outValue = false;
055: }
056:
057: return outValue;
058: }
059:
060: //
061: public void createMainFlag(String inName, Object inValue) {
062: createFlag(MAIN_NAMESPACE, inName, inValue);
063: }
064:
065: public void createFlag(String inNamespace, String inName,
066: Object inValue) {
067: if (!isFlagPresent(inNamespace, inName)) {
068: HashMap ns = null;
069: if (isNamespacePresent(inNamespace)) {
070: ns = getNamespace(inNamespace);
071: } else {
072: ns = createNamespace(inNamespace);
073: }
074: ns.put(inName, inValue);
075: } else {
076: throw new UnexpectedSystemException("Flag '" + inName
077: + "' in namespace '" + inNamespace
078: + "' is already present");
079: }
080: }
081:
082: //
083: public void updateMainFlag(String inName, Object inValue) {
084: updateFlag(MAIN_NAMESPACE, inName, inValue);
085: }
086:
087: public void updateFlag(String inNamespace, String inName,
088: Object inValue) {
089: removeFlag(inNamespace, inName);
090: createFlag(inNamespace, inName, inValue);
091: }
092:
093: //
094: public void removeMainFlag(String inName) {
095: removeFlag(MAIN_NAMESPACE, inName);
096: }
097:
098: public void removeFlag(String inNamespace, String inName) {
099: if (isFlagPresent(inNamespace, inName)) {
100: HashMap ns = getNamespace(inNamespace);
101: ns.remove(inName);
102: //
103: if (ns.size() == 0) {
104: this .flagMap.remove(inNamespace);
105: }
106: } else {
107: throw new UnexpectedSystemException("Flag '" + inName
108: + "' in namespace '" + inNamespace
109: + "' is not present");
110: }
111: }
112:
113: //
114: public Object getMainFlag(String inName) {
115: return getFlag(MAIN_NAMESPACE, inName);
116: }
117:
118: public Object getFlag(String inNamespace, String inName) {
119:
120: Object outValue = null;
121:
122: if (isFlagPresent(inNamespace, inName)) {
123: HashMap ns = getNamespace(inNamespace);
124: outValue = ns.get(inName);
125: } else {
126: throw new UnexpectedSystemException("Flag '" + inName
127: + "' in namespace '" + inNamespace
128: + "' is not present");
129: }
130:
131: return outValue;
132: }
133:
134: //
135: private boolean isNamespacePresent(String inNamespace) {
136: return this .flagMap.containsKey(inNamespace);
137: }
138:
139: private HashMap createNamespace(String inNamespace) {
140:
141: HashMap outValue = null;
142:
143: if (!isNamespacePresent(inNamespace)) {
144: outValue = new HashMap();
145: this .flagMap.put(inNamespace, outValue);
146: } else {
147: throw new UnexpectedSystemException("Namespace '"
148: + inNamespace + "' already registered");
149: }
150:
151: return outValue;
152: }
153:
154: private HashMap getNamespace(String inNamespace) {
155:
156: HashMap outValue = null;
157:
158: if (isNamespacePresent(inNamespace)) {
159: outValue = (HashMap) this .flagMap.get(inNamespace);
160: } else {
161: throw new UnexpectedSystemException("Namespace '"
162: + inNamespace + "' not present");
163: }
164:
165: return outValue;
166: }
167:
168: //
169: public ArrayList getAllNamespaces() {
170: return new ArrayList(this .flagMap.keySet());
171: }
172:
173: public ArrayList getAllNames(String inNamespace) {
174: return new ArrayList(getNamespace(inNamespace).keySet());
175: }
176:
177: //
178: public void copyFlagsFrom(ConceptTagFlags inCtf) {
179: duplicate(inCtf);
180: }
181:
182: private void duplicate(ConceptTagFlags inCtf) {
183:
184: //
185: ArrayList namespaces = inCtf.getAllNamespaces();
186: for (int i = 0; i < namespaces.size(); i++) {
187:
188: //
189: String nextNamespace = (String) namespaces.get(i);
190: //createNamespace(nextNamespace);
191:
192: //
193: ArrayList names = inCtf.getAllNames(nextNamespace);
194: for (int j = 0; j < names.size(); j++) {
195: String nextName = (String) names.get(j);
196: Object nextValue = inCtf.getFlag(nextNamespace,
197: nextName);
198: //
199: if (!isFlagPresent(nextNamespace, nextName)) {
200: createFlag(nextNamespace, nextName, nextValue);
201: } else {
202: throw new UnexpectedSystemException(
203: "Flag with namespace '" + nextNamespace
204: + "' and name '" + nextName
205: + "' already exists");
206: }
207: }
208: }
209:
210: }
211:
212: //////////////////////////////////////////////////////////////////
213: public int hashCode() {
214:
215: int outValue = 0;
216:
217: outValue += this .flagMap.hashCode();
218:
219: return outValue;
220: }
221:
222: public boolean equals(Object inObj) {
223:
224: boolean outValue = true;
225:
226: if (inObj instanceof ConceptTagFlags) {
227: ConceptTagFlags otherCtf = ((ConceptTagFlags) inObj);
228: outValue = equals(otherCtf);
229: } else {
230: outValue = false;
231: }
232:
233: return outValue;
234: }
235:
236: private boolean equals(ConceptTagFlags inOtherCtf) {
237:
238: boolean outValue = true;
239:
240: outValue = this.flagMap.equals(inOtherCtf.flagMap);
241:
242: return outValue;
243: }
244: }
|