KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > runtime > Arity


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
15  * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
16  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
17  *
18  * Alternatively, the contents of this file may be used under the terms of
19  * either of the GNU General Public License Version 2 or later (the "GPL"),
20  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
21  * in which case the provisions of the GPL or the LGPL are applicable instead
22  * of those above. If you wish to allow use of your version of this file only
23  * under the terms of either the GPL or the LGPL, and not to allow others to
24  * use your version of this file under the terms of the CPL, indicate your
25  * decision by deleting the provisions above and replace them with the notice
26  * and other provisions required by the GPL or the LGPL. If you do not delete
27  * the provisions above, a recipient may use your version of this file under
28  * the terms of any one of the CPL, the GPL or the LGPL.
29  ***** END LICENSE BLOCK *****/

30 package org.jruby.runtime;
31
32 import java.io.Serializable JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35 import org.jruby.Ruby;
36 import org.jruby.ast.AttrAssignNode;
37 import org.jruby.ast.CallNode;
38 import org.jruby.ast.Node;
39 import org.jruby.ast.types.IArityNode;
40 import org.jruby.runtime.builtin.IRubyObject;
41
42 /**
43  * The arity of a method is the number of arguments it takes.
44  */

45 public final class Arity implements Serializable JavaDoc {
46     private static final long serialVersionUID = 1L;
47     private static final Map JavaDoc arities = new HashMap JavaDoc();
48     private final int value;
49
50     private Arity(int value) {
51         this.value = value;
52     }
53
54     public static Arity createArity(int value) {
55         Integer JavaDoc integerValue = new Integer JavaDoc(value);
56         Arity result;
57         synchronized (arities) {
58             result = (Arity) arities.get(integerValue);
59             if (result == null) {
60                 result = new Arity(value);
61                 arities.put(integerValue, result);
62             }
63         }
64         return result;
65     }
66
67     public static Arity fixed(int arity) {
68         assert arity >= 0;
69         return createArity(arity);
70     }
71
72     public static Arity optional() {
73         return createArity(-1);
74     }
75
76     public static Arity required(int minimum) {
77         assert minimum >= 0;
78         return createArity(-(1 + minimum));
79     }
80
81     public static Arity noArguments() {
82         return createArity(0);
83     }
84
85     public static Arity singleArgument() {
86         return createArity(1);
87     }
88
89     public static Arity twoArguments() {
90         return createArity(2);
91     }
92     
93     public static Arity procArityOf(Node node) {
94         if (node instanceof AttrAssignNode && node != null) {
95             node = ((AttrAssignNode) node).getArgsNode();
96         }
97         if (node == null) {
98             return Arity.optional();
99         } else if (node instanceof IArityNode) {
100             return ((IArityNode) node).getArity();
101         } else if (node instanceof CallNode) {
102             return Arity.singleArgument();
103         }
104
105         throw new Error JavaDoc("unexpected type " + node.getClass() + " at " + node.getPosition());
106     }
107
108     public int getValue() {
109         return value;
110     }
111
112     public void checkArity(Ruby runtime, IRubyObject[] args) {
113         if (isFixed()) {
114             if (args.length != required()) {
115                 throw runtime.newArgumentError("wrong number of arguments(" + args.length + " for " + required() + ")");
116             }
117         } else {
118             if (args.length < required()) {
119                 throw runtime.newArgumentError("wrong number of arguments(" + args.length + " for " + required() + ")");
120             }
121         }
122     }
123
124     public boolean isFixed() {
125         return value >= 0;
126     }
127
128     private int required() {
129         if (value < 0) {
130             return -(1 + value);
131         }
132         return value;
133     }
134
135     public boolean equals(Object JavaDoc other) {
136         return this == other;
137     }
138
139     public int hashCode() {
140         return value;
141     }
142
143     public String JavaDoc toString() {
144         if(isFixed()) {
145             return "Fixed" + required();
146         } else {
147             return "Opt";
148         }
149     }
150 }
151
Popular Tags