extract DelegatingPropertyEvaluator from StringOperationsEvaluator
git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/props/trunk@916860 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/org/apache/ant/props/DelegatingPropertyEvaluator.java b/src/main/org/apache/ant/props/DelegatingPropertyEvaluator.java
new file mode 100644
index 0000000..10ad45f
--- /dev/null
+++ b/src/main/org/apache/ant/props/DelegatingPropertyEvaluator.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.ant.props;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Stack;
+
+import org.apache.tools.ant.PropertyHelper;
+import org.apache.tools.ant.PropertyHelper.PropertyEvaluator;
+
+/**
+ * Abstract delegating {@link PropertyEvaluator}.
+ */
+public abstract class DelegatingPropertyEvaluator implements PropertyHelper.PropertyEvaluator {
+ private final ThreadLocal stack = new ThreadLocal();
+
+ private ArrayList delegates = new ArrayList();
+
+ /**
+ * Add a {@link PropertyEvaluator} delegate.
+ * @param propertyEvaluator to add
+ */
+ protected void addDelegate(PropertyEvaluator propertyEvaluator) {
+ delegates.add(propertyEvaluator);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object evaluate(String propertyName, PropertyHelper propertyHelper) {
+ if (getRequiredStack().contains(propertyName)) {
+ return null;
+ }
+ push(propertyName);
+ try {
+ for (Iterator iter = delegates.iterator(); iter.hasNext();) {
+ Object value = ((PropertyEvaluator) iter.next()).evaluate(propertyName,
+ propertyHelper);
+ if (value != null) {
+ return value;
+ }
+ }
+ } finally {
+ pop();
+ }
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof DelegatingPropertyEvaluator == false) {
+ return false;
+ }
+ return delegates.equals(((DelegatingPropertyEvaluator) obj).delegates);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ return 17 * delegates.hashCode();
+ }
+
+ private synchronized Stack getRequiredStack() {
+ Stack result = (Stack) stack.get();
+ if (result == null) {
+ result = new Stack();
+ stack.set(result);
+ }
+ return result;
+ }
+
+ private synchronized void push(String propertyName) {
+ getRequiredStack().push(propertyName);
+ }
+
+ private synchronized void pop() {
+ Stack stk = (Stack) stack.get();
+ if (stk != null) {
+ stk.pop();
+ if (stk.isEmpty()) {
+ stack.set(null);
+ }
+ }
+ }
+}
diff --git a/src/main/org/apache/ant/props/stringops/StringOperationsEvaluator.java b/src/main/org/apache/ant/props/stringops/StringOperationsEvaluator.java
index cd06a0b..5afd675 100644
--- a/src/main/org/apache/ant/props/stringops/StringOperationsEvaluator.java
+++ b/src/main/org/apache/ant/props/stringops/StringOperationsEvaluator.java
@@ -19,63 +19,26 @@
*/
package org.apache.ant.props.stringops;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.Stack;
-
-import org.apache.tools.ant.PropertyHelper;
-import org.apache.tools.ant.PropertyHelper.PropertyEvaluator;
+import org.apache.ant.props.DelegatingPropertyEvaluator;
/**
* PropertyEvaluator to apply *nix-style string operations to Ant properties.
*/
-public class StringOperationsEvaluator implements PropertyHelper.PropertyEvaluator {
- private static final ThreadLocal STACK = new ThreadLocal() {
- protected Object initialValue() {
- return new Stack();
- }
- };
-
- private ArrayList delegates = new ArrayList();
-
+public class StringOperationsEvaluator extends DelegatingPropertyEvaluator {
/**
* Construct a new StringOperationsEvaluator.
*/
public StringOperationsEvaluator() {
- delegates.add(new Substring());
- delegates.add(new DefaultValue());
- delegates.add(new SetDefaultValue());
- delegates.add(new Translate());
- delegates.add(new RequireProperty());
- delegates.add(new DeleteFromStartGreedy());
- delegates.add(new DeleteFromStartReluctant());
- delegates.add(new DeleteFromEndGreedy());
- delegates.add(new DeleteFromEndReluctant());
- delegates.add(new ReplaceOperation());
+ addDelegate(new Substring());
+ addDelegate(new DefaultValue());
+ addDelegate(new SetDefaultValue());
+ addDelegate(new Translate());
+ addDelegate(new RequireProperty());
+ addDelegate(new DeleteFromStartGreedy());
+ addDelegate(new DeleteFromStartReluctant());
+ addDelegate(new DeleteFromEndGreedy());
+ addDelegate(new DeleteFromEndReluctant());
+ addDelegate(new ReplaceOperation());
}
- /**
- * {@inheritDoc}
- */
- public Object evaluate(String propertyName, PropertyHelper propertyHelper) {
- Stack stk = (Stack) STACK.get();
- if (stk.contains(propertyName)) {
- return null;
- }
- stk.push(propertyName);
- try {
- for (Iterator iter = delegates.iterator(); iter.hasNext();) {
- Object value = ((PropertyEvaluator) iter.next()).evaluate(propertyName,
- propertyHelper);
- if (value != null) {
- return value;
- }
- }
- } finally {
- if (stk.pop() != propertyName) {
- throw new IllegalStateException("stack out of balance");
- }
- }
- return null;
- }
}