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 */
017package org.apache.camel.util;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Enumeration;
022import java.util.LinkedHashMap;
023import java.util.LinkedHashSet;
024import java.util.Map;
025import java.util.Properties;
026import java.util.Set;
027import java.util.Vector;
028
029/**
030 * This class is an ordered {@link Properties} where the key/values are stored in the order they are added or loaded.
031 * <p/>
032 * Note: This implementation is only intended as implementation detail for the Camel properties component, and has only
033 * been designed to provide the needed functionality. The complex logic for loading properties has been kept from the
034 * JDK {@link Properties} class.
035 */
036public final class OrderedProperties extends Properties {
037
038    private final Map<String, String> map = new LinkedHashMap<>();
039
040    public OrderedProperties() {
041    }
042
043    @Override
044    public synchronized Object put(Object key, Object value) {
045        return map.put(key.toString(), value.toString());
046    }
047
048    @Override
049    public synchronized void putAll(Map<?, ?> t) {
050        for (Map.Entry<?, ?> entry : t.entrySet()) {
051            put(entry.getKey(), entry.getValue());
052        }
053    }
054
055    @Override
056    public synchronized Object get(Object key) {
057        return map.get(key);
058    }
059
060    @Override
061    public synchronized boolean isEmpty() {
062        return map.isEmpty();
063    }
064
065    @Override
066    public synchronized Object remove(Object key) {
067        return map.remove(key);
068    }
069
070    @Override
071    public synchronized void clear() {
072        map.clear();
073    }
074
075    @Override
076    public String getProperty(String key) {
077        return map.get(key);
078    }
079
080    @Override
081    public String getProperty(String key, String defaultValue) {
082        return map.getOrDefault(key, defaultValue);
083    }
084
085    @Override
086    public synchronized Enumeration<Object> keys() {
087        return new Vector<Object>(map.keySet()).elements();
088    }
089
090    @Override
091    public Set<Object> keySet() {
092        return new LinkedHashSet<>(map.keySet());
093    }
094
095    @Override
096    @SuppressWarnings("unchecked")
097    public Set<Map.Entry<Object, Object>> entrySet() {
098        Set entrySet = map.entrySet();
099        return entrySet;
100    }
101
102    @Override
103    public synchronized int size() {
104        return map.size();
105    }
106
107    @Override
108    public Set<String> stringPropertyNames() {
109        return map.keySet();
110    }
111
112    @Override
113    public Collection<Object> values() {
114        return new ArrayList<>(map.values());
115    }
116
117    @Override
118    public synchronized String toString() {
119        return map.toString();
120    }
121}