Skip to content

Commit

Permalink
MONDRIAN: Oops!
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//open/mondrian/": change = 3343]
  • Loading branch information
julianhyde committed Mar 12, 2005
1 parent 2f105ee commit 822256a
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions src/main/mondrian/olap/type/TypeUtil.java
@@ -0,0 +1,125 @@
/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// (C) Copyright 2005-2005 Julian Hyde
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/
package mondrian.olap.type;

import mondrian.olap.Hierarchy;
import mondrian.olap.Util;
import mondrian.olap.Level;

/**
* Utility methods relating to types.
*
* @author jhyde
* @since Feb 17, 2005
* @version $Id$
*/
public class TypeUtil {
public static Hierarchy typeToHierarchy(Type type) {
if (type instanceof MemberType) {
return ((MemberType) type).getHierarchy();
} else if (type instanceof LevelType) {
return ((LevelType) type).getHierarchy();
} else if (type instanceof HierarchyType) {
return ((HierarchyType) type).getHierarchy();
} else if (type instanceof DimensionType) {
return ((DimensionType) type).getHierarchy();
} else {
throw Util.newInternal("not an mdx object");
}
}

public static Level typeToLevel(Type type) {
if (type instanceof LevelType) {
return ((LevelType) type).getLevel();
} else {
throw Util.newInternal("not an mdx object");
}
}

/**
* Given a set type, returns the element type. Or its element type, if it
* is a set type. And so on.
*/
public static Type stripSetType(Type type) {
while (type instanceof SetType) {
type = ((SetType) type).getElementType();
}
return type;
}

/**
* Converts a type to a member (or tuple) type. If it is a set, strips
* the set. If it is a dimension, hierarchy or level type, converts it to
* a member.
*/
public static Type toMemberType(Type type) {
type = stripSetType(type);
if (type instanceof DimensionType) {
DimensionType dimensionType = (DimensionType) type;
return new MemberType(dimensionType.getHierarchy(),
dimensionType.getLevel(), null);
} else if (type instanceof HierarchyType) {
HierarchyType hierarchyType = (HierarchyType) type;
return new MemberType(hierarchyType.getHierarchy(),
hierarchyType.getLevel(), null);
} else if (type instanceof LevelType) {
LevelType levelType = (LevelType) type;
return new MemberType(levelType.getHierarchy(),
levelType.getLevel(), null);
} else {
return type;
}
}

/**
* Returns whether this type is union-compatible with another.
* In general, to be union-compatible, types must have the same
* dimensionality.
*/
public static boolean isUnionCompatible(Type type1, Type type2) {
if (type1 instanceof MemberType) {
MemberType memberType1 = (MemberType) type1;
if (type2 instanceof MemberType) {
MemberType memberType2 = (MemberType) type2;
final Hierarchy hierarchy1 = memberType1.getHierarchy();
final Hierarchy hierarchy2 = memberType2.getHierarchy();
if (hierarchy1 == null ||
hierarchy2 == null ||
hierarchy2.getUniqueName().equals(
hierarchy1.getUniqueName())) {
// They are compatible.
return true;
}
}
return false;
} else if (type1 instanceof TupleType) {
TupleType tupleType1 = (TupleType) type1;
if (type2 instanceof TupleType) {
TupleType tupleType2 = (TupleType) type2;
if (tupleType1.elementTypes.length ==
tupleType2.elementTypes.length) {
for (int i = 0; i < tupleType1.elementTypes.length; i++) {
if (!isUnionCompatible(
tupleType1.elementTypes[i],
tupleType2.elementTypes[i])) {
return false;
}
}
return true;
}
}
return false;
} else {
return false;
}
}
}

// End TypeUtil.java

0 comments on commit 822256a

Please sign in to comment.