1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
41 synchronized (original) {
42
43
44 Set<String> keys = original.stringPropertyNames();
45
46
47 checkArgument(keys.size() == original.size(), "Immutable properties only support strings");
48
49
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
62
63
64
65
66
67
68
69 @Deprecated
70 public static Properties of(Properties properties) {
71 return copyOf(properties);
72 }
73
74
75
76
77
78
79
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 }