001/* 002// This software is subject to the terms of the Eclipse Public License v1.0 003// Agreement, available at the following URL: 004// http://www.eclipse.org/legal/epl-v10.html. 005// You must accept the terms of that agreement to use this software. 006// 007// Copyright (C) 2005-2005 Julian Hyde 008// Copyright (C) 2005-2011 Pentaho 009// All Rights Reserved. 010*/ 011package mondrian.olap.type; 012 013import mondrian.olap.*; 014 015import java.util.List; 016 017/** 018 * Set type. 019 * 020 * @author jhyde 021 * @since Feb 17, 2005 022 */ 023public class SetType implements Type { 024 025 private final Type elementType; 026 private final String digest; 027 028 /** 029 * Creates a type representing a set of elements of a given type. 030 * 031 * @param elementType The type of the elements in the set, or null if not 032 * known 033 */ 034 public SetType(Type elementType) { 035 if (elementType != null) { 036 assert elementType instanceof MemberType 037 || elementType instanceof TupleType; 038 } 039 this.elementType = elementType; 040 this.digest = "SetType<" + elementType + ">"; 041 } 042 043 public int hashCode() { 044 return digest.hashCode(); 045 } 046 047 public boolean equals(Object obj) { 048 if (obj instanceof SetType) { 049 SetType that = (SetType) obj; 050 return Util.equals(this.elementType, that.elementType); 051 } else { 052 return false; 053 } 054 } 055 056 public String toString() { 057 return digest; 058 } 059 060 /** 061 * Returns the type of the elements of this set. 062 * 063 * @return the type of the elements in this set 064 */ 065 public Type getElementType() { 066 return elementType; 067 } 068 069 public boolean usesDimension(Dimension dimension, boolean definitely) { 070 if (elementType == null) { 071 return definitely; 072 } 073 return elementType.usesDimension(dimension, definitely); 074 } 075 076 public boolean usesHierarchy(Hierarchy hierarchy, boolean definitely) { 077 if (elementType == null) { 078 return definitely; 079 } 080 return elementType.usesHierarchy(hierarchy, definitely); 081 } 082 083 public Dimension getDimension() { 084 return elementType == null 085 ? null 086 : elementType.getDimension(); 087 } 088 089 public Hierarchy getHierarchy() { 090 return elementType == null 091 ? null 092 : elementType.getHierarchy(); 093 } 094 095 public Level getLevel() { 096 return elementType == null 097 ? null 098 : elementType.getLevel(); 099 } 100 101 public int getArity() { 102 return elementType.getArity(); 103 } 104 105 public Type computeCommonType(Type type, int[] conversionCount) { 106 if (!(type instanceof SetType)) { 107 return null; 108 } 109 SetType that = (SetType) type; 110 final Type mostGeneralElementType = 111 this.getElementType().computeCommonType( 112 that.getElementType(), conversionCount); 113 if (mostGeneralElementType == null) { 114 return null; 115 } 116 return new SetType(mostGeneralElementType); 117 } 118 119 public boolean isInstance(Object value) { 120 if (!(value instanceof List)) { 121 return false; 122 } 123 List list = (List) value; 124 for (Object o : list) { 125 if (!elementType.isInstance(o)) { 126 return false; 127 } 128 } 129 return true; 130 } 131} 132 133// End SetType.java