View Javadoc
1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.property;
17  
18  import static com.google.common.base.Preconditions.checkArgument;
19  import static org.kuali.common.util.base.Precondition.checkNotNull;
20  
21  import java.io.InputStream;
22  import java.io.Reader;
23  import java.util.Collection;
24  import java.util.Map;
25  import java.util.Properties;
26  import java.util.Set;
27  
28  import com.google.common.collect.ImmutableList;
29  import com.google.common.collect.ImmutableSet;
30  
31  public final class ImmutableProperties extends Properties {
32  
33  	private static final long serialVersionUID = -3964884087103719367L;
34  	private static final String UOE_MSG = "Immutable properties cannot be changed";
35  	private static final Properties EMPTY = copyOf(new Properties());
36  
37  	public ImmutableProperties(Properties original) {
38  		checkNotNull(original, "original");
39  
40  		// Prevent anything from changing original until we are done
41  		synchronized (original) {
42  
43  			// Extract only those keys where both the key and its corresponding value are strings
44  			Set<String> keys = original.stringPropertyNames();
45  
46  			// If the sizes are different, original contains at least one key or value that is not a string
47  			checkArgument(keys.size() == original.size(), "Immutable properties only support strings");
48  
49  			// Copy every key/value pair from original - can't use putAll() since it calls put() which is now disabled
50  			for (String key : keys) {
51  				super.put(key, original.getProperty(key));
52  			}
53  		}
54  	}
55  
56  	public static Properties of() {
57  		return EMPTY;
58  	}
59  
60  	/**
61  	 * Create and return a new immutable properties object identical to the one passed in. If <code>properties</code> is already immutable, no new object is created, the
62  	 * <code>properties</code> object passed in as a method argument is what is returned.
63  	 * 
64  	 * @throws NullPointerException
65  	 *             if {@code properties} is null
66  	 * 
67  	 * @deprecated use copyOf(Properties) instead
68  	 */
69  	@Deprecated
70  	public static Properties of(Properties properties) {
71  		return copyOf(properties);
72  	}
73  
74  	/**
75  	 * Create and return a new immutable properties object identical to the one passed in. If <code>properties</code> is already immutable, no new object is created, the
76  	 * <code>properties</code> object passed in as a method argument is what is returned.
77  	 * 
78  	 * @throws NullPointerException
79  	 *             if {@code properties} is null
80  	 */
81  	public static ImmutableProperties copyOf(Properties properties) {
82  		if (properties instanceof ImmutableProperties) {
83  			return (ImmutableProperties) properties;
84  		} else {
85  			return new ImmutableProperties(properties);
86  		}
87  	}
88  
89  	public static ImmutableProperties copyOf(Map<String, String> map) {
90  		Properties properties = new Properties();
91  		for (String key : map.keySet()) {
92  			properties.setProperty(key, map.get(key));
93  		}
94  		return copyOf(properties);
95  	}
96  
97  	@Override
98  	public Object setProperty(String key, String value) {
99  		throw new UnsupportedOperationException(UOE_MSG);
100 	}
101 
102 	@Override
103 	public void load(Reader reader) {
104 		throw new UnsupportedOperationException(UOE_MSG);
105 	}
106 
107 	@Override
108 	public void load(InputStream inStream) {
109 		throw new UnsupportedOperationException(UOE_MSG);
110 	}
111 
112 	@Override
113 	public void loadFromXML(InputStream in) {
114 		throw new UnsupportedOperationException(UOE_MSG);
115 	}
116 
117 	@Override
118 	public Object put(Object key, Object value) {
119 		throw new UnsupportedOperationException(UOE_MSG);
120 	}
121 
122 	@Override
123 	public Object remove(Object key) {
124 		throw new UnsupportedOperationException(UOE_MSG);
125 	}
126 
127 	@Override
128 	public void putAll(Map<? extends Object, ? extends Object> t) {
129 		throw new UnsupportedOperationException(UOE_MSG);
130 	}
131 
132 	@Override
133 	public void clear() {
134 		throw new UnsupportedOperationException(UOE_MSG);
135 	}
136 
137 	@Override
138 	public Set<Object> keySet() {
139 		return ImmutableSet.copyOf(super.keySet());
140 	}
141 
142 	@Override
143 	public Set<java.util.Map.Entry<Object, Object>> entrySet() {
144 		return ImmutableSet.copyOf(super.entrySet());
145 	}
146 
147 	@Override
148 	public Collection<Object> values() {
149 		return ImmutableList.copyOf(super.values());
150 	}
151 
152 }