001    /**
002     * Copyright (c) 2000-2010 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            public void clear() {
053                    super.clear();
054    
055                    _names.clear();
056            }
057    
058            public void list(PrintStream out) {
059                    System.out.println("-- listing properties --");
060    
061                    Enumeration<String> enu = propertyNames();
062    
063                    while (enu.hasMoreElements()) {
064                            String name = enu.nextElement();
065    
066                            out.println(name + StringPool.EQUAL + getProperty(name));
067                    }
068            }
069    
070            public void list(PrintWriter out) {
071                    System.out.println("-- listing properties --");
072    
073                    Enumeration<String> enu = propertyNames();
074    
075                    while (enu.hasMoreElements()) {
076                            String name = enu.nextElement();
077    
078                            out.println(name + StringPool.EQUAL + getProperty(name));
079                    }
080            }
081    
082            public Enumeration<String> propertyNames() {
083                    return Collections.enumeration(_names);
084            }
085    
086            public Object put(String key, String value) {
087                    if (_names.contains(key)) {
088                            _names.remove(key);
089                    }
090    
091                    _names.add(key);
092    
093                    return super.put(key, value);
094            }
095    
096            public Object remove(Object key) {
097                    _names.remove(key);
098    
099                    return super.remove(key);
100            }
101    
102            public Object setProperty(String key, String value) {
103                    return put(key, value);
104            }
105    
106            private Set<String> _names;
107    
108    }