1 10 package mondrian.olap.type; 11 12 import mondrian.olap.Hierarchy; 13 import mondrian.olap.Util; 14 import mondrian.olap.Category; 15 16 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 42 public static Type stripSetType(Type type) { 43 while (type instanceof SetType) { 44 type = ((SetType) type).getElementType(); 45 } 46 return type; 47 } 48 49 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 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 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 return true; 129 } else { 130 return false; 131 } 132 } 133 134 153 public static boolean canEvaluate(Type type) { 154 return ! (type instanceof SetType || 155 type instanceof CubeType || 156 type instanceof LevelType); 157 } 158 159 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 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 | Popular Tags |