Tabnine Logo
ConverterRegistry
Code IndexAdd Tabnine to your IDE (free)

How to use
ConverterRegistry
in
js.converter

Best Java code snippets using js.converter.ConverterRegistry (Showing top 20 results out of 315)

origin: com.js-lib/tiny-container

/**
 * Construct node object.
 * 
 * @param object wrapped object.
 */
public ObjectNode(Object object) {
  this.converter = ConverterRegistry.getConverter();
  this.object = object;
}
origin: com.js-lib/js-xhtml-template

  /**
   * An object is <em>strict object</em> if is not value type, that is, there is no converter for it.
   * 
   * @param object object instance to check.
   * @return true if <code>object</code> is <em>strict object</em>.
   */
  private boolean isStrictObject(Object object) {
    return !ConverterRegistry.hasType(object.getClass());
  }
}
origin: com.js-lib/js-commons

/**
 * Convenient way to retrieve converter instance, see {@link #getConverterInstance()}.
 * 
 * @return converter instance.
 */
public static Converter getConverter()
{
 return instance.getConverterInstance();
}
origin: com.js-lib/js-xhtml-template

if (!propertyPath.equals(".") && ConverterRegistry.hasType(scope.getClass())) {
  throw new TemplateException("Operand is property path but scope is not an object.");
  Object object = this.content.getObject(scope, propertyPath);
  if (object != null) {
    if (!ConverterRegistry.hasType(object.getClass())) {
      throw new TemplateException("Invalid element |%s|. Operand for VALUE operator without formatter should be convertible to string.", element);
    value = ConverterRegistry.getConverter().asString(object);
origin: com.js-lib/js-commons

if(Types.isKindOf(valueType, Converter.class)) {
 registerConverter(valueType, (Class<? extends Converter>)valueType);
   registerConverterInstance(valueType, entries.getValue());
origin: com.js-lib/tiny-container

ConverterRegistry.getInstance().registerConverter(valueType, converterClass);
origin: com.js-lib/js-commons

registerConverter(entry.getKey(), entry.getValue());
origin: com.js-lib/js-commons

/**
 * Convenient way to test for converter support, see {@link #hasClassConverter(Class)}. Note that this predicate
 * variant accepts {@link Type} as parameter to simplify integration with user code but always returns false if type
 * is not a {@link Class}.
 * 
 * @param valueType value type to check for conversion support.
 * @return true if value type has registered converter.
 */
public static boolean hasType(Type valueType)
{
 return valueType instanceof Class ? instance.hasClassConverter((Class<?>)valueType) : false;
}
origin: com.js-lib/js-commons

/**
 * Register user defined converter associated to value type. Converter class should be instantiable, i.e. not
 * abstract, interface or void, must have no arguments constructor, even if private and its constructor should of
 * course execute without exception. Otherwise unchecked exception may arise as described on throws section. There are
 * no other constrains on value type class.
 * <p>
 * Note: it is caller responsibility to enforce proper bound, i.e. given converter class to properly handle requested
 * value type. Otherwise exception may throw when this particular converter is enacted.
 * 
 * @param valueType value type class,
 * @param converterClass specialized converter class.
 * @throws BugError if converter class is not instantiable.
 * @throws NoSuchBeingException if converter class has no default constructor.
 * @throws InvocationException with target exception if converter class construction fails on its execution.
 */
public void registerConverter(Class<?> valueType, Class<? extends Converter> converterClass)
{
 Converter converter = Classes.newInstance(converterClass);
 if(Types.isConcrete(valueType)) {
  registerConverterInstance(valueType, converter);
 }
 else {
  if(abstractConverters.put(valueType, converter) == null) {
   log.debug("Register abstract converter |%s| for value type |%s|.", converterClass, valueType);
  }
  else {
   log.warn("Override abstract converter |%s| for value type |%s|.", converterClass, valueType);
  }
 }
}
origin: com.js-lib/js-xhtml-template

  return format.format(value);
if (!Types.isPrimitiveLike(value) && !ConverterRegistry.hasType(value.getClass())) {
  throw new TemplateException("Value |%s#%s| should be a primitive like but is |%s|.", scope.getClass(), propertyPath, value.getClass());
return ConverterRegistry.getConverter().asString(value);
origin: com.js-lib/tiny-container

/**
 * Construct list node for a given list component type.
 * 
 * @param object wrapped list,
 * @param componentType list component type.
 */
public ListNode(Object object, Class<?> componentType) {
  this.converter = ConverterRegistry.getConverter();
  this.list = (List<Object>) object;
  this.componentType = componentType;
}
origin: com.js-lib/tiny-container

  throw new BugError("Generic value types are not supported.");
if (ConverterRegistry.hasType(type)) {
  return ConverterRegistry.getConverter().asObject(value, (Class<T>) type);
Converter converter = ConverterRegistry.getConverter();
for (String s : strings) {
  collection.add(converter.asObject(s.trim(), itemType));
origin: com.js-lib/js-xhtml-template

if (!propertyPath.equals(".") && ConverterRegistry.hasType(scope.getClass())) {
  throw new TemplateException("Operand is property path but scope is not an object.");
origin: com.js-lib/js-commons

/**
 * Check if registry instance has registered converter for requested class.
 * 
 * @param classConverter class to check for conversion support.
 * @return true if this converter registry has support for requested class.
 */
public boolean hasClassConverter(Class<?> classConverter)
{
 return getConverter(classConverter) != null || String.class.equals(classConverter);
}
origin: com.js-lib/js-json

Converter converter = ConverterRegistry.getConverter();
try {
  if (Types.isBoolean(value)) {
  if (value instanceof String || ConverterRegistry.hasType(value.getClass())) {
    writeString(converter.asString(value));
    return;
origin: com.js-lib/js-xhtml-template

  /**
   * Execute TITLE operator. Uses property path to extract content value, convert it to string and set <em>title</em>
   * attribute.
   * 
   * @param element context element, unused,
   * @param scope scope object,
   * @param propertyPath property path,
   * @param arguments optional arguments, unused.
   * @return always returns null for void.
   * @throws TemplateException if requested content value is undefined.
   */
  @Override
  protected Object doExec(Element element, Object scope, String propertyPath, Object... arguments) throws TemplateException {
    if (!propertyPath.equals(".") && ConverterRegistry.hasType(scope.getClass())) {
      throw new TemplateException("Operand is property path but scope is not an object.");
    }
    Object value = content.getObject(scope, propertyPath);
    if (value == null) {
      return null;
    }
    if (!(value instanceof String)) {
      throw new TemplateException("Invalid element |%s|. TITLE operand should be string.", element);
    }
    return new AttrImpl("title", (String) value);
  }
}
origin: com.js-lib/tiny-container

/**
 * Construct array node.
 * 
 * @param array wrapped array.
 */
public ArrayNode(Object array) {
  this.converter = ConverterRegistry.getConverter();
  this.array = array;
  this.componentType = array.getClass().getComponentType();
}
origin: com.js-lib/js-json

Converter converter = ConverterRegistry.getConverter();
if (type == null) {
  return new MissingFieldValue(converter);
  return new CollectionValue(converter, type);
if (type instanceof Class<?> && ConverterRegistry.hasType(type)) {
  return new PrimitiveValue(converter, (Class<?>) type);
origin: com.js-lib/js-xhtml-template

  /**
   * Execute HREF operator. Uses property path to extract content value, convert it to string and set <em>href</em> attribute.
   * 
   * @param element context element, unused,
   * @param scope scope object,
   * @param propertyPath property path,
   * @param arguments optional arguments, unused.
   * @return always returns null for void.
   * @throws TemplateException if requested content value is undefined.
   */
  @Override
  protected Object doExec(Element element, Object scope, String propertyPath, Object... arguments) throws TemplateException {
    if (!propertyPath.equals(".") && ConverterRegistry.hasType(scope.getClass())) {
      throw new TemplateException("Operand is property path but scope is not an object.");
    }
    Object value = content.getObject(scope, propertyPath);
    if (value == null) {
      return null;
    }
    if (value instanceof URL) {
      value = ((URL) value).toExternalForm();
    }
    if (!(value instanceof String)) {
      throw new TemplateException("Invalid element |%s|. HREF operand should be URL or string.", element);
    }
    return new AttrImpl("href", (String) value);
  }
}
origin: com.js-lib/tiny-container

@Override
public void setProperty(String name, Object value) {
  if (!(value instanceof String)) {
    value = ConverterRegistry.getConverter().asString(value);
  }
  contextParameters.put(name, value);
}
js.converterConverterRegistry

Javadoc

Converter registry global per JVM. This is the facade of converters package and together with Converterinterface are the only public entities. Converter registry is a sort of directory service: it binds value types with converters. A value type is a class that wrap a single value susceptible to be represented as a single string and a converter is used exactly for that: convert value type to / from string.

String representation supplied by converter package is not meant for user interfaces but merely for internal value types serialization and storage. For this reason is critical to have the same converter - or at least compatible, for distributed applications running on separated JVM. Also this is the reason converter registry is singleton.

Converter registry provides method to retrieve registry singleton, see #getInstance() and convenient ways to get converter instance, #getConverter() and predicate to test for value type support, #hasType(Type).

 
if (ConverterRegistry.hasType(MessageID.class)) { 
String messageIdValue = ConverterRegistry.getConverter().asString(messageID); 
// store or send message ID 
} 
... 
MessageID messageID = ConverterRegistry.getConverter().asObject(messageIdValue); 
// message ID instance recreated 

Converter registry comes with built-in converters for common Java value types but user defined converters are supported. One can use #registerConverter(Class,Class) to bind a custom converter. Also registry searches for ConverterProvider implemented by third party libraries and deployed as services on Java run-time. Here are built-in converters, boxed types apply also to related primitives:

  • Boolean
  • Character
  • Byte
  • Short
  • Integer
  • Long
  • Float
  • Double
  • Enum
  • Date
  • Date
  • Time
  • Timestamp
  • Class
  • File
  • URL
  • Locale
  • TimeZone

Here a is sample code of an user defined converter for a hypothetical MessageID value object. Note that in the context Converter#asObject(String,Class) is executed string argument is already tested for null value; the same is true for object argument from Converter#asString(Object).

 
public final class MessageIDConverter implements Converter { 
@Override 
public <T> T asObject(String string, Class<T> valueType) { 
if (string.isEmpty()) { 
return null; 
} 
return (T) new MessageID(string); 
} 
@Override 
public String asString(Object object) { 
return ((MessageID) object).getValue(); 
} 
} 
... 
ConverterRegistry.getInstance().registerConverter(MessageID.class, MessageIDConverter.class); 

It is also possible to combine value type with converter in a single class.

 
public final class MessageID implements Converter { 
// MessageID implementation 
... 
@Override 
public <T> T asObject(String string, Class<T> valueType) { 
... 
} 
@Override 
public String asString(Object object) { 
... 
} 
} 

Most used methods

  • getConverter
    Return converter instance declared for requested value type or null if none found. This method has s
  • hasType
    Convenient way to test for converter support, see #hasClassConverter(Class). Note that this predicat
  • registerConverter
    Register user defined converter associated to value type. Converter class should be instantiable, i.
  • getConverterInstance
    Get converter instance.
  • getInstance
    Get converter registry instance.
  • hasClassConverter
    Check if registry instance has registered converter for requested class.
  • registerConverterInstance
    Utility method to bind converter instance to concrete value type.

Popular in Java

  • Making http requests using okhttp
  • startActivity (Activity)
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • getSharedPreferences (Context)
  • Kernel (java.awt.image)
  • FileNotFoundException (java.io)
    Thrown when a file specified by a program cannot be found.
  • PriorityQueue (java.util)
    A PriorityQueue holds elements on a priority heap, which orders the elements according to their natu
  • Scanner (java.util)
    A parser that parses a text string of primitive types and strings with the help of regular expressio
  • TimeUnit (java.util.concurrent)
    A TimeUnit represents time durations at a given unit of granularity and provides utility methods to
  • Scheduler (org.quartz)
    This is the main interface of a Quartz Scheduler. A Scheduler maintains a registry of org.quartz.Job
  • CodeWhisperer alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now