bpp
Class Format

java.lang.Object
  extended bybpp.Format

public class Format
extends java.lang.Object

This class simplifies the generation of java source code by the preprocessor.


The Beanshell Preprocessor
Copyright (C) 2003 Warren MacEvoy


Field Summary
static java.util.Map DEFAULTS
          Default initial values for primitives.
static java.util.Map PRIMITIVES
          Maps wrapped name to primitive name.
static java.util.Map WRAPPERS
          Maps primitive names to wrapped names.
 
Constructor Summary
Format()
           
 
Method Summary
static java.lang.String defval(java.lang.String type)
          Get default value for any type (as a string).
static void encodeTo(char x, java.lang.StringBuffer to)
          Format a single char into a StringBuffer as part of a char or String literal.
static java.lang.String getPrimitive(java.lang.String type)
          getPrimitive("Character")="char" and getPrimitive("java.lang.Integer")="int".
static java.lang.String getWrapper(java.lang.String type)
          getWrapper("char")="Character".
static boolean isPrimitive(java.lang.String type)
           
static boolean isWrapper(java.lang.String type)
          
static java.lang.String join(char separator, java.util.Collection c)
          Join the toString() representations of some objects together.
static java.lang.String join(char separator, java.lang.Object[] list)
          Join the toString() representations of some objects together.
static java.lang.String join(java.lang.String separator, java.util.Collection c)
          Join the toString() representations of some objects together.
static java.lang.String join(java.lang.String separator, java.lang.Object[] list)
          Join the toString() representations of some objects together.
static java.lang.String left(java.lang.String s, int w)
          Left justify s in a width of w chars and a pad of ' '.
static java.lang.String left(java.lang.String s, int w, char pad)
          Left justify format in a width w using the given pad char.
static java.lang.String literal(boolean x)
          Format boolean as a java literal.
static java.lang.String literal(java.lang.Boolean x)
          Format Boolean as a java literal.
static java.lang.String literal(byte x)
          /**
static java.lang.String literal(java.lang.Byte x)
          Format Byte as a java literal.
static java.lang.String literal(char x)
          Format char as a java literal.
static java.lang.String literal(java.lang.Character x)
          Format char as a java literal.
static java.lang.String literal(double x)
          Format double as a java literal.
static java.lang.String literal(java.lang.Double x)
          Format Double as a java literal.
static java.lang.String literal(float x)
          Format float as a java literal.
static java.lang.String literal(java.lang.Float x)
          Format float as a java literal.
static java.lang.String literal(int x)
          Format int as a java literal.
static java.lang.String literal(java.lang.Integer x)
          Format Integer as a java literal.
static java.lang.String literal(long x)
          Format long as a java literal.
static java.lang.String literal(java.lang.Long x)
          Format Long as a java literal.
static java.lang.String literal(short x)
          Format short as a java literal.
static java.lang.String literal(java.lang.Short x)
          Format short as a java literal.
static java.lang.String literal(java.lang.String s)
          Format String as a java literal.
static java.lang.String N(double n, java.lang.String f)
          Format number.
static java.lang.String N(java.lang.Number n, java.lang.String f)
          Format wrapped number.
static java.lang.String right(java.lang.String s, int w)
          Right justify s with blanks for a pad character.
static java.lang.String right(java.lang.String s, int w, char pad)
          Right justify s in a width w with given pad char.
static java.lang.String[] split(char separator, java.lang.String joined, boolean trim)
          Split a string up.
static java.lang.String[] split(java.lang.String separator, java.lang.String joined, boolean trim)
          Split a string up.
static java.lang.String typename(java.lang.Class c)
          Get correct name of a class
static java.lang.String typeof(boolean x)
          Simple "typeof" operator for boolean primitives.
static java.lang.String typeof(byte x)
          Simple "typeof" operator for byte primitives.
static java.lang.String typeof(char x)
          Simple "typeof" operator for char primitives.
static java.lang.String typeof(double x)
          Simple "typeof" operator for double primitives.
static java.lang.String typeof(float x)
          Simple "typeof" operator for float primitives.
static java.lang.String typeof(int x)
          Simple "typeof" operator for int primitives.
static java.lang.String typeof(long x)
          Simple "typeof" operator for long primitives.
static java.lang.String typeof(java.lang.Object o)
          Simple "typeof" operator for Objects
static java.lang.String typeof(short x)
          Simple "typeof" operator for short primitives.
static java.lang.String unwrap(java.lang.String type, java.lang.String exp)
          Selectively unwrap primitive types.
static java.lang.String wrap(java.lang.String type, java.lang.String exp)
          Selectively wrap primitive types.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WRAPPERS

public static final java.util.Map WRAPPERS
Maps primitive names to wrapped names. For example, WRAPPERS.get("int")="Integer".


PRIMITIVES

public static final java.util.Map PRIMITIVES
Maps wrapped name to primitive name. For example, PRIMITIVES.get("Integer")="int".


DEFAULTS

public static final java.util.Map DEFAULTS

Default initial values for primitives. It goes from the name of the primitive to a wrapped version of the default value for that primitive type. The specific entries are:

Constructor Detail

Format

public Format()
Method Detail

isPrimitive

public static boolean isPrimitive(java.lang.String type)

isWrapper

public static boolean isWrapper(java.lang.String type)

Long (java.lang.Integer) or short (Integer) names are ok.


getWrapper

public static java.lang.String getWrapper(java.lang.String type)
getWrapper("char")="Character". If the type argument is not a primitive type name, like "String" or "Boolean", getWrapper() returns null.


getPrimitive

public static java.lang.String getPrimitive(java.lang.String type)
getPrimitive("Character")="char" and getPrimitive("java.lang.Integer")="int". If the type argument is not a primitive wrapper type name, like "String" or "boolean", getPrimitive() returns null.


wrap

public static java.lang.String wrap(java.lang.String type,
                                    java.lang.String exp)

Selectively wrap primitive types.

For example, wrap("String","x") will produce "x", while wrap("boolean","x") will produce "new Boolean(x)". The type name must be the primitive type.


unwrap

public static java.lang.String unwrap(java.lang.String type,
                                      java.lang.String exp)

Selectively unwrap primitive types.

For example, unwrap("String","x") will produce "x", while unwrap("boolean","x") will produce "x.booleanValue()". The type name must be the primitive type.


defval

public static java.lang.String defval(java.lang.String type)

Get default value for any type (as a string).

For example, defval("String") will produce "((String)null)". All non-primitive types, including wrapper types, are treated in this way. The primitive types produce the following values:

To initialize a wrapper type, use wrap(primitive,defval(primitive)).


typename

public static java.lang.String typename(java.lang.Class c)

Get correct name of a class

This is better than using the java.lang.Class.getName directly, because that method returns strange names for array types.


typeof

public static java.lang.String typeof(java.lang.Object o)

Simple "typeof" operator for Objects

Returns typename(o.getClass())


typeof

public static java.lang.String typeof(boolean x)

Simple "typeof" operator for boolean primitives. Returns "boolean".


typeof

public static java.lang.String typeof(byte x)

Simple "typeof" operator for byte primitives. Returns "byte".


typeof

public static java.lang.String typeof(char x)

Simple "typeof" operator for char primitives. Returns "char".


typeof

public static java.lang.String typeof(short x)

Simple "typeof" operator for short primitives. Returns "short".


typeof

public static java.lang.String typeof(int x)

Simple "typeof" operator for int primitives. Returns "int".


typeof

public static java.lang.String typeof(long x)

Simple "typeof" operator for long primitives. Returns "long".


typeof

public static java.lang.String typeof(float x)

Simple "typeof" operator for float primitives. Returns "float".


typeof

public static java.lang.String typeof(double x)

Simple "typeof" operator for double primitives. Returns "double".


join

public static java.lang.String join(char separator,
                                    java.util.Collection c)
Join the toString() representations of some objects together.


join

public static java.lang.String join(java.lang.String separator,
                                    java.util.Collection c)
Join the toString() representations of some objects together.


join

public static java.lang.String join(char separator,
                                    java.lang.Object[] list)
Join the toString() representations of some objects together.


join

public static java.lang.String join(java.lang.String separator,
                                    java.lang.Object[] list)
Join the toString() representations of some objects together.


split

public static java.lang.String[] split(char separator,
                                       java.lang.String joined,
                                       boolean trim)

Split a string up.

After splitting by the given separator, each entry is trimmed (with String.trim()) if the trim argument is true. For example:

  split(',',",1, 3, 4",false)={"","1"," 3"," 4"}
  
while
  split(',',",1, 3, 4",true)={"","1","3","4"}
  


split

public static java.lang.String[] split(java.lang.String separator,
                                       java.lang.String joined,
                                       boolean trim)

Split a string up.

Just like the char-separator version of split.


left

public static java.lang.String left(java.lang.String s,
                                    int w,
                                    char pad)

Left justify format in a width w using the given pad char.

If s is longer than abs(w), it is not trimmed. If w is negative, s is right justified instead.


left

public static java.lang.String left(java.lang.String s,
                                    int w)
Left justify s in a width of w chars and a pad of ' '.


right

public static java.lang.String right(java.lang.String s,
                                     int w,
                                     char pad)
Right justify s in a width w with given pad char.


right

public static java.lang.String right(java.lang.String s,
                                     int w)
Right justify s with blanks for a pad character.


N

public static java.lang.String N(java.lang.Number n,
                                 java.lang.String f)

Format wrapped number.

f is the argument to the java.text.DecimalFormat used to format the number. See that class for all that can be done


N

public static java.lang.String N(double n,
                                 java.lang.String f)

Format number.

f is the argument to the java.text.DecimalFormat used to format the number. See that class for all that can be done


literal

public static java.lang.String literal(boolean x)

Format boolean as a java literal.

For example, literal(true)="true".


literal

public static java.lang.String literal(java.lang.Boolean x)

Format Boolean as a java literal.

For example, literal(new Boolean(true))="true".


literal

public static java.lang.String literal(byte x)
/**

Format byte as a java literal.

For example, literal((byte) 3)="((byte) 3)".


literal

public static java.lang.String literal(java.lang.Byte x)

Format Byte as a java literal.

For example, literal(new Byte(3))="((byte) 3)".


literal

public static java.lang.String literal(short x)

Format short as a java literal.

For example, literal((short) 3)="((short) 3)".


literal

public static java.lang.String literal(java.lang.Short x)

Format short as a java literal.

For example, literal(new Short(3))="((short) 3)".


literal

public static java.lang.String literal(int x)

Format int as a java literal.

For example, literal(3)="3".


literal

public static java.lang.String literal(java.lang.Integer x)

Format Integer as a java literal.

For example, literal(new Integer(3))="3".


literal

public static java.lang.String literal(long x)

Format long as a java literal.

For example, literal(3L)="3L".


literal

public static java.lang.String literal(java.lang.Long x)

Format Long as a java literal.

For example, literal(new Long(3L))="3L".


literal

public static java.lang.String literal(float x)

Format float as a java literal.

For example, literal(3.0f))="3.0f".


literal

public static java.lang.String literal(java.lang.Float x)

Format float as a java literal.

For example, literal(new Float(3.0f)))="3.0f".


literal

public static java.lang.String literal(double x)

Format double as a java literal.

For example, literal(3.0))="3.0".


literal

public static java.lang.String literal(java.lang.Double x)

Format Double as a java literal.

For example, literal(new Double(3.0)))="3.0".


encodeTo

public static void encodeTo(char x,
                            java.lang.StringBuffer to)
Format a single char into a StringBuffer as part of a char or String literal. Normally this is only used as part of literal(char) and literal(String)


literal

public static java.lang.String literal(char x)

Format char as a java literal.

For example, literal('\'')="\'\\'\'".


literal

public static java.lang.String literal(java.lang.Character x)

Format char as a java literal.

For example, literal(new Character('\''))="\'\\\'\'".


literal

public static java.lang.String literal(java.lang.String s)

Format String as a java literal.

For example, literal("\tPress \'return\'\n")="\"\\tPress \\\'return\\\'\\n\"".