You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2002/06/29 20:39:32 UTC
cvs commit: jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/immutable ImmutableUtils.java Immutable.java
scolebourne 2002/06/29 11:39:32
Added: pattern/src/java/org/apache/commons/pattern/immutable
ImmutableUtils.java Immutable.java
Log:
Initial checkin
Revision Changes Path
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/immutable/ImmutableUtils.java
Index: ImmutableUtils.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.immutable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
/**
* <code>ImmutableUtils</code> provides utilities for the Immutable pattern
* interface.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: ImmutableUtils.java,v 1.1 2002/06/29 18:39:32 scolebourne Exp $
*/
public class ImmutableUtils {
/**
* JVM shared Set of immutable classes
*/
public static Set cImmutableClasses = new HashSet();
static {
addImmutableClass(String.class);
addImmutableClass(Boolean.class);
addImmutableClass(Byte.class);
addImmutableClass(Character.class);
addImmutableClass(Short.class);
addImmutableClass(Integer.class);
addImmutableClass(Long.class);
addImmutableClass(Float.class);
addImmutableClass(Double.class);
addImmutableClass(Class.class);
addImmutableClass(Locale.class);
try {
addImmutableClass(Class.forName("java.util.Currency"));
} catch (ClassNotFoundException ex) {
// ignore
}
try {
addImmutableClass(Class.forName("java.net.URI"));
} catch (ClassNotFoundException ex) {
// ignore
}
// The following have been rejected:
// Method, Field, Constructor (mutable with setAccessible method)
// BigDecimal, BigInteger (not final)
}
/**
* Restructive constructor
*/
private ImmutableUtils() {
super();
}
/**
* Add a class to the list of known Immutable classes. This method
* is shared across the JVM, thus should usually only be called
* during initialisation.
* <p>
* This method is used to indicate that a class which does not
* implement Immutable is in fact immutable.
*
* @param the class to be added to the Set of immutable classes, null ignored
*/
public static void addImmutableClass(Class cls) {
if (cls != null) {
cImmutableClasses.add(cls);
}
}
/**
* Get the Set of Class objects representing those classes that
* are immutable but don't implement the interface.
*
* @return the Set of immutable classes
*/
public static Set getImmutableClasses() {
return Collections.unmodifiableSet(cImmutableClasses);
}
/**
* Tests if the object is immutable. This checks if it implements
* the Immutable interface, or if it is a known immutable type.
*
* @param object the object to check
* @throws IllegalArgumentException if the object specified is null
*/
public static boolean isImmutable(Object object) {
if (object == null) {
throw new IllegalArgumentException("The object must not be null");
}
if (object instanceof Immutable) {
return true;
}
return isImmutable(object.getClass());
}
/**
* Tests if the object is immutable. This checks if it implements
* the Immutable interface, or if it is a known immutable type.
*
* @param cls the class to check
* @throws IllegalArgumentException if the class specified is null
*/
public static boolean isImmutable(Class cls) {
if (cls == null) {
throw new IllegalArgumentException("The class must not be null");
}
if (Immutable.class.isAssignableFrom(cls)) {
return true;
}
return cImmutableClasses.contains(cls);
}
}
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/immutable/Immutable.java
Index: Immutable.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.immutable;
/**
* <code>Immutable</code> defines an interface implemented by classes that
* are immutable. To comply:
* <ul>
* <li>The class must be final
* <li>All fields must be private
* <li>The class must have no set methods, or other methods that modify
* the object
* <li>If the class holds a mutable object, that object must be cloned in
* the constructor, any get method and during deserialization using a
* readResolve() method
* <li>The class should have a readResolve method
* </ul>
* It should be noted that not every immutable class that you write will
* necessarily implement Immutable. Immutable is reserved for data classes,
* similar to String and Integer. The toString() method definition must
* be read carefully in particular.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: Immutable.java,v 1.1 2002/06/29 18:39:32 scolebourne Exp $
*/
public interface Immutable {
/**
* Compare this object to another. Immutable objects are often used
* in Maps, so the equals() method should be designed with care.
*
* @see java.lang.Object#equals()
* @return true if this object equals the passed in object
*/
public boolean equals(Object object);
/**
* Get a hash code that complies with the normal rules laid out in
* <code>java.lang.Object</code>. Immutable objects are often used
* in Maps, so the hashCode() method should be designed with care.
*
* @see java.lang.Object#hashCode()
* @return an integer hashcode for the object
*/
public int hashCode();
/**
* Return the entire information about the identifier as a String.
* <p>
* The method may not return null. The format of the String should
* be such that it can be parsed back to recreate the object. Normally,
* the class will have a static <code>parse(String)</code> method for
* this purpose.
* <p>
* This method is not a debugging method, and thus StringBuffer
* should be used to append Strings, not the + operator for
* performance reasons.
*
* @return a string representing the entire state of the immutable object
*/
public String toString();
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>