001 /*
002 // $Id: LevelType.java 482 2012-01-05 23:27:27Z jhyde $
003 //
004 // Licensed to Julian Hyde under one or more contributor license
005 // agreements. See the NOTICE file distributed with this work for
006 // additional information regarding copyright ownership.
007 //
008 // Julian Hyde licenses this file to you under the Apache License,
009 // Version 2.0 (the "License"); you may not use this file except in
010 // compliance with the License. You may obtain a copy of the License at:
011 //
012 // http://www.apache.org/licenses/LICENSE-2.0
013 //
014 // Unless required by applicable law or agreed to in writing, software
015 // distributed under the License is distributed on an "AS IS" BASIS,
016 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 // See the License for the specific language governing permissions and
018 // limitations under the License.
019 */
020 package org.olap4j.type;
021
022 import org.olap4j.OlapException;
023 import org.olap4j.metadata.*;
024
025 /**
026 * The type of an expression which represents a level.
027 *
028 * @author jhyde
029 * @since Feb 17, 2005
030 * @version $Id: LevelType.java 482 2012-01-05 23:27:27Z jhyde $
031 */
032 public class LevelType implements Type {
033 private final Dimension dimension;
034 private final Hierarchy hierarchy;
035 private final Level level;
036 private final String digest;
037
038 /**
039 * Creates a type representing a level.
040 *
041 * @param dimension Dimension which values of this type must belong to, or
042 * null if not known
043 *
044 * @param hierarchy Hierarchy which values of this type must belong to, or
045 * null if not known
046 *
047 * @param level Level which values of this type must belong to, or null if
048 * not known
049 */
050 public LevelType(
051 Dimension dimension,
052 Hierarchy hierarchy,
053 Level level)
054 {
055 this.dimension = dimension;
056 this.hierarchy = hierarchy;
057 this.level = level;
058 if (level != null) {
059 assert hierarchy != null : "hierarchy != null";
060 assert level.getHierarchy() == hierarchy
061 : "level.getHierarchy() == hierarchy";
062 }
063 if (hierarchy != null) {
064 assert dimension != null : "dimension != null";
065 assert hierarchy.getDimension() == dimension
066 : "hierarchy.getDimension() == dimension";
067 }
068 StringBuilder buf = new StringBuilder("LevelType<");
069 if (level != null) {
070 buf.append("level=").append(level.getUniqueName());
071 } else if (hierarchy != null) {
072 buf.append("hierarchy=").append(hierarchy.getUniqueName());
073 } else if (dimension != null) {
074 buf.append("dimension=").append(dimension.getUniqueName());
075 }
076 buf.append(">");
077 this.digest = buf.toString();
078 }
079
080 // not part of public olap4j API
081 private static LevelType forType(Type type) throws OlapException {
082 return new LevelType(
083 type.getDimension(),
084 type.getHierarchy(),
085 type.getLevel());
086 }
087
088 public boolean usesDimension(Dimension dimension, boolean maybe) {
089 if (this.dimension == null) {
090 return maybe;
091 } else {
092 return this.dimension.equals(dimension);
093 }
094 }
095
096 public Dimension getDimension() {
097 return dimension;
098 }
099
100 public Hierarchy getHierarchy() {
101 return hierarchy;
102 }
103
104 public Level getLevel() {
105 return level;
106 }
107
108 public String toString() {
109 return digest;
110 }
111 }
112
113 // End LevelType.java
114