<commit_before>/*
 * Copyright 2003-2010 the original author or authors.
 *
 * Licensed 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 groovy.lang;

import org.codehaus.groovy.reflection.ReflectionCache;
import org.codehaus.groovy.reflection.stdclasses.CachedClosureClass;
import org.codehaus.groovy.runtime.ComposedClosure;
import org.codehaus.groovy.runtime.CurriedClosure;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.memoize.LRUCache;
import org.codehaus.groovy.runtime.memoize.Memoize;
import org.codehaus.groovy.runtime.memoize.UnlimitedConcurrentCache;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;

import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.io.Writer;

/**
 * Represents any closure object in Groovy.
 * <p/>
 * Groovy allows instances of Closures to be called in a
 * short form. For example:
 * <pre>
 *   def a = 1
 *   def c = {a}
 *   assert c() == 1
 * </pre>
 * To be able to use a Closure in this way with your own
 * subclass, you need to provide a doCall method with any
 * signature you want to. This ensures that 
 * {@link #getMaximumNumberOfParameters()} and 
 * {@link #getParameterTypes()} will work too without any 
 * additional code. If no doCall method is provided a
 * closure must be used in its long form like
 * <pre>
 *   def a = 1
 *   def c = {a}
 *   assert c.call() == 1
 * </pre>
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <NAME>
 * @author <NAME>
 *
 * @version $Revision$
 */
public abstract class Closure<V> extends GroovyObjectSupport implements Cloneable, Runnable, GroovyCallable<V>, Serializable {

    /**
     * With this resolveStrategy set the closure will attempt to resolve property references to the
     * owner first
     */
    public static final int OWNER_FIRST = 0;
    /**
     * With this resolveStrategy set the closure will attempt to resolve property references to the
     * delegate first
     */
    public static final int DELEGATE_FIRST = 1;
    /**
     * With this resolveStrategy set the closure will resolve property references to the owner only
     * and not call the delegate at all
     */
    public static final int OWNER_ONLY = 2;
    /**
     * With this resolveStrategy set the closure will resolve property references to the delegate
     * only and entirely bypass the owner
     */
    public static final int DELEGATE_ONLY = 3;
    /**
     * With this resolveStrategy set the closure will resolve property references to itself and go
     * through the usual MetaClass look-up process. This allows the developer to override getProperty
     * using ExpandoMetaClass of the closure itself
     */
    public static final int TO_SELF = 4;

    public static final int DONE = 1, SKIP = 2;
    private static final Object[] EMPTY_OBJECT_ARRAY = {};
    
    private Object delegate;<commit_msg>GROOVY-4785: Improve Closure javadoc for resolution strategy (merge rev 22015)<commit_after>/*
 * Copyright 2003-2010 the original author or authors.
 *
 * Licensed 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 groovy.lang;

import org.codehaus.groovy.reflection.ReflectionCache;
import org.codehaus.groovy.reflection.stdclasses.CachedClosureClass;
import org.codehaus.groovy.runtime.ComposedClosure;
import org.codehaus.groovy.runtime.CurriedClosure;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.memoize.LRUCache;
import org.codehaus.groovy.runtime.memoize.Memoize;
import org.codehaus.groovy.runtime.memoize.UnlimitedConcurrentCache;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;

import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.io.Writer;

/**
 * Represents any closure object in Groovy.
 * <p/>
 * Groovy allows instances of Closures to be called in a
 * short form. For example:
 * <pre>
 *   def a = 1
 *   def c = {a}
 *   assert c() == 1
 * </pre>
 * To be able to use a Closure in this way with your own
 * subclass, you need to provide a doCall method with any
 * signature you want to. This ensures that 
 * {@link #getMaximumNumberOfParameters()} and 
 * {@link #getParameterTypes()} will work too without any 
 * additional code. If no doCall method is provided a
 * closure must be used in its long form like
 * <pre>
 *   def a = 1
 *   def c = {a}
 *   assert c.call() == 1
 * </pre>
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <NAME>
 * @author <NAME>
 *
 * @version $Revision$
 */
public abstract class Closure<V> extends GroovyObjectSupport implements Cloneable, Runnable, GroovyCallable<V>, Serializable {

    /**
     * With this resolveStrategy set the closure will attempt to resolve property references to the
     * owner first, then the delegate (<b>this is the default strategy</b>).
     *
     * For example the following code :
     * <pre>
     *  class Test {
     *    def x = 30
     *    def y = 40
     *
     *    def run() {
     *        def data = [ x: 10, y: 20 ]
     *        def cl = { y = x + y }
     *        cl.delegate = data
     *        cl()
     *        println x
     *        println y
     *        println data
     *    }
     *  }
     *
     *  new Test().run()
     * </pre>
     * will output :
     * <pre>
     *     30
     *     70
     *     [x:10, y:20]
     * </pre>
     * because the x and y fields declared in the Test class the variables in the delegate.<p>
     * <i>Note that local variables are always looked up first, independently of the resolution strategy.</i>
     */
    public static final int OWNER_FIRST = 0;

    /**
     * With this resolveStrategy set the closure will attempt to resolve property references to the
     * delegate first then the owner.
     *
     * For example the following code :
     * <pre>
     *  class Test {
     *    def x = 30
     *    def y = 40
     *
     *    def run() {
     *        def data = [ x: 10, y: 20 ]
     *        def cl = { y = x + y }
     *        cl.delegate = data
     *        cl.resolveStrategy = Closure.DELEGATE_FIRST
     *        cl()
     *        println x
     *        println y
     *        println data
     *    }
     *  }
     *
     *  new Test().run()
     * </pre>
     * will output :
     * <pre>
     *     30
     *     40
     *     [x:10, y:30]
     * </pre>
     * because the x and y variables declared in the delegate shadow the fields in the owner class.<p>
     * <i>Note that local variables are always looked up first, independently of the resolution strategy.</i>
     */
    public static final int DELEGATE_FIRST = 1;

    /**
     * With this resolveStrategy set the closure will resolve property references to the owner only
     * and not call the delegate at all. For example the following code :
     *
     * <pre>
     *  class Test {
     *    def x = 30
     *    def y = 40
     *
     *    def run() {
     *        def data = [ x: 10, y: 20, z: 30 ]
     *        def cl = { y = x + y }
     *        cl.delegate = data
     *        cl.resolveStrategy = Closure.OWNER_ONLY
     *        cl()
     *        println x
     *        println y
     *        println data
     *    }
     *  }
     *
     *  new Test().run()
     * </pre>
     *
     * will throw "No such property: z" error because even if the z variable is declared in the delegate, no
     * lookup is made.<p>
     * <i>Note that local variables are always looked up first, independently of the resolution strategy.</i>
     */
    public static final int OWNER_ONLY = 2;

    /**
     * With this resolveStrategy set the closure will resolve property references to the delegate
     * only and entirely bypass the owner. For example the following code :
     *
     * <pre>
     *  class Test {
     *    def x = 30
     *    def y = 40
     *    def z = 50
     *
     *    def run() {
     *        def data = [ x: 10, y: 20 ]
     *        def cl = { y = x + y + z}
     *        cl.delegate = data
     *        cl.resolveStrategy = Closure.DELEGATE_ONLY
     *        cl()
     *        println x
     *        println y
     *        println data
     *    }
     *  }
     *
     *  new Test().run()
     * </pre>
     *
     * will throw an error because even if the owner declares a "z" field, the resolution strategy will bypass
     * lookup in the owner.<p>
     * <i>Note that local variables are always looked up first, independently of the resolution strategy.</i>
     */
    public static final int DELEGATE_ONLY = 3;

    /**
     * With this resolveStrategy set the closure will resolve property references to itself and go
     * through the usual MetaClass look-up process. This means that properties are neither resolved from
     * the owner nor the delegate, but only on the closure object itself. This allows the developer to override
     * getProperty using ExpandoMetaClass of the closure itself.<p>
     * <i>Note that local variables are always looked up first, independently of the resolution strategy.</i>
     */
    public static final int TO_SELF = 4;

    public static final int DONE = 1, SKIP = 2;
    private static final Object[] EMPTY_OBJECT_ARRAY = {};
    
    private Object delegate;
{'fraction_non_alphanumeric': 0.09323250693979132, 'fraction_numerical': 0.010242174787020197, 'mean_word_length': 2.5083948959032907, 'pattern_counts': {'":': 0, '<': 74, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2211365', 'n_tokens_mistral': 3168, 'n_tokens_neox': 2980, 'n_words': 1154}