Class NullObject

java.lang.Object
groovy.lang.GroovyObjectSupport
org.codehaus.groovy.runtime.NullObject
All Implemented Interfaces:
GroovyObject

public class NullObject
extends GroovyObjectSupport
  • Method Summary

    Modifier and Type Method Description
    boolean asBoolean()
    A null object coerces to false.
    java.lang.Object asType​(java.lang.Class c)
    Type conversion method for null.
    java.lang.Object clone()
    Since this is implemented as a singleton, avoid the use of the clone method.
    boolean equals​(java.lang.Object o)
    null is only equal to null.
    static NullObject getNullObject()
    Returns the NullObject reference.
    java.lang.Object getProperty​(java.lang.String name)
    Tries to get a property on null, which fails.
    int hashCode()  
    java.lang.Object invokeMethod​(java.lang.String name, java.lang.Object arguments)
    Tries to invoke a method on null, which falis.
    boolean is​(java.lang.Object o)
    Tests for equal references.
    java.util.Iterator iterator()
    Provides ability to iterate on null.
    java.lang.Object plus​(java.lang.Object o)
    Fallback for null+null.
    java.lang.Object plus​(java.lang.String s)
    Allows to add a String to null.
    void setProperty​(java.lang.String name, java.lang.Object value)
    Tries to set a property on null, which fails.
    java.lang.String toString()  
    <T> T with​(Closure<T> closure)
    Allows the closure to be called for NullObject.

    Methods inherited from class groovy.lang.GroovyObjectSupport

    getMetaClass, setMetaClass

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • getNullObject

      public static NullObject getNullObject()
      Returns the NullObject reference.
      Returns:
      the null object
    • clone

      public java.lang.Object clone()
      Since this is implemented as a singleton, avoid the use of the clone method.
      Overrides:
      clone in class java.lang.Object
      Returns:
      never
      Throws:
      java.lang.NullPointerException
    • equals

      public boolean equals​(java.lang.Object o)
      null is only equal to null.
      Overrides:
      equals in class java.lang.Object
      Parameters:
      o - the reference object with which to compare
      Returns:
      true if this object is the same as the to argument
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class java.lang.Object
      Returns:
      never
      Throws:
      java.lang.NullPointerException
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
    • getProperty

      public java.lang.Object getProperty​(java.lang.String name)
      Tries to get a property on null, which fails.
      Parameters:
      name - the name of the property of interest
      Returns:
      never
      Throws:
      java.lang.NullPointerException
    • setProperty

      public void setProperty​(java.lang.String name, java.lang.Object value)
      Tries to set a property on null, which fails.
      Parameters:
      name - the name of the property of interest
      value - the new value for the property
      Throws:
      java.lang.NullPointerException
    • invokeMethod

      public java.lang.Object invokeMethod​(java.lang.String name, java.lang.Object arguments)
      Tries to invoke a method on null, which falis.
      Parameters:
      name - the name of the method to call
      arguments - the arguments to use for the method call
      Returns:
      never
      Throws:
      java.lang.NullPointerException
    • asBoolean

      public boolean asBoolean()
      A null object coerces to false.
      Returns:
      false
    • asType

      public java.lang.Object asType​(java.lang.Class c)
      Type conversion method for null.
      Returns:
      null
    • is

      public boolean is​(java.lang.Object o)
      Tests for equal references.
      Returns:
      true if object is null
    • iterator

      public java.util.Iterator iterator()
      Provides ability to iterate on null.
      Returns:
      an empty iterator
    • plus

      public java.lang.Object plus​(java.lang.Object o)
      Fallback for null+null. The plus(java.lang.String) variant catches the case of adding a non-null String to null.
      Returns:
      never
      Throws:
      java.lang.NullPointerException
    • plus

      public java.lang.Object plus​(java.lang.String s)
      Allows to add a String to null. The result is concatenated String of the result of calling toString() on this object and the String in the parameter.
      Returns:
      the concatenated string
    • with

      public <T> T with​(Closure<T> closure)
      Allows the closure to be called for NullObject.
      Parameters:
      closure - the closure to call on the object
      Returns:
      result of calling the closure