~ubuntu-branches/ubuntu/lucid/groovy/lucid

« back to all changes in this revision

Viewing changes to src/main/org/codehaus/groovy/runtime/CurriedClosure.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2009-05-19 21:49:37 UTC
  • mfrom: (3.2.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090519214937-56mctwb05t3xd63r
Tags: 1.6.3-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2003-2007 the original author or authors.
3
 
 *
4
 
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 
 * you may not use this file except in compliance with the License.
6
 
 * You may obtain a copy of the License at
7
 
 *
8
 
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
10
 
 * Unless required by applicable law or agreed to in writing, software
11
 
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
 * See the License for the specific language governing permissions and
14
 
 * limitations under the License.
15
 
 */
16
 
 
17
 
package org.codehaus.groovy.runtime;
18
 
 
19
 
 
20
 
import groovy.lang.Closure;
21
 
 
22
 
/**
23
 
 * Represents wrapper around a Closure to support currying
24
 
 * 
25
 
 * @author Jochen Theodorou
26
 
 */
27
 
public final class CurriedClosure extends Closure {
28
 
 
29
 
    private Object[] curriedParams;
30
 
    
31
 
    public CurriedClosure(Closure uncurriedClosure, Object[] arguments) {
32
 
        super(uncurriedClosure);
33
 
        curriedParams = arguments;
34
 
        maximumNumberOfParameters = uncurriedClosure.getMaximumNumberOfParameters()-arguments.length;
35
 
    }
36
 
    
37
 
    public CurriedClosure(Closure uncurriedClosure, int i) {
38
 
        this(uncurriedClosure, new Object[]{Integer.valueOf(i)});
39
 
    }
40
 
 
41
 
    public Object[] getUncurriedArguments(Object[] arguments) {
42
 
        final Object newCurriedParams[] = new Object[curriedParams.length + arguments.length];
43
 
        System.arraycopy(curriedParams, 0, newCurriedParams, 0, curriedParams.length);
44
 
        System.arraycopy(arguments, 0, newCurriedParams, curriedParams.length, arguments.length);
45
 
        return newCurriedParams;        
46
 
    }
47
 
    
48
 
    public void setDelegate(Object delegate) {
49
 
        ((Closure)getOwner()).setDelegate(delegate);
50
 
    }
51
 
    
52
 
    public Object clone() {
53
 
        Closure uncurriedClosure = (Closure) ((Closure) getOwner()).clone();
54
 
        return new CurriedClosure(uncurriedClosure,curriedParams);
55
 
    }
56
 
    
57
 
    public Class[] getParameterTypes() {
58
 
        Class[] oldParams = ((Closure)getOwner()).getParameterTypes();
59
 
        Class[] newParams = new Class[oldParams.length-curriedParams.length];
60
 
        System.arraycopy(oldParams, curriedParams.length, newParams, 0, newParams.length);
61
 
        return newParams;  
62
 
    }
63
 
}
 
1
/*
 
2
 * Copyright 2003-2007 the original author or authors.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
package org.codehaus.groovy.runtime;
 
18
 
 
19
 
 
20
import groovy.lang.Closure;
 
21
 
 
22
/**
 
23
 * Represents wrapper around a Closure to support currying
 
24
 * 
 
25
 * @author Jochen Theodorou
 
26
 */
 
27
public final class CurriedClosure extends Closure {
 
28
 
 
29
    private Object[] curriedParams;
 
30
    
 
31
    public CurriedClosure(Closure uncurriedClosure, Object[] arguments) {
 
32
        super(uncurriedClosure);
 
33
        curriedParams = arguments;
 
34
        maximumNumberOfParameters = uncurriedClosure.getMaximumNumberOfParameters()-arguments.length;
 
35
    }
 
36
    
 
37
    public CurriedClosure(Closure uncurriedClosure, int i) {
 
38
        this(uncurriedClosure, new Object[]{Integer.valueOf(i)});
 
39
    }
 
40
 
 
41
    public Object[] getUncurriedArguments(Object[] arguments) {
 
42
        final Object newCurriedParams[] = new Object[curriedParams.length + arguments.length];
 
43
        System.arraycopy(curriedParams, 0, newCurriedParams, 0, curriedParams.length);
 
44
        System.arraycopy(arguments, 0, newCurriedParams, curriedParams.length, arguments.length);
 
45
        return newCurriedParams;        
 
46
    }
 
47
    
 
48
    public void setDelegate(Object delegate) {
 
49
        ((Closure)getOwner()).setDelegate(delegate);
 
50
    }
 
51
    
 
52
    public Object clone() {
 
53
        Closure uncurriedClosure = (Closure) ((Closure) getOwner()).clone();
 
54
        return new CurriedClosure(uncurriedClosure,curriedParams);
 
55
    }
 
56
    
 
57
    public Class[] getParameterTypes() {
 
58
        Class[] oldParams = ((Closure)getOwner()).getParameterTypes();
 
59
        Class[] newParams = new Class[oldParams.length-curriedParams.length];
 
60
        System.arraycopy(oldParams, curriedParams.length, newParams, 0, newParams.length);
 
61
        return newParams;  
 
62
    }
 
63
}