001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * ProtectedProperties.java
019: * Copyright (C) 2001 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.core;
024:
025: import java.io.InputStream;
026: import java.util.Enumeration;
027: import java.util.Map;
028: import java.util.Properties;
029:
030: /**
031: * Simple class that extends the Properties class so that the properties are
032: * unable to be modified.
033: *
034: * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
035: * @version $Revision: 1.5 $
036: */
037: public class ProtectedProperties extends Properties {
038:
039: /** for serialization */
040: private static final long serialVersionUID = 3876658672657323985L;
041:
042: /** the properties need to be open during construction of the object */
043: private boolean closed = false;
044:
045: /**
046: * Creates a set of protected properties from a set of normal ones.
047: *
048: * @param props the properties to be stored and protected.
049: */
050: public ProtectedProperties(Properties props) {
051:
052: Enumeration propEnum = props.propertyNames();
053: while (propEnum.hasMoreElements()) {
054: String propName = (String) propEnum.nextElement();
055: String propValue = props.getProperty(propName);
056: super .setProperty(propName, propValue);
057: }
058: closed = true; // no modifications allowed from now on
059: }
060:
061: /**
062: * Overrides a method to prevent the properties from being modified.
063: *
064: * @return never returns without throwing an exception.
065: * @throws UnsupportedOperationException always.
066: */
067: public Object setProperty(String key, String value) {
068:
069: if (closed)
070: throw new UnsupportedOperationException(
071: "ProtectedProperties cannot be modified!");
072: else
073: return super .setProperty(key, value);
074: }
075:
076: /**
077: * Overrides a method to prevent the properties from being modified.
078: *
079: * @throws UnsupportedOperationException always.
080: */
081: public void load(InputStream inStream) {
082:
083: throw new UnsupportedOperationException(
084: "ProtectedProperties cannot be modified!");
085: }
086:
087: /**
088: * Overrides a method to prevent the properties from being modified.
089: *
090: * @throws UnsupportedOperationException always.
091: */
092: public void clear() {
093:
094: throw new UnsupportedOperationException(
095: "ProtectedProperties cannot be modified!");
096: }
097:
098: /**
099: * Overrides a method to prevent the properties from being modified.
100: *
101: * @return never returns without throwing an exception.
102: * @throws UnsupportedOperationException always.
103: */
104: public Object put(Object key, Object value) {
105:
106: if (closed)
107: throw new UnsupportedOperationException(
108: "ProtectedProperties cannot be modified!");
109: else
110: return super .put(key, value);
111: }
112:
113: /**
114: * Overrides a method to prevent the properties from being modified.
115: *
116: * @throws UnsupportedOperationException always.
117: */
118: public void putAll(Map t) {
119:
120: throw new UnsupportedOperationException(
121: "ProtectedProperties cannot be modified!");
122: }
123:
124: /**
125: * Overrides a method to prevent the properties from being modified.
126: *
127: * @return never returns without throwing an exception.
128: * @throws UnsupportedOperationException always.
129: */
130: public Object remove(Object key) {
131:
132: throw new UnsupportedOperationException(
133: "ProtectedProperties cannot be modified!");
134: }
135:
136: }
|