javolution.text.internal.TextContextImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javolution-core-java-msftbx Show documentation
Show all versions of javolution-core-java-msftbx Show documentation
Only the Java Core part of Javolution library, with slight modifications for use in MSFTBX.
/*
* 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.internal;
import java.io.IOException;
import javolution.context.LogContext;
import javolution.text.CharSet;
import javolution.text.Cursor;
import javolution.text.DefaultTextFormat;
import javolution.text.TextContext;
import javolution.text.TextFormat;
import javolution.text.TypeFormat;
import javolution.util.FastMap;
/**
* Holds the default implementation of TextContext.
*
* @author Jean-Marie Dautelle
* @version 6.0, July 21, 2013
*/
public final class TextContextImpl extends TextContext {
// Holds class->format mapping.
private final FastMap, TextFormat>> classToFormat = new FastMap, TextFormat>>()
.shared();
// Holds parent (null if root).
private final TextContextImpl parent;
/** Default constructor for root */
public TextContextImpl() {
parent = null;
classToFormat.put(Boolean.class, BOOLEAN_FORMAT);
classToFormat.put(Byte.class, BYTE_FORMAT);
classToFormat.put(Character.class, CHARACTER_FORMAT);
classToFormat.put(Class.class, CLASS_FORMAT);
classToFormat.put(Double.class, DOUBLE_FORMAT);
classToFormat.put(Float.class, FLOAT_FORMAT);
classToFormat.put(Integer.class, INTEGER_FORMAT);
classToFormat.put(Long.class, LONG_FORMAT);
classToFormat.put(Short.class, SHORT_FORMAT);
classToFormat.put(String.class, STRING_FORMAT);
}
/** Inner constructor */
public TextContextImpl(TextContextImpl parent) {
this.parent = parent;
}
@Override
protected TextContext inner() {
return new TextContextImpl(this);
}
@SuppressWarnings("unchecked")
@Override
protected TextFormat searchFormat(Class extends T> type) {
TextFormat format = (TextFormat) classToFormat.get(type);
if (format != null) return format;
if (parent != null) { // Searches parent.
format = parent.searchFormat(type);
classToFormat.put(type, format);
return format;
}
// Root context (search inheritable annotations).
DefaultTextFormat annotation = type
.getAnnotation(DefaultTextFormat.class);
if (annotation != null) { // Found it.
try {
format = (TextFormat) annotation.value().newInstance();
classToFormat.put(type, format);
return format;
} catch (Throwable error) {
LogContext.warning(error);
}
}
classToFormat.put(type, OBJECT_FORMAT);
return (TextFormat) OBJECT_FORMAT;
}
@Override
public void setFormat(Class extends T> type, TextFormat format) {
classToFormat.put(type, format);
}
////////////////////////
// PREDEFINED FORMATS //
////////////////////////
private static final TextFormat> OBJECT_FORMAT = new TextFormat