javolution.text.TextContext Maven / Gradle / Ivy
Show all versions of javolution-core-java-msftbx Show documentation
/*
* Javolution - Java(TM) Solution for Real-Time and Embedded Systems
* Copyright (C) 2012 - Javolution (http://javolution.org/)
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software is
* freely granted, provided that this notice is preserved.
*/
package javolution.text;
import java.io.IOException;
import javolution.context.AbstractContext;
import javolution.context.FormatContext;
import javolution.osgi.internal.OSGiServices;
/**
* A context for plain text parsing/formatting. This context provides
* the {@link javolution.text.TextFormat TextFormat} to parse/format objects
* of any class. If not superseded, the text format for a class is specified
* by the {@link javolution.text.DefaultTextFormat DefaultTextFormat}
* annotation.
*
* A text context always returns the most specialized format. If a class
* has no default format annotation (inherited or not), then the default
* {@link java.lang.Object} format (which calls {@link Object#toString})
* is returned. A predefined format exists for the following standard types:
*
* - java.lang.Object (parsing not supported, formatting calls toString())
* - java.lang.Boolean
* - java.lang.Character
* - java.lang.Byte
* - java.lang.Short
* - java.lang.Integer
* - java.lang.Long
* - java.lang.Float
* - java.lang.Double
* - java.lang.Class
* - java.lang.String
*
*
*
* @author Jean-Marie Dautelle
* @version 6.0 December 12, 2012
*/
public abstract class TextContext extends FormatContext {
/**
* Default constructor.
*/
protected TextContext() {}
/**
* Enters and returns a new text context instance.
*/
public static TextContext enter() {
return (TextContext) TextContext.currentTextContext().enterInner();
}
/**
* Returns the text format for the specified type. It is the most
* specialized format able to parse/format instances of the specified
* class. If there is no default format for the specified class,
* the standard object format (toString based) is returned.
*/
public static TextFormat getFormat(Class extends T> type) {
return TextContext.currentTextContext().searchFormat(type);
}
/**
* Sets the text format for the specified type (and its sub-types).
*/
public abstract void setFormat(Class extends T> type,
TextFormat newFormat);
/**
* Formats the specified object using its current {@link #getFormat(Class)
* format} (convenience method).
*/
public static Appendable format(Object obj, Appendable dest) throws IOException {
if (obj == null) return dest.append("null");
return TextContext.getFormat(obj.getClass()).format(obj, dest);
}
/**
* Searches the most specialized format for the specified type.
*/
protected abstract TextFormat searchFormat(Class extends T> type);
/**
* Returns the current text context.
*/
private static TextContext currentTextContext() {
TextContext ctx = AbstractContext.current(TextContext.class);
if (ctx != null)
return ctx;
return OSGiServices.getTextContext();
}
}