Author: mbenson
Date: Fri Feb 26 23:23:54 2010
New Revision: 916860

URL: http://svn.apache.org/viewvc?rev=916860&view=rev
Log:
extract DelegatingPropertyEvaluator from StringOperationsEvaluator

Added:
    
ant/antlibs/props/trunk/src/main/org/apache/ant/props/DelegatingPropertyEvaluator.java
   (with props)
Modified:
    
ant/antlibs/props/trunk/src/main/org/apache/ant/props/stringops/StringOperationsEvaluator.java

Added: 
ant/antlibs/props/trunk/src/main/org/apache/ant/props/DelegatingPropertyEvaluator.java
URL: 
http://svn.apache.org/viewvc/ant/antlibs/props/trunk/src/main/org/apache/ant/props/DelegatingPropertyEvaluator.java?rev=916860&view=auto
==============================================================================
--- 
ant/antlibs/props/trunk/src/main/org/apache/ant/props/DelegatingPropertyEvaluator.java
 (added)
+++ 
ant/antlibs/props/trunk/src/main/org/apache/ant/props/DelegatingPropertyEvaluator.java
 Fri Feb 26 23:23:54 2010
@@ -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);
+            }
+        }
+    }
+}

Propchange: 
ant/antlibs/props/trunk/src/main/org/apache/ant/props/DelegatingPropertyEvaluator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
ant/antlibs/props/trunk/src/main/org/apache/ant/props/stringops/StringOperationsEvaluator.java
URL: 
http://svn.apache.org/viewvc/ant/antlibs/props/trunk/src/main/org/apache/ant/props/stringops/StringOperationsEvaluator.java?rev=916860&r1=916859&r2=916860&view=diff
==============================================================================
--- 
ant/antlibs/props/trunk/src/main/org/apache/ant/props/stringops/StringOperationsEvaluator.java
 (original)
+++ 
ant/antlibs/props/trunk/src/main/org/apache/ant/props/stringops/StringOperationsEvaluator.java
 Fri Feb 26 23:23:54 2010
@@ -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;
-    }
 }


Reply via email to