001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package core.tut.pori.properties;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.io.OutputStream;
021import java.io.PrintStream;
022import java.io.PrintWriter;
023import java.io.Reader;
024import java.io.Writer;
025import java.util.Collection;
026import java.util.Collections;
027import java.util.Enumeration;
028import java.util.InvalidPropertiesFormatException;
029import java.util.Map;
030import java.util.Properties;
031import java.util.Set;
032
033import org.apache.commons.collections4.map.UnmodifiableEntrySet;
034
035/**
036 * Implementation of an unmodifiable/immutable Properties class.
037 * 
038 * Attempting to call any setter methods of this class will results in UnsupportedOperationException
039 */
040public class UnmodifiableProperties extends Properties{
041  /** for serialization */
042  private static final long serialVersionUID = -449888750397691453L;
043  private Properties _properties = null;
044  
045  /**
046   * 
047   */
048  private UnmodifiableProperties() {
049    super();
050  }
051  
052  /**
053   * wrap the properties object to unmodifiable properties
054   * 
055   * @param properties
056   * @return new UnmodifiableProperties or null if null was passed
057   */
058  public static UnmodifiableProperties unmodifiableProperties(Properties properties){
059    if(properties == null){
060      return null;
061    }
062    UnmodifiableProperties up = new UnmodifiableProperties();
063    up._properties = properties;
064    return up;
065  }
066
067  @Override
068  public synchronized Object setProperty(String key, String value) throws UnsupportedOperationException {
069    throw new UnsupportedOperationException("Not supported.");
070  }
071
072  @Override
073  public synchronized void load(Reader reader) throws IOException, UnsupportedOperationException {
074    throw new UnsupportedOperationException("Not supported.");
075  }
076
077  @Override
078  public synchronized void load(InputStream inStream) throws IOException, UnsupportedOperationException {
079    throw new UnsupportedOperationException("Not supported.");
080  }
081
082  @Override
083  public void save(OutputStream out, String comments) throws UnsupportedOperationException {
084    throw new UnsupportedOperationException("Not supported.");
085  }
086
087  @Override
088  public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException, UnsupportedOperationException {
089    throw new UnsupportedOperationException("Not supported.");
090  }
091
092  @Override
093  public synchronized Object put(Object key, Object value) throws UnsupportedOperationException {
094    throw new UnsupportedOperationException("Not supported.");
095  }
096
097  @Override
098  public synchronized Object remove(Object key) throws UnsupportedOperationException {
099    throw new UnsupportedOperationException("Not supported.");
100  }
101
102  @Override
103  public synchronized void putAll(Map<? extends Object, ? extends Object> t) throws UnsupportedOperationException {
104    throw new UnsupportedOperationException("Not supported.");
105  }
106
107  @Override
108  public synchronized void clear() throws UnsupportedOperationException{
109    throw new UnsupportedOperationException("Not supported.");
110  }
111
112  @Override
113  public Set<Object> keySet() {
114    return Collections.unmodifiableSet(super.keySet());
115  }
116
117  @Override
118  public Set<java.util.Map.Entry<Object, Object>> entrySet() {
119    return UnmodifiableEntrySet.unmodifiableEntrySet(super.entrySet());
120  }
121
122  @Override
123  public Collection<Object> values() {
124    return Collections.unmodifiableCollection(super.values());
125  }
126
127  @Override
128  public void store(Writer writer, String comments) throws IOException {
129    _properties.store(writer, comments);
130  }
131
132  @Override
133  public void store(OutputStream out, String comments) throws IOException {
134    _properties.store(out, comments);
135  }
136
137  @Override
138  public void storeToXML(OutputStream os, String comment) throws IOException {
139    _properties.storeToXML(os, comment);
140  }
141
142  @Override
143  public void storeToXML(OutputStream os, String comment, String encoding) throws IOException {
144    _properties.storeToXML(os, comment, encoding);
145  }
146
147  @Override
148  public String getProperty(String key) {
149    return _properties.getProperty(key);
150  }
151
152  @Override
153  public String getProperty(String key, String defaultValue) {
154    return _properties.getProperty(key, defaultValue);
155  }
156
157  @Override
158  public Enumeration<?> propertyNames() {
159    return _properties.propertyNames();
160  }
161
162  @Override
163  public Set<String> stringPropertyNames() {
164    return _properties.stringPropertyNames();
165  }
166
167  @Override
168  public void list(PrintStream out) {
169    _properties.list(out);
170  }
171
172  @Override
173  public void list(PrintWriter out) {
174    _properties.list(out);
175  }
176
177  @Override
178  public synchronized int size() {
179    return _properties.size();
180  }
181
182  @Override
183  public synchronized boolean isEmpty() {
184    return _properties.isEmpty();
185  }
186
187  @Override
188  public synchronized Enumeration<Object> keys() {
189    return _properties.keys();
190  }
191
192  @Override
193  public synchronized Enumeration<Object> elements() {
194    return _properties.elements();
195  }
196
197  @Override
198  public synchronized boolean contains(Object value) {
199    return _properties.contains(value);
200  }
201
202  @Override
203  public boolean containsValue(Object value) {
204    return _properties.containsValue(value);
205  }
206
207  @Override
208  public synchronized boolean containsKey(Object key) {
209    return _properties.containsKey(key);
210  }
211
212  @Override
213  public synchronized Object get(Object key) {
214    return _properties.get(key);
215  }
216
217  @SuppressWarnings("sync-override")
218  @Override
219  public int hashCode() {
220    final int prime = 31;
221    int result = super.hashCode();
222    result = prime * result + ((_properties == null) ? 0 : _properties.hashCode());
223    return result;
224  }
225
226  @SuppressWarnings("sync-override")
227  @Override
228  public boolean equals(Object obj) {
229    if (this == obj)
230      return true;
231    if (!super.equals(obj))
232      return false;
233    if (getClass() != obj.getClass())
234      return false;
235    UnmodifiableProperties other = (UnmodifiableProperties) obj;
236    if (_properties == null) {
237      if (other._properties != null)
238        return false;
239    } else if (!_properties.equals(other._properties))
240      return false;
241    return true;
242  }
243}