001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.util.extendable;
019:
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Map.Entry;
025:
026: public class UnmodifiableExtendableItem implements ExtendableItem {
027: private final Map attributes = new HashMap();
028:
029: private final Map unmodifiableAttributesView = Collections
030: .unmodifiableMap(attributes);
031:
032: private final Map stdAttributes = new HashMap();
033:
034: private final Map unmodifiableStdAttributesView = Collections
035: .unmodifiableMap(stdAttributes);
036:
037: private final Map extraAttributes = new HashMap();
038:
039: private final Map unmodifiableExtraAttributesView = Collections
040: .unmodifiableMap(extraAttributes);
041:
042: /*
043: * this is the only place where extra attributes are stored in qualified form. In all other maps
044: * they are stored unqualified.
045: */
046: private final Map qualifiedExtraAttributes = new HashMap();
047:
048: private final Map unmodifiableQualifiedExtraAttributesView = Collections
049: .unmodifiableMap(qualifiedExtraAttributes);
050:
051: public UnmodifiableExtendableItem(Map stdAttributes,
052: Map extraAttributes) {
053: if (stdAttributes != null) {
054: this .attributes.putAll(stdAttributes);
055: this .stdAttributes.putAll(stdAttributes);
056: }
057: if (extraAttributes != null) {
058: for (Iterator iter = extraAttributes.entrySet().iterator(); iter
059: .hasNext();) {
060: Entry extraAtt = (Entry) iter.next();
061: setExtraAttribute((String) extraAtt.getKey(),
062: (String) extraAtt.getValue());
063: }
064: }
065: }
066:
067: public String getAttribute(String attName) {
068: return (String) attributes.get(attName);
069: }
070:
071: public String getExtraAttribute(String attName) {
072: String v = (String) qualifiedExtraAttributes.get(attName);
073: if (v == null) {
074: v = (String) extraAttributes.get(attName);
075: }
076: return v;
077: }
078:
079: public String getStandardAttribute(String attName) {
080: return (String) stdAttributes.get(attName);
081: }
082:
083: protected void setExtraAttribute(String attName, String attValue) {
084: setAttribute(attName, attValue, true);
085: }
086:
087: protected void setStandardAttribute(String attName, String attValue) {
088: setAttribute(attName, attValue, false);
089: }
090:
091: protected void setAttribute(String attName, String attValue,
092: boolean extra) {
093: if (extra) {
094: qualifiedExtraAttributes.put(attName, attValue);
095:
096: // unqualify att name if required
097: int index = attName.indexOf(':');
098: if (index != -1) {
099: attName = attName.substring(index + 1);
100: }
101: extraAttributes.put(attName, attValue);
102: } else {
103: stdAttributes.put(attName, attValue);
104: }
105: attributes.put(attName, attValue);
106: }
107:
108: public Map getAttributes() {
109: return unmodifiableAttributesView;
110: }
111:
112: public Map getStandardAttributes() {
113: return unmodifiableStdAttributesView;
114: }
115:
116: public Map getExtraAttributes() {
117: return unmodifiableExtraAttributesView;
118: }
119:
120: public Map getQualifiedExtraAttributes() {
121: return unmodifiableQualifiedExtraAttributesView;
122: }
123:
124: }
|