KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > type > LevelType


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/type/LevelType.java#6 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2005-2006 Julian Hyde
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.olap.type;
11
12 import mondrian.olap.Level;
13 import mondrian.olap.Dimension;
14 import mondrian.olap.Hierarchy;
15 import mondrian.olap.Util;
16
17 /**
18  * The type of an expression which represents a level.
19  *
20  * @author jhyde
21  * @since Feb 17, 2005
22  * @version $Id: //open/mondrian/src/main/mondrian/olap/type/LevelType.java#6 $
23  */

24 public class LevelType implements Type {
25     private final Dimension dimension;
26     private final Hierarchy hierarchy;
27     private final Level level;
28     private final String JavaDoc digest;
29
30     /**
31      * Creates a type representing a level.
32      *
33      * @param dimension
34      * @param hierarchy Hierarchy which values of this type must belong to, or
35      * null if not known
36      * @param level Level which values of this type must belong to, or null if
37      */

38     public LevelType(Dimension dimension, Hierarchy hierarchy, Level level) {
39         this.dimension = dimension;
40         this.hierarchy = hierarchy;
41         this.level = level;
42         if (level != null) {
43             Util.assertPrecondition(hierarchy != null, "hierarchy != null");
44             Util.assertPrecondition(level.getHierarchy() == hierarchy,
45                     "level.getHierarchy() == hierarchy");
46         }
47         if (hierarchy != null) {
48             Util.assertPrecondition(dimension != null, "dimension != null");
49             Util.assertPrecondition(hierarchy.getDimension() == dimension,
50                     "hierarchy.getDimension() == dimension");
51         }
52         StringBuilder JavaDoc buf = new StringBuilder JavaDoc("LevelType<");
53         if (level != null) {
54             buf.append("level=").append(level.getUniqueName());
55         } else if (hierarchy != null) {
56             buf.append("hierarchy=").append(hierarchy.getUniqueName());
57         } else if (dimension != null) {
58             buf.append("dimension=").append(dimension.getUniqueName());
59         }
60         buf.append(">");
61         this.digest = buf.toString();
62     }
63
64     public static LevelType forType(Type type) {
65         return new LevelType(
66                 type.getDimension(),
67                 type.getHierarchy(),
68                 type.getLevel());
69
70     }
71
72     public static LevelType forLevel(Level level) {
73         return new LevelType(
74                 level.getDimension(),
75                 level.getHierarchy(),
76                 level);
77     }
78
79     public boolean usesDimension(Dimension dimension, boolean maybe) {
80         if (this.dimension == null) {
81             return maybe;
82         } else {
83             return this.dimension == dimension;
84         }
85     }
86
87     public Dimension getDimension() {
88         return dimension;
89     }
90
91     public Hierarchy getHierarchy() {
92         return hierarchy;
93     }
94
95     public Level getLevel() {
96         return level;
97     }
98
99     public String JavaDoc toString() {
100         return digest;
101     }
102 }
103
104 // End LevelType.java
105
Popular Tags