KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > fun > FunInfo


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/fun/FunInfo.java#10 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2005-2006 Julian Hyde and others
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10
11 package mondrian.olap.fun;
12
13 import mondrian.olap.FunDef;
14 import mondrian.olap.Syntax;
15
16 import java.util.*;
17 import java.lang.reflect.Array JavaDoc;
18
19 /**
20  * Support class for the {@link mondrian.tui.CmdRunner} allowing one to view
21  * available functions and their syntax.
22  *
23  * @author Richard M. Emberson
24  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/FunInfo.java#10 $
25  */

26 public class FunInfo implements Comparable JavaDoc<FunInfo> {
27     private final Syntax syntax;
28     private final String JavaDoc name;
29     private final String JavaDoc description;
30     private final int[] returnTypes;
31     private final int[][] parameterTypes;
32     private String JavaDoc[] sigs;
33
34     static FunInfo make(Resolver resolver) {
35         FunDef funDef = resolver.getFunDef();
36         if (funDef != null) {
37             return new FunInfo(funDef);
38         } else if (resolver instanceof MultiResolver) {
39             return new FunInfo((MultiResolver) resolver);
40         } else {
41             return new FunInfo(resolver);
42         }
43     }
44
45     FunInfo(FunDef funDef) {
46         this.syntax = funDef.getSyntax();
47         this.name = funDef.getName();
48         this.returnTypes = new int[] { funDef.getReturnCategory() };
49         this.parameterTypes = new int[][] { funDef.getParameterCategories() };
50         this.sigs = makeSigs(syntax, name, returnTypes, parameterTypes);
51         this.description = funDef.getDescription();
52     }
53
54     FunInfo(MultiResolver multiResolver) {
55         this.syntax = multiResolver.getSyntax();
56         this.name = multiResolver.getName();
57         this.description = multiResolver.getDescription();
58
59         String JavaDoc[] signatures = multiResolver.getSignatures();
60         this.returnTypes = new int[signatures.length];
61         this.parameterTypes = new int[signatures.length][];
62         for (int i = 0; i < signatures.length; i++) {
63             returnTypes[i] = FunUtil.decodeReturnCategory(signatures[i]);
64             parameterTypes[i] = FunUtil.decodeParameterCategories(signatures[i]);
65         }
66         this.sigs = makeSigs(syntax, name, returnTypes, parameterTypes);
67     }
68
69     FunInfo(Resolver resolver) {
70         this.syntax = resolver.getSyntax();
71         this.name = resolver.getName();
72         this.description = resolver.getDescription();
73         this.returnTypes = null;
74         this.parameterTypes = null;
75         final String JavaDoc signature = resolver.getSignature();
76         this.sigs = signature == null ? new String JavaDoc[0] :
77                 new String JavaDoc[] {signature};
78     }
79
80     public String JavaDoc[] getSignatures() {
81         return sigs;
82     }
83
84     private static String JavaDoc[] makeSigs(
85             Syntax syntax,
86             String JavaDoc name, int[] returnTypes, int[][] parameterTypes) {
87         if (parameterTypes == null) {
88             return null;
89         }
90
91         String JavaDoc[] sigs = new String JavaDoc[parameterTypes.length];
92         for (int i = 0; i < sigs.length; i++) {
93             sigs[i] = syntax.getSignature(
94                     name, returnTypes[i], parameterTypes[i]);
95         }
96         return sigs;
97     }
98
99     /**
100      * Returns the syntactic type of the function.
101      */

102     public Syntax getSyntax() {
103         return this.syntax;
104     }
105
106     /**
107      * Returns the name of this function.
108      */

109     public String JavaDoc getName() {
110         return this.name;
111     }
112
113     /**
114      * Returns the description of this function.
115      */

116     public String JavaDoc getDescription() {
117         return this.description;
118     }
119
120     /**
121      * Returns the type of value returned by this function. Values are the same
122      * as those returned by {@link mondrian.olap.Exp#getCategory()}.
123      */

124     public int[] getReturnCategories() {
125         return this.returnTypes;
126     }
127
128     /**
129      * Returns the types of the arguments of this function. Values are the same
130      * as those returned by {@link mondrian.olap.Exp#getCategory()}. The
131      * 0<sup>th</sup> argument of methods and properties are the object they
132      * are applied to. Infix operators have two arguments, and prefix operators
133      * have one argument.
134      */

135     public int[][] getParameterCategories() {
136         return this.parameterTypes;
137     }
138
139     public int compareTo(FunInfo fi) {
140         int c = this.name.compareTo(fi.name);
141         if (c != 0) {
142             return c;
143         }
144         final List pcList = toList(this.getParameterCategories());
145         final String JavaDoc pc = pcList.toString();
146         final List otherPcList = toList(fi.getParameterCategories());
147         final String JavaDoc otherPc = otherPcList.toString();
148         return pc.compareTo(otherPc);
149     }
150
151     private static List toList(Object JavaDoc a) {
152         final List<Object JavaDoc> list = new ArrayList<Object JavaDoc>();
153         if (a == null) {
154             return list;
155         }
156         final int length = Array.getLength(a);
157         for (int i = 0; i < length; i++) {
158             final Object JavaDoc o = Array.get(a, i);
159             if (o.getClass().isArray()) {
160                 list.add(toList(o));
161             } else {
162                 list.add(o);
163             }
164         }
165         return list;
166     }
167 }
168
169 // End FunInfo.java
170
Popular Tags