Represents a persistent set of properties : Properties « Development Class « Java






Represents a persistent set of properties

      
/*
 * Copyright 2006-2007 The Scriptella Project Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Represents a persistent set of properties.
 * <p>This class is a replacement for {@link Properties} class.
 * <p>Please note that {@link #put(String,Object)} has additional semantics.
 *
 * @author Fyodor Kupolov
 * @version 1.0
 */
public class PropertiesMap implements Map<String, Object> {
    private Map<String, Object> props;

    public PropertiesMap() {
        props = new LinkedHashMap<String, Object>();
    }

    public PropertiesMap(int initialCapacity) {
        props = new LinkedHashMap<String, Object>(initialCapacity);
    }

    public PropertiesMap(Map<String, ?> props) {
        this(props.size());
        putAll(props);
    }

    /**
     * Creates a properties map from the stream.
     *
     * @param is stream to {@link #load(java.io.InputStream) load} properties from.
     * @throws IOException if I/O error occurs
     * @see #load(java.io.InputStream)
     */
    public PropertiesMap(InputStream is) throws IOException {
        this();
        load(is);
    }

    public int size() {
        return props.size();
    }

    public boolean isEmpty() {
        return props.isEmpty();
    }

    public boolean containsKey(Object key) {
        return props.containsKey(key);
    }

    public boolean containsValue(Object value) {
        return props.containsValue(value);
    }

    public Object get(Object key) {
        return props.get(key);
    }

    /**
     * Put the property to underlying map.
     * <p>The properties are immutable, i.e. if the property is already present in the map, the new value is ignored.
     *
     * @param key   property name
     * @param value property value
     * @return value associated with specified key,
     *         or null if there was no mapping for key.
     */
    public Object put(String key, Object value) {
        Object old = props.get(key);
        if (old == null) {
            props.put(key, value);
        }
        return old;
    }

    public Object remove(Object key) {
        return props.remove(key);
    }

    public void putAll(Map<? extends String, ? extends Object> t) {
        for (Entry<? extends String, ? extends Object> entry : t.entrySet()) {
            put(entry.getKey(), entry.getValue());
        }
    }

    public void clear() {
        props.clear();
    }

    public Set<String> keySet() {
        return props.keySet();
    }

    public Collection<Object> values() {
        return props.values();
    }

    public Set<Entry<String, Object>> entrySet() {
        return props.entrySet();
    }

    public boolean equals(Object o) {
        return props.equals(o);
    }

    public int hashCode() {
        return props.hashCode();
    }

    /**
     * Loads properties using {@link Properties#load(java.io.InputStream)}.
     * <p>Properties order is preserved
     *
     * @param is input stream with properties.
     * @throws IOException if I/O error occurs.
     */
    public void load(InputStream is) throws IOException {
        new Properties() { //Overrides Properties to preserve insertion order

            public Object put(final Object k, final Object v) {
                return PropertiesMap.this.put((String) k, v);
            }
        }.load(is);
    }

    


    public String toString() {
        return String.valueOf(props);
    }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Read a set of properties from the received input stream, strip off any excess white space that exists in those property values,
2.Copy a set of properties from one Property to another.
3.Sorts property list and print out each key=value pair prepended with specific indentation.
4.Sorts a property list and turns the sorted list into a string.
5.A utility class for replacing properties in strings.
6.Property Loader
7.Property access utility methods
8.Converts specified map to java.util.Properties
9.A java.util.Properties class that will check a file or URL for changes periodically
10.Encapsulates java.util.Properties to add java primitives and some other java classes
11.Load and save properties to files.
12.Adds new properties to an existing set of properties
13.Extracts a specific property key subset from the known properties
14.Observable Properties
15.Merge Properties Into Map
16.XML configuration management
17.JDOM based XML properties
18.XML Properties
19.Converts Unicode into something that can be embedded in a java properties file
20.Create Properties from String array
21.Task to overwrite Properties
22.Gets strong-type-value property from a standard Properties
23.The properties iterator iterates over a set of enumerated properties.
24.An utility class to ease up using property-file resource bundles.
25.Sorted Properties
26.A subclass of the java.util.Properties class that must be initialized from a file on disk
27.This class contains a collection of static utility methods for creating, retrieving, saving and loading properties.
28.Simplify routine job with properties
29.Property Manager