org.nakedobjects.object
Class Title

java.lang.Object
  extended byorg.nakedobjects.object.Title

public class Title
extends java.lang.Object

Title objects are used to create title strings for labelling and titling object. This usually involves the titles of contained objects and as this are often null lots of checking and concatenation of spaces is required. This utility class simplifies the process by taken care of these issues.

Two basic methods are provided concat and append. Concat appends the specified text or text from a title to the immediate end of the text held by the object. Append starts of by appended a space if required (there is already existing text) and then the specified text or text from a title. Each of these methods return the called object reference so the method can be chained together.

The following code serves as an example.

   TextString name = new TextString("Fred Smith");
   TextString addr = new TextString("Larch Road");
   TextString addr2 = new TextString();
   NakedObject lot = null;
   System.out.println(new Title(name));
   //append
   System.out.println(new Title().append(name));
   System.out.println(new Title("Name:").append(name));
   System.out.println(new Title(name).append(",", addr));
   System.out.println(name.title().append(",", addr));
   System.out.println(new Title(name).append(",", addr).append(",", addr2));
   System.out.println(new Title(name).append(" Lot:", lot, "none"));
   //concat
   System.out.println(new Title("Name:").concat(name));
   System.out.println(new Title(name).concat(" & ").concat(lot, "none"));
   System.out.println(new Title(name).concat(",").concat(addr));
   

And produces the following output:-

   Fred Smith
   Fred Smith
   Name: Fred Smith
   Fred Smith, Larch Road
   Fred Smith, Larch Road
   Fred Smith, Larch Road
   Fred Smith Lot: none
   Name:Fred Smith
   Fred Smith & none
   Fred Smith,Larch Road
   


Constructor Summary
Title()
          Creates a new Title objects with no text in it.
Title(Naked object)
          Create a new Title object containing the title of the specified object.
Title(Naked object, java.lang.String defaultValue)
          Create a new Title object containing the title of the specified object or the default text if the object reference is null
Title(java.lang.String text)
          Create a new Title object containing the specified text.
 
Method Summary
 Title append(int number)
           
 Title append(Naked object)
          Appends the title text of the specified object.
 Title append(Naked object, java.lang.String defaultValue)
          Appends the title of the specified object, or the specified text if the objects title is null, and prepends a space if there is already some text in this title object.
 Title append(java.lang.String text)
          Appends a space (if there is already some text in this title object) and then the specified text.
 Title append(java.lang.String joiner, Naked object)
          Append the joiner text, a space, and the title of the specified naked object (object) (got by calling the objects title() method) to the text of this Title object.
 Title append(java.lang.String joiner, Naked object, java.lang.String defaultValue)
          Append the joiner text, a space, and the title of the specified naked object (object) (got by calling the objects title() method) to the text of this Title object.
 Title append(java.lang.String joiner, java.lang.String text)
          Appends the joiner text, a space, and the text to the text of this Title object.
 Title appendSpace()
          Append a space to the text of this Title object if, and only if, there is some existing text i.e., a space is only added to existing text and will not create a text entry consisting of only one space.
 Title concat(Naked object)
          Concatenate the the title value (the result of calling an objects title() method) to this Title object.
 Title concat(Naked object, java.lang.String defaultValue)
          Concatenate the the title value (the result of calling an objects title() method), or the specified default value if the title is equal to null or is empty, to this Title object.
 Title concat(java.lang.String text)
          Concatenate the specified text on to the end of the text of this Title.
 Title concat(java.lang.String joiner, Naked object)
           
 Title concat(java.lang.String joiner, Naked object, java.lang.String defaultValue)
           
 java.lang.String toString()
          Returns a String that represents the value of this object.
 Title truncate(int noWords)
          Truncates this title so it has a maximum number of words.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Title

public Title()
Creates a new Title objects with no text in it.


Title

public Title(java.lang.String text)
Create a new Title object containing the specified text.

Parameters:
text - for the title to contain.

Title

public Title(Naked object)
Create a new Title object containing the title of the specified object.

Parameters:
object - the object who's title text to copy

Title

public Title(Naked object,
             java.lang.String defaultValue)
Create a new Title object containing the title of the specified object or the default text if the object reference is null

Parameters:
object - the object who's title is to be used.
defaultValue - text for the title to be set to if the former parameter is null.
Method Detail

append

public Title append(int number)

append

public Title append(java.lang.String text)
Appends a space (if there is already some text in this title object) and then the specified text.

Returns:
a reference to the called object (itself).

append

public Title append(java.lang.String joiner,
                    java.lang.String text)
Appends the joiner text, a space, and the text to the text of this Title object. If no text yet exists in the object then the joiner text and space are omiited.

Returns:
a reference to the called object (itself).

append

public Title append(java.lang.String joiner,
                    Naked object)
Append the joiner text, a space, and the title of the specified naked object (object) (got by calling the objects title() method) to the text of this Title object. If the title of the specified object is null then use the defaultValue text.

Parameters:
joiner - text to append before the title
object - object whose title needs to be appended
Returns:
a reference to the called object (itself).

append

public Title append(java.lang.String joiner,
                    Naked object,
                    java.lang.String defaultValue)
Append the joiner text, a space, and the title of the specified naked object (object) (got by calling the objects title() method) to the text of this Title object. If the title of the specified object is null then use the defaultValue text. If both the objects title and the default value are null or equate to a zero-length string then no text will be appended ; not even the joiner text.

Parameters:
joiner - text to append before the title
object - object whose title needs to be appended
defaultValue - the text to use if the the object's title is null.
Returns:
a reference to the called object (itself).

append

public Title append(Naked object)
Appends the title text of the specified object. If object or object.title() is null or the object.title().toString() returns an empty string then nothing will be appended.

Parameters:
object - the object whose we want to append to this title object.
Returns:
a reference to the called object (itself).

append

public Title append(Naked object,
                    java.lang.String defaultValue)
Appends the title of the specified object, or the specified text if the objects title is null, and prepends a space if there is already some text in this title object.

Parameters:
object - the object whose title is to be appended to this title.
defaultValue - a textual value to be used if the object's title is null.
Returns:
a reference to the called object (itself).

appendSpace

public Title appendSpace()
Append a space to the text of this Title object if, and only if, there is some existing text i.e., a space is only added to existing text and will not create a text entry consisting of only one space.

Returns:
a reference to the called object (itself).

concat

public final Title concat(java.lang.String text)
Concatenate the specified text on to the end of the text of this Title.

Parameters:
text - text to append
Returns:
a reference to the called object (itself).

concat

public final Title concat(java.lang.String joiner,
                          Naked object)

concat

public final Title concat(Naked object)
Concatenate the the title value (the result of calling an objects title() method) to this Title object. If the value is null the no text is added.

Parameters:
object - the naked object to get a title from
Returns:
a reference to the called object (itself).

concat

public final Title concat(java.lang.String joiner,
                          Naked object,
                          java.lang.String defaultValue)

concat

public final Title concat(Naked object,
                          java.lang.String defaultValue)
Concatenate the the title value (the result of calling an objects title() method), or the specified default value if the title is equal to null or is empty, to this Title object.

Parameters:
object - the naked object to get a title from
defaultValue - the default text to use when the naked object is null
Returns:
a reference to the called object (itself).

toString

public java.lang.String toString()
Returns a String that represents the value of this object.

Returns:
a string representation of the receiver

truncate

public Title truncate(int noWords)
Truncates this title so it has a maximum number of words. Spaces are used to determine words, thus two spaces in a title will cause two words to be mistakenly identified.

Parameters:
noWords - the number of words to show
Returns:
a reference to the called object (itself).