1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.io.PrintStream;
18  import java.io.PrintWriter;
19  
20  import java.util.Collections;
21  import java.util.Enumeration;
22  import java.util.Properties;
23  import java.util.Set;
24  import java.util.TreeSet;
25  
26  /**
27   * <a href="SortedProperties.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class SortedProperties extends Properties {
32  
33      public SortedProperties() {
34          super();
35  
36          _names = new TreeSet<String>();
37      }
38  
39      public SortedProperties(Properties properties) {
40          this();
41  
42          Enumeration<String> enu =
43              (Enumeration<String>)properties.propertyNames();
44  
45          while (enu.hasMoreElements()) {
46              String key = enu.nextElement();
47  
48              String value = properties.getProperty(key);
49  
50              setProperty(key, value);
51          }
52      }
53  
54      public void clear() {
55          super.clear();
56  
57          _names.clear();
58      }
59  
60      public void list(PrintStream out) {
61          System.out.println("-- listing properties --");
62  
63          Enumeration<String> enu = propertyNames();
64  
65          while (enu.hasMoreElements()) {
66              String name = enu.nextElement();
67  
68              out.println(name + StringPool.EQUAL + getProperty(name));
69          }
70      }
71  
72      public void list(PrintWriter out) {
73          System.out.println("-- listing properties --");
74  
75          Enumeration<String> enu = propertyNames();
76  
77          while (enu.hasMoreElements()) {
78              String name = enu.nextElement();
79  
80              out.println(name + StringPool.EQUAL + getProperty(name));
81          }
82      }
83  
84      public Enumeration<String> propertyNames() {
85          return Collections.enumeration(_names);
86      }
87  
88      public Object put(String key, String value) {
89          if (_names.contains(key)) {
90              _names.remove(key);
91          }
92  
93          _names.add(key);
94  
95          return super.put(key, value);
96      }
97  
98      public Object remove(Object key) {
99          _names.remove(key);
100 
101         return super.remove(key);
102     }
103 
104     public Object setProperty(String key, String value) {
105         return put(key, value);
106     }
107 
108     private Set<String> _names;
109 
110 }