package trytocatch.regexreplacer.model.expression;

/**
 * the base class of expression node(the functions in the replace expression)
 * @author trytocatch@163.com
 */
public abstract class ExpressionNode implements ResetObserver {
	private Object cache;

	protected boolean cacheSupported = false;

	public ExpressionNode(boolean cacheSupported) {
		this.cacheSupported = cacheSupported;
	}

	public void onReset() {
		cache = null;
	}
	
	public Object getResult() {
		if (isCacheAvailable() && getResultCache() != null)
			return getResultCache();
		else
			return cache = getResultImpl();
	}
	
	public Object getResultWithoutCacheUpdate() {
		if (isCacheAvailable() && getResultCache() != null)
			return getResultCache();
		else
			return getResultImpl();
	}

	public Object getResultCache() {
		return cache;
	}

	/**
	 * whether can it use the cache
	 * if getResult always(although in replacing other line) returns the same
	 * results with same parameters, you can use the cache
	 */
	public boolean isCacheAvailable() {
		return cacheSupported;
	}

	protected abstract Object getResultImpl();

	public String toString() {
		Object o = getResult();
		if (o != null)
			return o.toString();
		else
			return "";
	}
}

@@ -467,104 +467,99 @@
 %7B%0A%09%09
-if (isCacheAvailable() && getResultCache() != null)%0A%09%09%09return getResultCache();%0A%09%09else%0A%09
+return getResultCombine(true);%0A%09%7D%0A%09%0A%09public Object getResultWithoutCacheUpdate() %7B%0A
 %09%09return
  cac
@@ -558,16 +558,8 @@
 turn
- cache =
  get
@@ -568,28 +568,36 @@
 sult
-Impl(
+Combine(false
 );%0A%09%7D%0A
-%09
 %0A%09p
-ublic
+rivate
  Obj
@@ -609,35 +609,43 @@
 etResult
-WithoutCacheUpdate(
+Combine(boolean updateCache
 ) %7B%0A%09%09if
@@ -732,35 +732,130 @@
 else
+ %7B
 %0A%09%09%09
-return getResultImpl();
+Object result = normalizeResult(getResultImpl());%0A%09%09%09if(updateCache)%7B%0A%09%09%09%09cache = result;%0A%09%09%09%7D%0A%09%09%09return result;%0A%09%09%7D
 %0A%09%7D%0A
@@ -1312,11 +1312,127 @@
 n %22%22;%0A%09%7D
+%0A%0A%09public static Object normalizeResult(Object obj)%7B%0A%09%09return obj instanceof ExpressionNode ? obj.toString():obj;%0A%09%7D
 %0A%7D%0A

the result of node won't be node