KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > type > TypeUtil


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/type/TypeUtil.java#6 $
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
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.olap.type;
11
12 import mondrian.olap.Hierarchy;
13 import mondrian.olap.Util;
14 import mondrian.olap.Category;
15
16 /**
17  * Utility methods relating to types.
18  *
19  * @author jhyde
20  * @since Feb 17, 2005
21  * @version $Id: //open/mondrian/src/main/mondrian/olap/type/TypeUtil.java#6 $
22  */

23 public class TypeUtil {
24     public static Hierarchy typeToHierarchy(Type type) {
25         if (type instanceof MemberType) {
26             return ((MemberType) type).getHierarchy();
27         } else if (type instanceof LevelType) {
28             return ((LevelType) type).getHierarchy();
29         } else if (type instanceof HierarchyType) {
30             return ((HierarchyType) type).getHierarchy();
31         } else if (type instanceof DimensionType) {
32             return ((DimensionType) type).getHierarchy();
33         } else {
34             throw Util.newInternal("not an mdx object");
35         }
36     }
37
38     /**
39      * Given a set type, returns the element type. Or its element type, if it
40      * is a set type. And so on.
41      */

42     public static Type stripSetType(Type type) {
43         while (type instanceof SetType) {
44             type = ((SetType) type).getElementType();
45         }
46         return type;
47     }
48
49     /**
50      * Converts a type to a member or tuple type.
51      * If it cannot, returns null.
52      */

53     public static Type toMemberOrTupleType(Type type) {
54         type = stripSetType(type);
55         if (type instanceof TupleType) {
56             return (TupleType) type;
57         } else {
58             return toMemberType(type);
59         }
60     }
61
62     /**
63      * Converts a type to a member type.
64      * If it is a set, strips the set.
65      * If it is a member type, returns the type unchanged.
66      * If it is a dimension, hierarchy or level type, converts it to
67      * a member type.
68      * If it is a tuple, number, string, or boolean, returns null.
69      */

70     public static MemberType toMemberType(Type type) {
71         type = stripSetType(type);
72         if (type instanceof MemberType) {
73             return (MemberType) type;
74         } else if (type instanceof DimensionType ||
75                 type instanceof HierarchyType ||
76                 type instanceof LevelType) {
77             return MemberType.forType(type);
78         } else {
79             return null;
80         }
81     }
82
83     /**
84      * Returns whether this type is union-compatible with another.
85      * In general, to be union-compatible, types must have the same
86      * dimensionality.
87      */

88     public static boolean isUnionCompatible(Type type1, Type type2) {
89         if (type1 instanceof TupleType) {
90             TupleType tupleType1 = (TupleType) type1;
91             if (type2 instanceof TupleType) {
92                 TupleType tupleType2 = (TupleType) type2;
93                 if (tupleType1.elementTypes.length ==
94                         tupleType2.elementTypes.length) {
95                     for (int i = 0; i < tupleType1.elementTypes.length; i++) {
96                         if (!isUnionCompatible(
97                                 tupleType1.elementTypes[i],
98                                 tupleType2.elementTypes[i])) {
99                             return false;
100                         }
101                     }
102                     return true;
103                 }
104             }
105             return false;
106         } else {
107             final MemberType memberType1 = toMemberType(type1);
108             if (memberType1 == null) {
109                 return false;
110             }
111             final MemberType memberType2 = toMemberType(type2);
112             if (memberType2 == null) {
113                 return false;
114             }
115             final Hierarchy hierarchy1 = memberType1.getHierarchy();
116             final Hierarchy hierarchy2 = memberType2.getHierarchy();
117             return equal(hierarchy1, hierarchy2);
118         }
119     }
120
121     private static boolean equal(
122             final Hierarchy hierarchy1, final Hierarchy hierarchy2) {
123         if (hierarchy1 == null ||
124                 hierarchy2 == null ||
125                 hierarchy2.getUniqueName().equals(
126                         hierarchy1.getUniqueName())) {
127             // They are compatible.
128
return true;
129         } else {
130             return false;
131         }
132     }
133
134     /**
135      * Returns whether a value of a given type can be evaluated to a scalar
136      * value.
137      *
138      * <p>The rules are as follows:<ul>
139      * <li>Clearly boolean, numeric and string expressions can be evaluated.
140      * <li>Member and tuple expressions can be interpreted as a scalar value.
141      * The expression is evaluated to establish the context where a measure
142      * can be evaluated.
143      * <li>Hierarchy and dimension expressions are implicitly
144      * converted into the current member, and evaluated as above.
145      * <li>Level expressions cannot be evaluated
146      * <li>Cube and Set (even sets with a single member) cannot be evaluated.
147      * </ul>
148      *
149      * @param type Type
150      * @return Whether an expression of this type can be evaluated to yield a
151      * scalar value.
152      */

153     public static boolean canEvaluate(Type type) {
154         return ! (type instanceof SetType ||
155                 type instanceof CubeType ||
156                 type instanceof LevelType);
157     }
158
159     /**
160      * Returns whether a type is a set type.
161      *
162      * @param type Type
163      * @return Whether a value of this type can be evaluated to yield a set.
164      */

165     public static boolean isSet(Type type) {
166         return type instanceof SetType;
167     }
168
169     public static boolean couldBeMember(Type type) {
170         return type instanceof MemberType ||
171                 type instanceof HierarchyType ||
172                 type instanceof DimensionType;
173     }
174
175     /**
176      * Converts a {@link Type} value to a {@link Category} ordinal.
177      */

178     public static int typeToCategory(Type type) {
179         if (type instanceof NumericType) {
180             return Category.Numeric;
181         } else if (type instanceof BooleanType) {
182             return Category.Logical;
183         } else if (type instanceof DimensionType) {
184             return Category.Dimension;
185         } else if (type instanceof HierarchyType) {
186             return Category.Hierarchy;
187         } else if (type instanceof MemberType) {
188             return Category.Member;
189         } else if (type instanceof LevelType) {
190             return Category.Level;
191         } else if (type instanceof SymbolType) {
192             return Category.Symbol;
193         } else if (type instanceof StringType) {
194             return Category.String;
195         } else if (type instanceof ScalarType) {
196             return Category.Value;
197         } else if (type instanceof SetType) {
198             return Category.Set;
199         } else if (type instanceof TupleType) {
200             return Category.Tuple;
201         } else {
202             throw Util.newInternal("Unknown type " + type);
203         }
204     }
205 }
206
207 // End TypeUtil.java
208
Popular Tags