1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import java.io.PrintStream;
23  import java.io.PrintWriter;
24  
25  import java.util.Collections;
26  import java.util.Enumeration;
27  import java.util.Properties;
28  import java.util.Set;
29  import java.util.TreeSet;
30  
31  /**
32   * <a href="SortedProperties.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class SortedProperties extends Properties {
38  
39      public SortedProperties() {
40          super();
41  
42          _names = new TreeSet<String>();
43      }
44  
45      public SortedProperties(Properties properties) {
46          this();
47  
48          Enumeration<String> enu =
49              (Enumeration<String>)properties.propertyNames();
50  
51          while (enu.hasMoreElements()) {
52              String key = enu.nextElement();
53  
54              String value = properties.getProperty(key);
55  
56              setProperty(key, value);
57          }
58      }
59  
60      public void clear() {
61          super.clear();
62  
63          _names.clear();
64      }
65  
66      public void list(PrintStream out) {
67          System.out.println("-- listing properties --");
68  
69          Enumeration<String> enu = propertyNames();
70  
71          while (enu.hasMoreElements()) {
72              String name = enu.nextElement();
73  
74              out.println(name + StringPool.EQUAL + getProperty(name));
75          }
76      }
77  
78      public void list(PrintWriter out) {
79          System.out.println("-- listing properties --");
80  
81          Enumeration<String> enu = propertyNames();
82  
83          while (enu.hasMoreElements()) {
84              String name = enu.nextElement();
85  
86              out.println(name + StringPool.EQUAL + getProperty(name));
87          }
88      }
89  
90      public Enumeration<String> propertyNames() {
91          return Collections.enumeration(_names);
92      }
93  
94      public Object put(String key, String value) {
95          if (_names.contains(key)) {
96              _names.remove(key);
97          }
98  
99          _names.add(key);
100 
101         return super.put(key, value);
102     }
103 
104     public Object remove(Object key) {
105         _names.remove(key);
106 
107         return super.remove(key);
108     }
109 
110     public Object setProperty(String key, String value) {
111         return put(key, value);
112     }
113 
114     private Set<String> _names;
115 
116 }