001 /* 002 // $Id: //open/mondrian/src/main/mondrian/olap/type/HierarchyType.java#10 $ 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 * The type of an expression which represents a hierarchy. 016 * 017 * @author jhyde 018 * @since Feb 17, 2005 019 * @version $Id: //open/mondrian/src/main/mondrian/olap/type/HierarchyType.java#10 $ 020 */ 021 public class HierarchyType implements Type { 022 private final Dimension dimension; 023 private final Hierarchy hierarchy; 024 private final String digest; 025 026 public static final HierarchyType Unknown = new HierarchyType(null, null); 027 028 /** 029 * Creates a type representing a hierarchy. 030 * 031 * @param dimension Dimension that values of this type must belong to, or 032 * null if the dimension is unknown 033 * @param hierarchy Hierarchy that values of this type must belong to, 034 * null if the hierarchy is unknown 035 */ 036 public HierarchyType(Dimension dimension, Hierarchy hierarchy) { 037 this.dimension = dimension; 038 this.hierarchy = hierarchy; 039 StringBuilder buf = new StringBuilder("HierarchyType<"); 040 if (hierarchy != null) { 041 buf.append("hierarchy=").append(hierarchy.getUniqueName()); 042 } else if (dimension != null) { 043 buf.append("dimension=").append(dimension.getUniqueName()); 044 } 045 buf.append(">"); 046 this.digest = buf.toString(); 047 } 048 049 public static HierarchyType forHierarchy(Hierarchy hierarchy) { 050 return new HierarchyType(hierarchy.getDimension(), hierarchy); 051 } 052 053 public static HierarchyType forType(Type type) { 054 return new HierarchyType(type.getDimension(), type.getHierarchy()); 055 } 056 057 public boolean usesDimension(Dimension dimension, boolean definitely) { 058 return this.dimension == dimension || 059 (!definitely && this.dimension == null); 060 } 061 062 public Dimension getDimension() { 063 return dimension; 064 } 065 066 public Hierarchy getHierarchy() { 067 return hierarchy; 068 } 069 070 public Level getLevel() { 071 return null; 072 } 073 074 public String toString() { 075 return digest; 076 } 077 078 public int hashCode() { 079 return digest.hashCode(); 080 } 081 082 public boolean equals(Object obj) { 083 if (obj instanceof HierarchyType) { 084 HierarchyType that = (HierarchyType) obj; 085 return Util.equals(this.hierarchy, that.hierarchy) 086 && Util.equals(this.dimension, that.dimension); 087 } 088 return false; 089 } 090 091 public Type computeCommonType(Type type, int[] conversionCount) { 092 if (!(type instanceof HierarchyType)) { 093 return null; 094 } 095 HierarchyType that = (HierarchyType) type; 096 if (this.getHierarchy() != null 097 && this.getHierarchy().equals(that.getHierarchy())) { 098 return this; 099 } 100 if (this.getDimension() != null 101 && this.getDimension().equals(that.getDimension())) { 102 return new HierarchyType( 103 this.getDimension(), 104 null); 105 } 106 return HierarchyType.Unknown; 107 } 108 } 109 110 // End HierarchyType.java