01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: CharSequenceDeferred.java 3734 2007-05-02 22:19:07Z gbevin $
07: */
08: package com.uwyn.rife.engine;
09:
10: import com.uwyn.rife.template.TemplateEncoder;
11:
12: public abstract class CharSequenceDeferred<T extends CharSequenceDeferred>
13: implements CharSequence {
14: private CharSequence mContent = null;
15: private String mResult = null;
16: private TemplateEncoder mEncoder = null;
17: private long mPreservedInputsModified = -1;
18:
19: public synchronized void setContent(CharSequence content) {
20: mContent = content;
21: }
22:
23: synchronized T encoder(TemplateEncoder encoder) {
24: mEncoder = encoder;
25: mResult = null;
26:
27: return (T) this ;
28: }
29:
30: public synchronized String encode(String value) {
31: if (null == mEncoder) {
32: return value;
33: }
34:
35: return mEncoder.encode(value);
36: }
37:
38: protected abstract void fillInContent();
39:
40: public synchronized final String toString() {
41: long modified = RequestState.getActiveRequestState()
42: .getElementResultStatesObtained().getModified();
43: if (mPreservedInputsModified != modified) {
44: mResult = null;
45: mPreservedInputsModified = modified;
46: fillInContent();
47: }
48:
49: if (null == mContent) {
50: return null;
51: }
52:
53: if (null == mResult) {
54: mResult = mContent.toString();
55: }
56:
57: return mResult;
58: }
59:
60: public int length() {
61: return 0;
62: }
63:
64: public char charAt(int index) {
65: return toString().charAt(index);
66: }
67:
68: public CharSequence subSequence(int start, int end) {
69: return toString().subSequence(start, end);
70: }
71: }
|