org.norther.tammi.spray.engine.tool
Class TypeTool

java.lang.Object
  extended by org.norther.tammi.core.pool.RecyclableSupport
      extended by org.norther.tammi.core.pool.PoolableSupport
          extended by org.norther.tammi.spray.engine.tool.GlobalTool
              extended by org.norther.tammi.spray.engine.tool.TypeTool
All Implemented Interfaces:
Serializable, Cloneable, EventListener, Poolable, Recyclable, ContextBindingListener

public class TypeTool
extends GlobalTool

A global context tool for type conversions.

Author:
Ilkka Priha
See Also:
Serialized Form

Constructor Summary
TypeTool()
          Constructs a new tool.
 
Method Summary
 String cite(String str)
          Returns a string within apostrophes.
 Collection counter()
          Returns an endless counter.
 Collection counter(int size)
          Returns a counter.
 String digest(String algorithm, String string)
          Computes a digest hash.
 String digest(String algorithm, String string, String encoding)
          Computes a digest hash applying the specified encoding.
 String digest(String algorithm, String salt, String string, String encoding)
          Computes a digest hash using the given salt and applying the specified encoding.
 boolean equals(Object object1, Object object2)
          Checks whether two objects are equal.
 String fromXML(Object object)
          Converts an XML encoded object to a text string.
 Object get(Object array, int index)
          Gets an indexed element.
 boolean isArray(Object object)
          Checks whether an object is an array.
 boolean isBoolean(Object object)
          Checks whether an object is a boolean.
 boolean isByte(Object object)
          Checks whether an object is a byte.
 boolean isChar(String str, int c)
          Checks whether a string represents the specified char.
 boolean isCharacter(Object object)
          Checks whether an object is a character.
 boolean isDate(String className)
          Checks whether the specified class name is date based.
 boolean isDouble(Object object)
          Checks whether an object is a doouble.
 boolean isFloat(Object object)
          Checks whether an object is a float.
 boolean isInstance(Object object, String className)
          Checks whether the specified object is an instance of the named class.
 boolean isInteger(Object object)
          Checks whether an object is an integer.
 boolean isLong(Object object)
          Checks whether an object is a long.
 boolean isNull(Object object)
          Checks whether an object is null.
 boolean isNumber(Object object)
          Checks whether an object is a number.
 boolean isNumeric(String className)
          Checks whether the specified class name is numeric.
 boolean isPrimitive(String className)
          Checks whether the specified class name is primitive.
 boolean isShort(Object object)
          Checks whether an object is a short.
 boolean isString(Object object)
          Checks whether an object is a string.
 boolean isString(String className)
          Checks whether the specified class name is character based.
 int length(Object object)
          Gets the length of an object.
 Object none()
          Returns a null.
 Map prefix(String prefix, Map map)
          Returns a map that is a prefixed subset of the given one.
 Map prefix(String prefix, String delims, Map map)
          Returns a map that is a prefixed subset of the given one.
 String printStackTrace(Throwable error)
          Prints the stack trace of a throwable.
 String quote(String str)
          Returns a string within quotation.
 String replace(String source, String from, String to)
          Replaces occurancies of the specified substring in a string to another substring.
 void set(Object array, int index, Object value)
          Sets an indexed element.
 Object sort(Object source)
          Sorts the specified source into ascending order.
 Object sort(Object source, Comparator comp)
          Sorts the specified source to the order induced by the specified comparator.
 Object sort(Object array, int fromIndex, int toIndex)
          Sorts the specified range of the specified array into ascending order.
 Object sort(Object source, String className)
          Sorts the specified source to the order induced by the specified comparator class.
 Map suffix(String suffix, Map map)
          Returns a map that is a suffixed subset of the given one.
 Map suffix(String suffix, String delims, Map map)
          Returns a map that is a suffixed subset of the given one.
 String toActions(int permission)
          Converts a permission mask into actions.
 Object toArray(Object object)
          Returns an object in an array.
 Object toArray(Object first, Object second)
          Returns two objects in an array.
 boolean toBoolean(Object obj)
          Converts a string into a boolean.
 StringBuffer toBuffer()
          Returns an empty buffer.
 StringBuffer toBuffer(Object object)
          Returns an initialized buffer.
 byte toByte(Object obj)
          Converts an object into a byte.
 char toChar(Object obj)
          Converts a string into a char.
protected  Class toClass(String className)
          Returns the class instance of the named class.
 Comparator toComparator(List attributes)
          Returns an attribute comparator for the specified attributes.
 double toDouble(Object obj)
          Converts a string into a double.
 float toFloat(Object obj)
          Converts a string into a float.
 int toInt(Object obj)
          Converts a string into an int.
 List toList()
          Returns an empty list.
 List toList(int size)
          Returns a sized list.
 List toList(Object object)
          Returns a collection, array or object as a list.
 long toLong(Object obj)
          Converts a string into a long.
 Map toMap()
          Returns an empty map.
 Object toObject(String className, String string)
          Converts a string into an object.
 Object toObject(String className, String string, String pattern)
          Converts a string into an object.
 OrderedMap toOrderedMap()
          Returns an empty ordered map.
 OrderedSet toOrderedSet()
          Returns an empty ordered set.
 Set toOrderedSet(Object object)
          Returns a collection, array or object as an ordered set.
 Paint toPaint(String color)
          Converts a named color to a paint.
 int toPermission(String actions)
          Converts an action string into a permission mask.
 String toPlainType(Class clazz)
          Converts a class to its plain type.
 String toPlainType(String className)
          Converts a qualified class name to its plain type.
 Set toSet()
          Returns an empty set.
 Set toSet(Object object)
          Returns a collection, array or object as a set.
 short toShort(Object obj)
          Converts a string into a short.
 OutputStream toStream()
          Returns a byte stream.
 String toString(Object object)
          Converts an object into a string.
 String toString(String className, Object object)
          Converts an object of the named class into a string.
 String toString(String className, Object object, String pattern)
          Converts an object of the named class into a string by applying a pattern.
 Stroke toStroke(String pattern)
          Converts a pattern to a stroke.
 String toXML(Object object)
          Converts an object to an XML encoded string.
 String toXML(String className, Object object)
          Converts an object to an XML encoded string.
 String toXML(String className, Object object, String pattern)
          Converts an object to an XML encoded string applying a pattern.
 OutputStream toZipStream(OutputStream out)
          Returns a zip stream.
 String wrap(String str)
          Returns a string wrapped by linefeeds.
 
Methods inherited from class org.norther.tammi.spray.engine.tool.GlobalTool
clone, dispose, getContext, getContextToolFilter, getMBeanServer, getName, isBound, nop, toString, valueBound, valueUnbound
 
Methods inherited from class org.norther.tammi.core.pool.PoolableSupport
disposed, recycled
 
Methods inherited from class org.norther.tammi.core.pool.RecyclableSupport
isDisposed, recycle
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.norther.tammi.core.pool.Recyclable
isDisposed, recycle
 

Constructor Detail

TypeTool

public TypeTool()
Constructs a new tool.

Method Detail

get

public Object get(Object array,
                  int index)
Gets an indexed element.

Parameters:
array - the array or list.
index - the index.
Returns:
the element or null if not found.

set

public void set(Object array,
                int index,
                Object value)
Sets an indexed element.

Parameters:
array - the array or list.
index - the index.
value - the value.

length

public int length(Object object)
Gets the length of an object.

Parameters:
object - the object.
Returns:
the object length.

prefix

public Map prefix(String prefix,
                  Map map)
Returns a map that is a prefixed subset of the given one. The key components must be delimited by periods.

Parameters:
prefix - a key prefix.
map - the source map.
Returns:
the subset map.

prefix

public Map prefix(String prefix,
                  String delims,
                  Map map)
Returns a map that is a prefixed subset of the given one.

Parameters:
prefix - a key prefix.
delims - key delimiters.
map - the source map.
Returns:
the subset map.

suffix

public Map suffix(String suffix,
                  Map map)
Returns a map that is a suffixed subset of the given one. The key components must be delimited by periods.

Parameters:
suffix - a key suffix.
map - the source map.
Returns:
the subset map.

suffix

public Map suffix(String suffix,
                  String delims,
                  Map map)
Returns a map that is a suffixed subset of the given one.

Parameters:
suffix - a key suffix.
delims - key delimiters.
map - the source map.
Returns:
the subset map.

replace

public String replace(String source,
                      String from,
                      String to)
Replaces occurancies of the specified substring in a string to another substring.

Parameters:
source - the source string.
from - the substring to replace from.
to - the substring to replace to.
Returns:
the replaced string.

isDate

public boolean isDate(String className)
Checks whether the specified class name is date based.

Parameters:
className - the class name to check.
Returns:
true for a date type, otherwise false.

isNumeric

public boolean isNumeric(String className)
Checks whether the specified class name is numeric.

Parameters:
className - the class name to check.
Returns:
true for a numeric type, otherwise false.

isString

public boolean isString(String className)
Checks whether the specified class name is character based.

Parameters:
className - the class name to check.
Returns:
true for a characted based type, otherwise false.

isPrimitive

public boolean isPrimitive(String className)
Checks whether the specified class name is primitive.

Parameters:
className - the class name to check.
Returns:
true for a primitive type, otherwise false.

isArray

public boolean isArray(Object object)
Checks whether an object is an array.

Parameters:
object - an object.
Returns:
true if an array, otherwise false.

isString

public boolean isString(Object object)
Checks whether an object is a string.

Parameters:
object - an object.
Returns:
true if a string, otherwise false.

isNumber

public boolean isNumber(Object object)
Checks whether an object is a number.

Parameters:
object - an object.
Returns:
true if a number, otherwise false.

isByte

public boolean isByte(Object object)
Checks whether an object is a byte.

Parameters:
object - an object.
Returns:
true if a byte, otherwise false.

isCharacter

public boolean isCharacter(Object object)
Checks whether an object is a character.

Parameters:
object - an object.
Returns:
true if a character, otherwise false.

isShort

public boolean isShort(Object object)
Checks whether an object is a short.

Parameters:
object - an object.
Returns:
true if a short, otherwise false.

isInteger

public boolean isInteger(Object object)
Checks whether an object is an integer.

Parameters:
object - an object.
Returns:
true if an integer, otherwise false.

isLong

public boolean isLong(Object object)
Checks whether an object is a long.

Parameters:
object - an object.
Returns:
true if a long, otherwise false.

isFloat

public boolean isFloat(Object object)
Checks whether an object is a float.

Parameters:
object - an object.
Returns:
true if a float, otherwise false.

isDouble

public boolean isDouble(Object object)
Checks whether an object is a doouble.

Parameters:
object - an object.
Returns:
true if a double, otherwise false.

isBoolean

public boolean isBoolean(Object object)
Checks whether an object is a boolean.

Parameters:
object - an object.
Returns:
true if a boolean, otherwise false.

isNull

public boolean isNull(Object object)
Checks whether an object is null.

Parameters:
object - an object.
Returns:
true if null, otherwise false.

isInstance

public boolean isInstance(Object object,
                          String className)
Checks whether the specified object is an instance of the named class.

Parameters:
object - an object.
className - the class name.
Returns:
true if an instance, false otherwise.

isChar

public boolean isChar(String str,
                      int c)
Checks whether a string represents the specified char.

Parameters:
str - the string.
c - the char.
Returns:
true if a char, false otherwise.

equals

public boolean equals(Object object1,
                      Object object2)
Checks whether two objects are equal. Array elements are compared one by one, if both objects are arrays. Numbers are compared as doubles.

Parameters:
object1 - the first object.
object2 - the second object.
Returns:
true if objects are equal, otherwise false.

none

public Object none()
Returns a null.

Returns:
always null.

toByte

public byte toByte(Object obj)
Converts an object into a byte.

Parameters:
obj - the object.
Returns:
the byte.

toChar

public char toChar(Object obj)
Converts a string into a char.

Parameters:
obj - the object.
Returns:
the char.

toShort

public short toShort(Object obj)
Converts a string into a short.

Parameters:
obj - the object.
Returns:
the short.

toInt

public int toInt(Object obj)
Converts a string into an int.

Parameters:
obj - the object.
Returns:
the int.

toLong

public long toLong(Object obj)
Converts a string into a long.

Parameters:
obj - the object.
Returns:
the long.

toFloat

public float toFloat(Object obj)
Converts a string into a float.

Parameters:
obj - the object.
Returns:
the float.

toDouble

public double toDouble(Object obj)
Converts a string into a double.

Parameters:
obj - the object.
Returns:
the double.

toBoolean

public boolean toBoolean(Object obj)
Converts a string into a boolean.

Parameters:
obj - the object.
Returns:
the boolean.

toString

public String toString(Object object)
Converts an object into a string.

Parameters:
object - the object to convert.
Returns:
the converted string.

toString

public String toString(String className,
                       Object object)
Converts an object of the named class into a string.

Parameters:
className - the source class name.
object - the object to convert.
Returns:
the converted string.

toString

public String toString(String className,
                       Object object,
                       String pattern)
Converts an object of the named class into a string by applying a pattern.

Parameters:
className - the source class name.
object - the object to convert.
pattern - the conversion pattern.
Returns:
the converted string.

toObject

public Object toObject(String className,
                       String string)
Converts a string into an object.

Parameters:
className - the target class name.
string - the string to convert.
Returns:
the converted object.

toObject

public Object toObject(String className,
                       String string,
                       String pattern)
Converts a string into an object.

Parameters:
className - the target class name.
string - the string to convert.
pattern - the conversion pattern.
Returns:
the converted object.

toActions

public String toActions(int permission)
Converts a permission mask into actions.

Parameters:
permission - the permission to convert.
Returns:
the action string.

toPlainType

public String toPlainType(Class clazz)
Converts a class to its plain type.

Parameters:
clazz - the class.
Returns:
the plain type.

toPlainType

public String toPlainType(String className)
Converts a qualified class name to its plain type.

Parameters:
className - the qualified class name.
Returns:
the plain type.

toPermission

public int toPermission(String actions)
Converts an action string into a permission mask.

Parameters:
actions - the actions to convert.
Returns:
the action mask.

counter

public Collection counter()
Returns an endless counter.

Returns:
the counter.

counter

public Collection counter(int size)
Returns a counter.

Parameters:
size - the size of the counter.
Returns:
the counter.

sort

public Object sort(Object source)
Sorts the specified source into ascending order.

Parameters:
source - the source to sort.
Returns:
the sorted source.

sort

public Object sort(Object source,
                   Comparator comp)
Sorts the specified source to the order induced by the specified comparator.

Parameters:
source - the source to sort.
comp - the comparator.
Returns:
the sorted source.

sort

public Object sort(Object source,
                   String className)
Sorts the specified source to the order induced by the specified comparator class.

Parameters:
source - the source to sort.
className - the class name of the comparator.
Returns:
the sorted source.

sort

public Object sort(Object array,
                   int fromIndex,
                   int toIndex)
Sorts the specified range of the specified array into ascending order.

Parameters:
array - the array to sort.
fromIndex - the indext to start from.
toIndex - the index to end to.
Returns:
the original array sorted in place.

toComparator

public Comparator toComparator(List attributes)
Returns an attribute comparator for the specified attributes.

Parameters:
attributes - a list of attributes.
Returns:
the attribute comparator.

toOrderedMap

public OrderedMap toOrderedMap()
Returns an empty ordered map.

Returns:
an empty ordered map.

toOrderedSet

public OrderedSet toOrderedSet()
Returns an empty ordered set.

Returns:
an empty ordered set.

toOrderedSet

public Set toOrderedSet(Object object)
Returns a collection, array or object as an ordered set.

Parameters:
object - the object.
Returns:
an ordered set of items.

toMap

public Map toMap()
Returns an empty map.

Returns:
an empty map.

toSet

public Set toSet()
Returns an empty set.

Returns:
an empty set.

toSet

public Set toSet(Object object)
Returns a collection, array or object as a set.

Parameters:
object - the object.
Returns:
a set of items.

toList

public List toList()
Returns an empty list.

Returns:
an empty list.

toList

public List toList(int size)
Returns a sized list.

Parameters:
size - the size.
Returns:
a sized list.

toList

public List toList(Object object)
Returns a collection, array or object as a list.

Parameters:
object - the object.
Returns:
a list of itemss.

toBuffer

public StringBuffer toBuffer()
Returns an empty buffer.

Returns:
an empty buffer.

toBuffer

public StringBuffer toBuffer(Object object)
Returns an initialized buffer.

Parameters:
object - the initial object.
Returns:
an initialized buffer.

toStream

public OutputStream toStream()
Returns a byte stream.

Returns:
an empty byte stream.

toZipStream

public OutputStream toZipStream(OutputStream out)
Returns a zip stream.

Parameters:
out - the output stream.
Returns:
an empty zip stream.

toArray

public Object toArray(Object object)
Returns an object in an array.

Parameters:
object - the object.
Returns:
an array containing the object.

toArray

public Object toArray(Object first,
                      Object second)
Returns two objects in an array. If any or both of them are arrays themselves, the result array merges the original ones.

Parameters:
first - the first object.
second - the second object.
Returns:
an array of objects.

toClass

protected Class toClass(String className)
Returns the class instance of the named class.

Parameters:
className - the class name to load.
Returns:
the loaded class or null.

toXML

public String toXML(Object object)
Converts an object to an XML encoded string.

Parameters:
object - the object to convert.
Returns:
an XML string.

toXML

public String toXML(String className,
                    Object object)
Converts an object to an XML encoded string.

Parameters:
className - the source class name.
object - the object to convert.
Returns:
the converted string.

toXML

public String toXML(String className,
                    Object object,
                    String pattern)
Converts an object to an XML encoded string applying a pattern.

Parameters:
className - the source class name.
object - the object to convert.
pattern - the conversion pattern.
Returns:
the converted string.

fromXML

public String fromXML(Object object)
Converts an XML encoded object to a text string.

Parameters:
object - the object to convert.
Returns:
an XML string.

toPaint

public Paint toPaint(String color)
Converts a named color to a paint.

Parameters:
color - the name of the color.
Returns:
the corresponding paint.

toStroke

public Stroke toStroke(String pattern)
Converts a pattern to a stroke. The pattern is a comma separated list starting with the width followed by dash lenghts.

Parameters:
pattern - the stroke pattern.
Returns:
the corresponding stroke.

cite

public String cite(String str)
Returns a string within apostrophes.

Parameters:
str - the string.
Returns:
the cited string.

quote

public String quote(String str)
Returns a string within quotation.

Parameters:
str - the string.
Returns:
the quoted string.

wrap

public String wrap(String str)
Returns a string wrapped by linefeeds.

Parameters:
str - the string.
Returns:
the wrapped string.

digest

public String digest(String algorithm,
                     String string)
Computes a digest hash.

Parameters:
algorithm - the digest algorithm.
string - the string to use.
Returns:
the encoded hash or null if failed.

digest

public String digest(String algorithm,
                     String string,
                     String encoding)
Computes a digest hash applying the specified encoding.

Parameters:
algorithm - the digest algorithm.
string - the string to use.
encoding - the character encoding.
Returns:
the encoded hash or null if failed.

digest

public String digest(String algorithm,
                     String salt,
                     String string,
                     String encoding)
Computes a digest hash using the given salt and applying the specified encoding.

Parameters:
algorithm - the digest algorithm.
salt - the salt to use.
string - the string to use.
encoding - the character encoding.
Returns:
the encoded hash or null if failed.

printStackTrace

public String printStackTrace(Throwable error)
Prints the stack trace of a throwable.

Parameters:
error - the throwable.
Returns:
the stack trace.


Copyright © 2004 The Norther Organization. All rights reserved.