Common utilities and foundational infrastructure for the Errai GWT-based framework
—
Core type conversion system for converting between Java types, with built-in support for collections, numbers, and extensible custom type handlers.
Central factory for type conversion operations with pre-configured handlers for common conversions.
/**
* Factory for type conversion handlers with built-in handlers for collections and numbers
*/
public class TypeHandlerFactory {
/**
* Convert value between types using registered handlers
* @param from - Source type class
* @param to - Target type class
* @param value - Value to convert
* @return Converted value of target type
*/
public static <T> T convert(Class from, Class<? extends T> to, Object value);
/**
* Get available type handlers for source type
* @param from - Source type class
* @return Map of target types to their handlers
*/
public static Map<Class, TypeHandler> getHandler(Class from);
/**
* Register custom type handler
* @param from - Source type class
* @param to - Target type class
* @param handler - Handler implementation
*/
public static void addHandler(Class from, Class to, TypeHandler handler);
}Usage Examples:
import org.jboss.errai.common.client.types.TypeHandlerFactory;
// Convert string to integer
Integer result = TypeHandlerFactory.convert(String.class, Integer.class, "123");
// Convert collection to array
List<String> list = Arrays.asList("a", "b", "c");
String[] array = TypeHandlerFactory.convert(Collection.class, String[].class, list);
// Convert number to date
Number timestamp = 1234567890000L;
Date date = TypeHandlerFactory.convert(Number.class, Date.class, timestamp);Base interface for implementing custom type conversions.
/**
* Interface for converting between types
* @param <V> - Source value type
* @param <T> - Target type
*/
public interface TypeHandler<V, T> {
/**
* Convert input value to target type
* @param in - Input value of source type
* @return Converted value of target type
*/
public T getConverted(V in);
}Usage Example:
import org.jboss.errai.common.client.types.TypeHandler;
import org.jboss.errai.common.client.types.TypeHandlerFactory;
// Custom handler for converting strings to custom objects
public class StringToCustomObject implements TypeHandler<String, CustomObject> {
public CustomObject getConverted(String in) {
return new CustomObject(in);
}
}
// Register the handler
TypeHandlerFactory.addHandler(String.class, CustomObject.class, new StringToCustomObject());
// Use the handler
CustomObject obj = TypeHandlerFactory.convert(String.class, CustomObject.class, "input");Utility for converting GWT JSONValue objects to typed Java objects using the type conversion system.
/**
* Utility for converting JSONValue objects to typed Java objects
*/
public class JSONTypeHelper {
/**
* Convert JSONValue to specified type using type handlers
* @param value - JSONValue to convert
* @param to - Target type class
* @return Converted object of target type
*/
public static <T> T convert(JSONValue value, Class<? extends T> to);
/**
* Helper method for encoding objects to JSON-compatible strings
* @param v - Object to encode
* @return JSON-compatible string representation
*/
public static String encodeHelper(Object v);
}Usage Example:
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import org.jboss.errai.common.client.types.JSONTypeHelper;
// Parse JSON and convert to typed object
JSONValue jsonValue = JSONParser.parseStrict("[1, 2, 3]");
List<Integer> list = JSONTypeHelper.convert(jsonValue, List.class);
// Encode helper
String encoded = JSONTypeHelper.encodeHelper("hello"); // Returns "\"hello\""Pre-configured handlers for converting collections to various array and collection types.
Available Collection Handlers:
Collection → boolean[] (CollectionToBooleanArray)Collection → byte[] (CollectionToByteArray)Collection → char[] (CollectionToCharArray)Collection → double[] (CollectionToDoubleArray)Collection → float[] (CollectionToFloatArray)Collection → int[] (CollectionToIntArray)Collection → long[] (CollectionToLongArray)Collection → String[] (CollectionToStringArray)Collection → Object[] (CollectionToObjArray)Collection → List (CollectionToList)Collection → Set (CollectionToSet)Usage Example:
import java.util.*;
import org.jboss.errai.common.client.types.TypeHandlerFactory;
List<String> stringList = Arrays.asList("a", "b", "c");
// Convert to string array
String[] stringArray = TypeHandlerFactory.convert(Collection.class, String[].class, stringList);
// Convert to set
Set<String> stringSet = TypeHandlerFactory.convert(Collection.class, Set.class, stringList);Pre-configured handlers for converting Number objects to specific numeric types and dates.
Available Number Handlers:
Number → Integer (NumberToInt)Number → Long (NumberToLong)Number → Short (NumberToShort)Number → Float (NumberToFloat)Number → Double (NumberToDouble)Number → Byte (NumberToByte)Number → java.util.Date (NumberToDate)Number → java.sql.Date (NumberToSQLDate)Usage Example:
import org.jboss.errai.common.client.types.TypeHandlerFactory;
import java.util.Date;
Double doubleValue = 1234567890000.0;
// Convert to integer
Integer intValue = TypeHandlerFactory.convert(Number.class, Integer.class, doubleValue);
// Convert to date (from timestamp)
Date date = TypeHandlerFactory.convert(Number.class, Date.class, doubleValue);Limited set of handlers for primitive type conversions.
Available Primitive Handlers:
Integer → Byte (IntToByte)The type handler factory includes numerous built-in implementations that can be accessed directly if needed:
// Available Number conversion handler classes (all public)
public class NumberToInt implements TypeHandler<Number, Integer> {
public Integer getConverted(Number in);
}
public class NumberToLong implements TypeHandler<Number, Long> {
public Long getConverted(Number in);
}
public class NumberToShort implements TypeHandler<Number, Short> {
public Short getConverted(Number in);
}
public class NumberToFloat implements TypeHandler<Number, Float> {
public Float getConverted(Number in);
}
public class NumberToDouble implements TypeHandler<Number, Double> {
public Double getConverted(Number in);
}
public class NumberToByte implements TypeHandler<Number, Byte> {
public Byte getConverted(Number in);
}
public class NumberToDate implements TypeHandler<Number, java.util.Date> {
public java.util.Date getConverted(Number in);
}
public class NumberToSQLDate implements TypeHandler<Number, java.sql.Date> {
public java.sql.Date getConverted(Number in);
}// Available Collection conversion handler classes (all public)
public class CollectionToList implements TypeHandler<Collection, List> {
public List getConverted(Collection in);
}
public class CollectionToSet implements TypeHandler<Collection, Set> {
public Set getConverted(Collection in);
}
public class CollectionToObjArray implements TypeHandler<Collection, Object[]> {
public Object[] getConverted(Collection in);
}
public class CollectionToStringArray implements TypeHandler<Collection, String[]> {
public String[] getConverted(Collection in);
}
public class CollectionToIntArray implements TypeHandler<Collection, int[]> {
public int[] getConverted(Collection in);
}
public class CollectionToLongArray implements TypeHandler<Collection, long[]> {
public long[] getConverted(Collection in);
}
public class CollectionToBooleanArray implements TypeHandler<Collection, boolean[]> {
public boolean[] getConverted(Collection in);
}
public class CollectionToDoubleArray implements TypeHandler<Collection, double[]> {
public double[] getConverted(Collection in);
}
public class CollectionToFloatArray implements TypeHandler<Collection, float[]> {
public float[] getConverted(Collection in);
}
public class CollectionToByteArray implements TypeHandler<Collection, byte[]> {
public byte[] getConverted(Collection in);
}
public class CollectionToCharArray implements TypeHandler<Collection, char[]> {
public char[] getConverted(Collection in);
}// Available primitive conversion handler classes
public class IntToByte implements TypeHandler<Integer, Byte> {
public Byte getConverted(Integer in);
}These handler classes are automatically registered with the TypeHandlerFactory and can be accessed through the factory methods or instantiated directly if needed.
The type handler factory includes inheritance mapping for finding appropriate handlers:
Number Inheritance:
Integer, Long, Short, Float, Double → NumberCollection Inheritance:
ArrayList, LinkedList, AbstractList, Stack → ListHashSet, AbstractSet → SetList, Set → CollectionThis allows handlers registered for parent types to work with subclasses automatically.
Install with Tessl CLI
npx tessl i tessl/maven-org-jboss-errai--errai-common