A toy to create dummy null objects with intelligent behaviour.
The package provides a proxy factory creating proxies, that work as a
dummy replacement. Main component is the {@linkplain
com.thoughtworks.proxy.toys.nullobject.Null Null toy}, a utility class
creating these proxies. Such a proxy contains an instance of a {@link
com.thoughtworks.proxy.toys.nullobject.NullInvoker} that handles the
calls.
A null object instantiated by this toy has deterministically boring
behaviour as follows:
- If a method's return type is a primitive, the null object
returns the default for that type (e.g. false for boolean).
- If a method's return type is an array, the null object
returns an empty array, which is usually nicer than just returning null.
(In fact an empty array is just the null object for arrays.)
- If a method's return type is {@link java.util.List}, {@link
java.util.Collections#EMPTY_LIST} is returned.
- If a method's return type is {@link java.util.Set}, {@link
java.util.Collections#EMPTY_SET} is returned.
- If a method's return type is {@link java.util.Map}, {@link
java.util.Collections#EMPTY_MAP} is returned.
- If a method's return type is {@link java.util.SortedSet}, {@link
com.thoughtworks.proxy.toys.nullobject.Null#NULL_SORTED_SET} is
returned.
- If a method's return type is {@link java.util.SortedMap}, {@link
com.thoughtworks.proxy.toys.nullobject.Null#NULL_SORTED_MAP} is
returned.
- If a method's return type is {@link java.lang.Object}, an Object is
returned.
- If the method's return type is any other type, the Null toy returns
one of the following:
- If the currently installed {@link
com.thoughtworks.proxy.ProxyFactory} can create a proxy for the type, a
new null object for that type is returned (so you can recurse or step
through object graphs without surprises). For the standard proxy
factory this requires the return type to be an interface.
- If the ProxyFactory in use cannot create a proxy for the type, null
is returned.
In the follwoing example the behaviour is demonstrated by an Null
object of the type {@link java.io.File}:
ProxyFactory factory = new CglibProxyFactory();
File file = (File)Null.object(File.class, factory);
System.out.println("Length is: " + file.length());
System.out.println("Exists: " + file.exists());
System.out.println("Array is empty: " + file.list().length);
System.out.println("toURL returns null, since URL is final: " + (file.toURL() == null));
System.out.println("Parent file is Null proxy: " + Null.isNullObject(file.getParentFile(), factory));