001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.PrintStream;
018    import java.io.PrintWriter;
019    
020    import java.util.Collections;
021    import java.util.Enumeration;
022    import java.util.Properties;
023    import java.util.Set;
024    import java.util.TreeSet;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class SortedProperties extends Properties {
030    
031            public SortedProperties() {
032                    super();
033    
034                    _names = new TreeSet<String>();
035            }
036    
037            public SortedProperties(Properties properties) {
038                    this();
039    
040                    Enumeration<String> enu =
041                            (Enumeration<String>)properties.propertyNames();
042    
043                    while (enu.hasMoreElements()) {
044                            String key = enu.nextElement();
045    
046                            String value = properties.getProperty(key);
047    
048                            setProperty(key, value);
049                    }
050            }
051    
052            @Override
053            public void clear() {
054                    super.clear();
055    
056                    _names.clear();
057            }
058    
059            @Override
060            public void list(PrintStream out) {
061                    System.out.println("-- listing properties --");
062    
063                    Enumeration<String> enu = propertyNames();
064    
065                    while (enu.hasMoreElements()) {
066                            String name = enu.nextElement();
067    
068                            out.println(name + StringPool.EQUAL + getProperty(name));
069                    }
070            }
071    
072            @Override
073            public void list(PrintWriter out) {
074                    System.out.println("-- listing properties --");
075    
076                    Enumeration<String> enu = propertyNames();
077    
078                    while (enu.hasMoreElements()) {
079                            String name = enu.nextElement();
080    
081                            out.println(name + StringPool.EQUAL + getProperty(name));
082                    }
083            }
084    
085            @Override
086            public Enumeration<String> propertyNames() {
087                    return Collections.enumeration(_names);
088            }
089    
090            public Object put(String key, String value) {
091                    if (_names.contains(key)) {
092                            _names.remove(key);
093                    }
094    
095                    _names.add(key);
096    
097                    return super.put(key, value);
098            }
099    
100            @Override
101            public Object remove(Object key) {
102                    _names.remove(key);
103    
104                    return super.remove(key);
105            }
106    
107            @Override
108            public Object setProperty(String key, String value) {
109                    return put(key, value);
110            }
111    
112            private Set<String> _names;
113    
114    }