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.commons.configuration;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: /**
026: * A Map based Configuration.
027: *
028: * @author Emmanuel Bourg
029: * @version $Revision: 515306 $, $Date: 2007-03-06 22:15:00 +0100 (Di, 06 Mrz 2007) $
030: * @since 1.1
031: */
032: public class MapConfiguration extends AbstractConfiguration implements
033: Cloneable {
034: /** The Map decorated by this configuration. */
035: protected Map map;
036:
037: /**
038: * Create a Configuration decorator around the specified Map. The map is
039: * used to store the configuration properties, any change will also affect
040: * the Map.
041: *
042: * @param map the map
043: */
044: public MapConfiguration(Map map) {
045: this .map = map;
046: }
047:
048: /**
049: * Return the Map decorated by this configuration.
050: *
051: * @return the map this configuration is based onto
052: */
053: public Map getMap() {
054: return map;
055: }
056:
057: public Object getProperty(String key) {
058: Object value = map.get(key);
059: if ((value instanceof String)
060: && (!isDelimiterParsingDisabled())) {
061: List list = PropertyConverter.split((String) value,
062: getListDelimiter());
063: return list.size() > 1 ? list : list.get(0);
064: } else {
065: return value;
066: }
067: }
068:
069: protected void addPropertyDirect(String key, Object value) {
070: Object previousValue = getProperty(key);
071:
072: if (previousValue == null) {
073: map.put(key, value);
074: } else if (previousValue instanceof List) {
075: // the value is added to the existing list
076: ((List) previousValue).add(value);
077: } else {
078: // the previous value is replaced by a list containing the previous value and the new value
079: List list = new ArrayList();
080: list.add(previousValue);
081: list.add(value);
082:
083: map.put(key, list);
084: }
085: }
086:
087: public boolean isEmpty() {
088: return map.isEmpty();
089: }
090:
091: public boolean containsKey(String key) {
092: return map.containsKey(key);
093: }
094:
095: protected void clearPropertyDirect(String key) {
096: map.remove(key);
097: }
098:
099: public Iterator getKeys() {
100: return map.keySet().iterator();
101: }
102:
103: /**
104: * Returns a copy of this object. The returned configuration will contain
105: * the same properties as the original. Event listeners are not cloned.
106: *
107: * @return the copy
108: * @since 1.3
109: */
110: public Object clone() {
111: try {
112: MapConfiguration copy = (MapConfiguration) super .clone();
113: copy.clearConfigurationListeners();
114: copy.map = (Map) ConfigurationUtils.clone(map);
115: return copy;
116: } catch (CloneNotSupportedException cex) {
117: // cannot happen
118: throw new ConfigurationRuntimeException(cex);
119: }
120: }
121: }
|