Javolution 6.0.0 java
javolution.text.internal.TextContextImpl Class Reference
Inheritance diagram for javolution.text.internal.TextContextImpl:
[legend]
Collaboration diagram for javolution.text.internal.TextContextImpl:
[legend]

Public Member Functions

 TextContextImpl ()
 
 TextContextImpl (TextContextImpl parent)
 
void exit ()
 

Static Public Member Functions

static TextContext enter ()
 
static< T extends AbstractContext > T enter (Class< T > custom)
 
static< T > TextFormat< T > getFormat (Class<? extends T > type)
 
static AbstractContext current ()
 
static void inherit (AbstractContext ctx)
 

Protected Member Functions

TextContext inner ()
 
AbstractContext enterInner ()
 
AbstractContext getOuter ()
 

Static Protected Member Functions

static< T extends AbstractContext > T current (Class< T > type)
 

Package Functions

protected< T > TextFormat< T > searchFormat (Class<? extends T > type)
 
public< T > void setFormat (Class<? extends T > type, TextFormat< T > format)
 

Private Member Functions

void storePrimitiveTypesFormats ()
 

Static Private Member Functions

static TextContext currentTextContext ()
 

Private Attributes

final FastMap< Class<?>, TextFormat<?> > localFormats
 
final FastMap< Class<?>, TextFormat<?> > defaultFormats
 
AbstractContext outer
 

Static Private Attributes

static final TextFormat<?> OBJECT_FORMAT
 
static final ThreadLocal< AbstractContextCURRENT = new ThreadLocal<AbstractContext>()
 

Detailed Description

Holds the default implementation of TextContext.

Author
Jean-Marie Dautelle
Version
6.0, July 21, 2013

Definition at line 37 of file TextContextImpl.java.

Constructor & Destructor Documentation

◆ TextContextImpl() [1/2]

javolution.text.internal.TextContextImpl.TextContextImpl ( )

Default constructor for root

Definition at line 70 of file TextContextImpl.java.

70  {
71  localFormats = new FastMap<Class<?>, TextFormat<?>>(); // Updated only during configuration.
72  defaultFormats = new FastMap<Class<?>, TextFormat<?>>().shared(); // Can be updated concurrently.
74  }

References javolution.text.internal.TextContextImpl.defaultFormats, javolution.text.internal.TextContextImpl.localFormats, and javolution.text.internal.TextContextImpl.storePrimitiveTypesFormats().

Referenced by javolution.text.internal.TextContextImpl.inner().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TextContextImpl() [2/2]

javolution.text.internal.TextContextImpl.TextContextImpl ( TextContextImpl  parent)

Inner constructor

Definition at line 77 of file TextContextImpl.java.

77  {
78  localFormats = new FastMap<Class<?>, TextFormat<?>>()
79  .putAll(parent.localFormats);
80  defaultFormats = parent.defaultFormats;
81  }

References javolution.text.internal.TextContextImpl.defaultFormats, and javolution.text.internal.TextContextImpl.localFormats.

Member Function Documentation

◆ current() [1/2]

static AbstractContext javolution.context.AbstractContext.current ( )
staticinherited

Returns the current context for the current thread or

null

if this thread has no context (default).

Definition at line 61 of file AbstractContext.java.

61  {
62  return AbstractContext.CURRENT.get();
63  }

References javolution.context.AbstractContext.CURRENT.

Referenced by javolution.context.LogContext.currentLogContext(), javolution.context.SecurityContext.currentSecurityContext(), javolution.text.TextContext.currentTextContext(), javolution.xml.XMLContext.currentXMLContext(), javolution.context.LocalContext.enter(), javolution.context.ConcurrentContext.enter(), and javolution.context.LocalContext.Parameter< T >.get().

Here is the caller graph for this function:

◆ current() [2/2]

static <T extends AbstractContext> T javolution.context.AbstractContext.current ( Class< T >  type)
staticprotectedinherited

Returns the current context of specified type or

null

if none.

Definition at line 69 of file AbstractContext.java.

69  {
70  AbstractContext ctx = AbstractContext.CURRENT.get();
71  while (ctx != null) {
72  if (type.isInstance(ctx))
73  return (T) ctx;
74  ctx = ctx.outer;
75  }
76  return null;
77  }

References javolution.context.AbstractContext.CURRENT, and javolution.context.AbstractContext.outer.

◆ currentTextContext()

static TextContext javolution.text.TextContext.currentTextContext ( )
staticprivateinherited

Returns the current text context.

Definition at line 87 of file TextContext.java.

87  {
88  TextContext ctx = AbstractContext.current(TextContext.class);
89  if (ctx != null)
90  return ctx;
91  return OSGiServices.getTextContext();
92  }

References javolution.context.AbstractContext.current(), and javolution.osgi.internal.OSGiServices.getTextContext().

Referenced by javolution.text.TextContext.enter(), and javolution.text.TextContext.getFormat().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ enter() [1/2]

static TextContext javolution.text.TextContext.enter ( )
staticinherited

Enters and returns a new text context instance.

Definition at line 59 of file TextContext.java.

59  {
60  return (TextContext) TextContext.currentTextContext().enterInner();
61  }

References javolution.text.TextContext.currentTextContext(), and javolution.context.AbstractContext.enterInner().

Here is the call graph for this function:

◆ enter() [2/2]

static <T extends AbstractContext> T javolution.context.AbstractContext.enter ( Class< T >  custom)
staticinherited

Enters the scope of a custom context. This method raises a SecurityException if the permission to enter contexts of the specified class is not granted. For example, the following disallow entering any custom context. [code] SecurityContext ctx = SecurityContext.enter(); try { ctx.revoke(new SecurityContext.Permission(AbstractContext.class, "enter")); ... // Cannot enter any custom context. } finally { ctx.exit(); // Back to previous security settings. }[/code]


Parameters
customthe custom context to enter.
Exceptions
IllegalArgumentExceptionif the specified class default constructor cannot be instantiated.
SecurityExceptionif
SecurityContext.Permission(custom, "enter")

is not granted.
See also
SecurityContext.Permission

Definition at line 101 of file AbstractContext.java.

101  {
102  SecurityContext.check(new Permission<T>(custom, "enter"));
103  try {
104  return (T) custom.newInstance().enterInner();
105  } catch (InstantiationException e) {
106  throw new IllegalArgumentException(
107  "Cannot instantiate instance of " + custom, e);
108  } catch (IllegalAccessException e) {
109  throw new IllegalArgumentException("Cannot access " + custom, e);
110  }
111  }

References javolution.context.SecurityContext.check().

Here is the call graph for this function:

◆ enterInner()

AbstractContext javolution.context.AbstractContext.enterInner ( )
protectedinherited

Enters the scope of an inner context which becomes the current context; the previous current context becomes the outer of this context.

Returns
the inner context entered.

Definition at line 141 of file AbstractContext.java.

141  {
143  inner.outer = AbstractContext.CURRENT.get();
144  AbstractContext.CURRENT.set(inner);
145  return inner;
146  }

References javolution.context.AbstractContext.CURRENT, and javolution.context.AbstractContext.outer.

Referenced by javolution.xml.XMLContext.enter(), javolution.text.TextContext.enter(), javolution.context.LogContext.enter(), javolution.context.LocalContext.enter(), javolution.context.SecurityContext.enter(), and javolution.context.ConcurrentContext.enter().

Here is the caller graph for this function:

◆ exit()

void javolution.context.AbstractContext.exit ( )
inherited

Exits the scope of this context; the outer of this context becomes
the current context.

Exceptions
IllegalStateExceptionif this context is not the current context.

Reimplemented in javolution.context.ConcurrentContext, and javolution.context.internal.ConcurrentContextImpl.

Definition at line 155 of file AbstractContext.java.

155  {
156  if (this != AbstractContext.CURRENT.get())
157  throw new IllegalStateException(
158  "This context is not the current context");
159  AbstractContext.CURRENT.set(outer);
160  outer = null;
161  }

References javolution.context.AbstractContext.CURRENT.

◆ getFormat()

static <T> TextFormat<T> javolution.text.TextContext.getFormat ( Class<? extends T >  type)
staticinherited

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.

Definition at line 69 of file TextContext.java.

69  {
70  return TextContext.currentTextContext().searchFormat(type);
71  }

References javolution.text.TextContext.currentTextContext(), and javolution.text.TextContext.searchFormat().

Referenced by javolution.text.TextBuilder.append(), javolution.util.FastCollection< E >.Format.format(), javolution.xml.XMLFormat< T >.InputElement.getAttribute(), javolution.xml.XMLFormat< T >.Default.newInstance(), javolution.lang.Configurable< javolution.context.LogContext.Level >.parse(), javolution.util.Index.toString(), javolution.util.FastMap< Object, javolution.util.Index >.toString(), javolution.util.FastCollection< E >.toString(), and javolution.xml.XMLFormat< T >.Default.write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getOuter()

AbstractContext javolution.context.AbstractContext.getOuter ( )
protectedinherited

Returns the outer context of this context or

null

if this context has no outer context.

Definition at line 167 of file AbstractContext.java.

167  {
168  return outer;
169  }

◆ inherit()

static void javolution.context.AbstractContext.inherit ( AbstractContext  ctx)
staticinherited

Inherits the specified context which becomes the context of the current thread. This method is particularly useful when creating new threads to make them inherits from the context stack of the parent thread. [code] //Spawns a new thread inheriting the context of the current thread. MyThread myThread = new MyThread(); myThread.inherited = AbstractContext.current(); myThread.start(); ... class MyThread extends Thread { AbstractContext inherited; public void run() { AbstractContext.inherit(inherited); // Sets current context. ... } }[/code]

Definition at line 131 of file AbstractContext.java.

131  {
132  CURRENT.set(ctx);
133  }

Referenced by javolution.context.internal.ConcurrentThreadImpl.run().

Here is the caller graph for this function:

◆ inner()

TextContext javolution.text.internal.TextContextImpl.inner ( )
protected

Returns a new inner instance of this context inheriting the properties of this context. The new instance can be configured independently from its parent.

Reimplemented from javolution.context.AbstractContext.

Definition at line 84 of file TextContextImpl.java.

84  {
85  return new TextContextImpl(this);
86  }

References javolution.text.internal.TextContextImpl.TextContextImpl().

Here is the call graph for this function:

◆ searchFormat()

protected<T> TextFormat<T> javolution.text.internal.TextContextImpl.searchFormat ( Class<? extends T >  type)
package

Searches the most specialized format for the specified type.

Reimplemented from javolution.text.TextContext.

Definition at line 90 of file TextContextImpl.java.

90  {
91  Class<?> cls = type;
92  while (cls != null) {
93  TextFormat<?> format;
94  // Search local format first.
95  if (localFormats.size() > 0) {
96  format = localFormats.get(cls);
97  if (format != null) return (TextFormat<T>) format;
98  }
99  // Then search default format.
100  format = defaultFormats.get(cls);
101  if (format != null) return (TextFormat<T>) format;
102 
103  // Search annotations.
104  DefaultTextFormat annotation = cls
105  .getAnnotation(DefaultTextFormat.class);
106  if (annotation != null) { // Found it.
107  try {
108  format = annotation.value().newInstance();
109  } catch (Throwable error) {
110  LogContext.warning(error);
111  }
112  // Updates the default mapping.
113  Class<?> mappedClass = type;
114  while (true) {
115  defaultFormats.put(mappedClass, format);
116  if (mappedClass.equals(cls)) break;
117  mappedClass = mappedClass.getSuperclass();
118  }
119  return (TextFormat<T>) format;
120  }
121 
122  // Search superclass.
123  cls = cls.getSuperclass();
124  }
125  throw new Error("Object default format not found !");
126  }

References javolution.text.internal.TextContextImpl.defaultFormats, javolution.text.internal.TextContextImpl.localFormats, javolution.text.DefaultTextFormat.value(), and javolution.context.LogContext.warning().

Here is the call graph for this function:

◆ setFormat()

public<T> void javolution.text.internal.TextContextImpl.setFormat ( Class<? extends T >  type,
TextFormat< T >  newFormat 
)
package

Sets the text format for the specified type (and its sub-types).

Reimplemented from javolution.text.TextContext.

Definition at line 129 of file TextContextImpl.java.

129  {
130  localFormats.put(type, format);
131  }

References javolution.text.internal.TextContextImpl.localFormats.

◆ storePrimitiveTypesFormats()

void javolution.text.internal.TextContextImpl.storePrimitiveTypesFormats ( )
private

Definition at line 137 of file TextContextImpl.java.

137  {
138  defaultFormats.put(Object.class, OBJECT_FORMAT);
139  defaultFormats.put(Boolean.class, new TextFormat<Boolean>() {
140 
141  public Appendable format(Boolean obj, Appendable dest)
142  throws IOException {
143  return TypeFormat.format(obj.booleanValue(), dest);
144  }
145 
146  public Boolean parse(CharSequence csq, Cursor cursor) {
147  return TypeFormat.parseBoolean(csq, cursor);
148  }
149 
150  });
151  defaultFormats.put(Character.class, new TextFormat<Character>() {
152 
153  public Appendable format(Character obj, Appendable dest)
154  throws IOException {
155  return dest.append(obj.charValue());
156  }
157 
158  public Character parse(CharSequence csq, Cursor cursor) {
159  return Character.valueOf(cursor.nextChar(csq));
160  }
161 
162  });
163  defaultFormats.put(Byte.class, new TextFormat<Byte>() {
164 
165  public Appendable format(Byte obj, Appendable dest)
166  throws IOException {
167  return TypeFormat.format(obj.byteValue(), dest);
168  }
169 
170  public Byte parse(CharSequence csq, Cursor cursor) {
171  return Byte.valueOf(TypeFormat.parseByte(csq, 10, cursor));
172  }
173 
174  });
175  defaultFormats.put(Short.class, new TextFormat<Short>() {
176 
177  public Appendable format(Short obj, Appendable dest)
178  throws IOException {
179  return TypeFormat.format(obj.shortValue(), dest);
180  }
181 
182  public Short parse(CharSequence csq, Cursor cursor) {
183  return Short.valueOf(TypeFormat.parseShort(csq, 10, cursor));
184  }
185 
186  });
187  defaultFormats.put(Integer.class, new TextFormat<Integer>() {
188 
189  public Appendable format(Integer obj, Appendable dest)
190  throws IOException {
191  return TypeFormat.format(obj.intValue(), dest);
192  }
193 
194  public Integer parse(CharSequence csq, Cursor cursor) {
195  return Integer.valueOf(TypeFormat.parseInt(csq, 10, cursor));
196  }
197 
198  });
199  defaultFormats.put(Long.class, new TextFormat<Long>() {
200 
201  public Appendable format(Long obj, Appendable dest)
202  throws IOException {
203  return TypeFormat.format(obj.longValue(), dest);
204  }
205 
206  public Long parse(CharSequence csq, Cursor cursor) {
207  return Long.valueOf(TypeFormat.parseLong(csq, 10, cursor));
208  }
209 
210  });
211  defaultFormats.put(Float.class, new TextFormat<Float>() {
212 
213  public Appendable format(Float obj, Appendable dest)
214  throws IOException {
215  return TypeFormat.format(obj.floatValue(), dest);
216  }
217 
218  public Float parse(CharSequence csq, Cursor cursor) {
219  return new Float(TypeFormat.parseFloat(csq, cursor));
220  }
221 
222  });
223  defaultFormats.put(Double.class, new TextFormat<Double>() {
224 
225  public Appendable format(Double obj, Appendable dest)
226  throws IOException {
227  return TypeFormat.format(obj.doubleValue(), dest);
228  }
229 
230  public Double parse(CharSequence csq, Cursor cursor) {
231  return new Double(TypeFormat.parseDouble(csq, cursor));
232  }
233 
234  });
235  defaultFormats.put(String.class, new TextFormat<String>() {
236 
237  public Appendable format(String obj, Appendable dest)
238  throws IOException {
239  return dest.append(obj);
240  }
241 
242  public String parse(CharSequence csq, Cursor cursor) {
243  CharSequence tmp = csq.subSequence(cursor.getIndex(),
244  csq.length());
245  cursor.setIndex(csq.length());
246  return tmp.toString();
247  }
248 
249  });
250  defaultFormats.put(Class.class, new TextFormat<Class<?>>() {
251 
252  public Appendable format(Class<?> obj, Appendable dest)
253  throws IOException {
254  return dest.append(obj.getName());
255  }
256 
257  public Class<?> parse(CharSequence csq, Cursor cursor) {
258  CharSequence name = cursor.nextToken(csq, CharSet.WHITESPACES);
259  try {
260  return Class.forName(name.toString());
261  } catch (ClassNotFoundException e) {
262  throw new IllegalArgumentException("Class " + name
263  + " Not Found");
264  }
265  }
266 
267  });
268  defaultFormats.put(Date.class, new TextFormat<Date>() {
269  TimeZone tz = TimeZone.getTimeZone("UTC");
270  DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
271  {
272  df.setTimeZone(tz);
273  }
274 
275  public Appendable format(Date obj, Appendable dest)
276  throws IOException {
277  return dest.append(df.format(obj));
278  }
279 
280  public Date parse(CharSequence csq, Cursor cursor) {
281  CharSequence date = cursor.nextToken(csq, CharSet.WHITESPACES);
282  try {
283  return df.parse(date.toString());
284  } catch (ParseException error) {
285  throw new IllegalArgumentException(error);
286  }
287  }
288  });
289  defaultFormats.put(BigInteger.class, new TextFormat<BigInteger>() {
290 
291  public Appendable format(BigInteger obj, Appendable dest)
292  throws IOException {
293  return dest.append(obj.toString());
294  }
295 
296  public BigInteger parse(CharSequence csq, Cursor cursor) {
297  CharSequence value = cursor.nextToken(csq, CharSet.WHITESPACES);
298  return new BigInteger(value.toString());
299  }
300 
301  });
302  defaultFormats.put(BigDecimal.class, new TextFormat<BigDecimal>() {
303 
304  public Appendable format(BigDecimal obj, Appendable dest)
305  throws IOException {
306  return dest.append(obj.toString());
307  }
308 
309  public BigDecimal parse(CharSequence csq, Cursor cursor) {
310  CharSequence value = cursor.nextToken(csq, CharSet.WHITESPACES);
311  return new BigDecimal(value.toString());
312  }
313 
314  });
315  defaultFormats.put(Font.class, new TextFormat<Font>() {
316 
317  public Appendable format(Font obj, Appendable dest)
318  throws IOException {
319  return dest.append(obj.getName());
320  }
321 
322  public Font parse(CharSequence csq, Cursor cursor) {
323  CharSequence name = cursor.nextToken(csq, CharSet.WHITESPACES);
324  return Font.decode(name.toString());
325  }
326 
327  });
328  defaultFormats.put(Color.class, new TextFormat<Color>() {
329 
330  public Appendable format(Color obj, Appendable dest)
331  throws IOException {
332  return dest.append('#').append(
333  Integer.toHexString(obj.getRGB()));
334  }
335 
336  public Color parse(CharSequence csq, Cursor cursor) {
337  CharSequence name = cursor.nextToken(csq, CharSet.WHITESPACES);
338  return Color.decode(name.toString());
339  }
340 
341  });
342 
343  }

References javolution.text.internal.TextContextImpl.defaultFormats, javolution.text.internal.TextContextImpl.OBJECT_FORMAT, and javolution.text.TypeFormat.parseBoolean().

Referenced by javolution.text.internal.TextContextImpl.TextContextImpl().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ CURRENT

final ThreadLocal<AbstractContext> javolution.context.AbstractContext.CURRENT = new ThreadLocal<AbstractContext>()
staticprivateinherited

◆ defaultFormats

◆ localFormats

final FastMap<Class<?>, TextFormat<?> > javolution.text.internal.TextContextImpl.localFormats
private

◆ OBJECT_FORMAT

final TextFormat<?> javolution.text.internal.TextContextImpl.OBJECT_FORMAT
staticprivate
Initial value:
= new TextFormat<Object>() {
ThreadLocal<Object> objToString = new ThreadLocal<Object>();
public Appendable format(Object obj, Appendable dest)
throws IOException {
if (obj == null) return dest.append("null");
if (objToString.get() == obj)
return TypeFormat.format(System.identityHashCode(obj),
dest.append("Object#"));
objToString.set(obj);
try {
String str = obj.toString();
return dest.append(str);
} finally {
objToString.set(null);
}
}
public Object parse(CharSequence csq, Cursor cursor) {
throw new UnsupportedOperationException(
"Generic object parsing not supported.");
}
}

Definition at line 39 of file TextContextImpl.java.

Referenced by javolution.text.internal.TextContextImpl.storePrimitiveTypesFormats().

◆ outer

AbstractContext javolution.context.AbstractContext.outer
privateinherited

Holds the outer context or

null

if none (top context).

Definition at line 50 of file AbstractContext.java.

Referenced by javolution.context.AbstractContext.current(), and javolution.context.AbstractContext.enterInner().


The documentation for this class was generated from the following file:
javolution.text.internal.TextContextImpl.localFormats
final FastMap< Class<?>, TextFormat<?> > localFormats
Definition: TextContextImpl.java:64
javolution.context.AbstractContext.CURRENT
static final ThreadLocal< AbstractContext > CURRENT
Definition: AbstractContext.java:45
javolution.context.AbstractContext.inner
abstract AbstractContext inner()
javolution.text.internal.TextContextImpl.TextContextImpl
TextContextImpl()
Definition: TextContextImpl.java:70
javolution.text.internal.TextContextImpl.defaultFormats
final FastMap< Class<?>, TextFormat<?> > defaultFormats
Definition: TextContextImpl.java:67
javolution.text.internal.TextContextImpl.storePrimitiveTypesFormats
void storePrimitiveTypesFormats()
Definition: TextContextImpl.java:137
javolution.context.AbstractContext.outer
AbstractContext outer
Definition: AbstractContext.java:50
javolution.text.TextContext.TextContext
TextContext()
Definition: TextContext.java:54
javolution.text.internal.TextContextImpl.OBJECT_FORMAT
static final TextFormat<?> OBJECT_FORMAT
Definition: TextContextImpl.java:39
javolution.text.TextFormat<?>
javolution.context.AbstractContext.AbstractContext
AbstractContext()
Definition: AbstractContext.java:55