001 /*
002 // $Id: //open/mondrian-release/3.0/src/main/mondrian/olap/type/SetType.java#2 $
003 // This software is subject to the terms of the Common Public License
004 // Agreement, available at the following URL:
005 // http://www.opensource.org/licenses/cpl.html.
006 // Copyright (C) 2005-2008 Julian Hyde
007 // All Rights Reserved.
008 // You must accept the terms of that agreement to use this software.
009 */
010 package mondrian.olap.type;
011
012 import mondrian.olap.*;
013
014 /**
015 * Set type.
016 *
017 * @author jhyde
018 * @since Feb 17, 2005
019 * @version $Id: //open/mondrian-release/3.0/src/main/mondrian/olap/type/SetType.java#2 $
020 */
021 public class SetType implements Type {
022
023 private final Type elementType;
024 private final String digest;
025
026 /**
027 * Creates a type representing a set of elements of a given type.
028 *
029 * @param elementType The type of the elements in the set, or null if not
030 * known
031 */
032 public SetType(Type elementType) {
033 if (elementType != null) {
034 assert elementType instanceof MemberType ||
035 elementType instanceof TupleType;
036 }
037 this.elementType = elementType;
038 this.digest = "SetType<" + elementType + ">";
039 }
040
041 public int hashCode() {
042 return digest.hashCode();
043 }
044
045 public boolean equals(Object obj) {
046 if (obj instanceof SetType) {
047 SetType that = (SetType) obj;
048 return Util.equals(this.elementType, that.elementType);
049 } else {
050 return false;
051 }
052 }
053
054 public String toString() {
055 return digest;
056 }
057
058 /**
059 * Returns the type of the elements of this set.
060 *
061 * @return the type of the elements in this set
062 */
063 public Type getElementType() {
064 return elementType;
065 }
066
067 public boolean usesDimension(Dimension dimension, boolean definitely) {
068 if (elementType == null) {
069 return definitely;
070 }
071 return elementType.usesDimension(dimension, definitely);
072 }
073
074 public Dimension getDimension() {
075 return elementType == null ? null :
076 elementType.getDimension();
077 }
078
079 public Hierarchy getHierarchy() {
080 return elementType == null ? null :
081 elementType.getHierarchy();
082 }
083
084 public Level getLevel() {
085 return elementType == null ? null :
086 elementType.getLevel();
087 }
088
089 /**
090 * Returns the dimensionality of this SetType. If set contains members,
091 * returns 1, otherwise returns the width of the tuples.
092 *
093 * @return Dimensionality of this SetType
094 */
095 public int getArity() {
096 return elementType instanceof TupleType ?
097 ((TupleType) elementType).elementTypes.length :
098 1;
099 }
100
101 public Type computeCommonType(Type type, int[] conversionCount) {
102 if (!(type instanceof SetType)) {
103 return null;
104 }
105 SetType that = (SetType) type;
106 final Type mostGeneralElementType =
107 this.getElementType().computeCommonType(
108 that.getElementType(), conversionCount);
109 if (mostGeneralElementType == null) {
110 return null;
111 }
112 return new SetType(mostGeneralElementType);
113 }
114 }
115
116 // End SetType.java