KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/type/MemberType.java#4 $
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.*;
13
14 /**
15  * The type of an expression which represents a member.
16  *
17  * @author jhyde
18  * @since Feb 17, 2005
19  * @version $Id: //open/mondrian/src/main/mondrian/olap/type/MemberType.java#4 $
20  */

21 public class MemberType implements Type {
22     private final Hierarchy hierarchy;
23     private final Dimension dimension;
24     private final Level level;
25     private final Member member;
26     private final String JavaDoc digest;
27
28     public static final MemberType Unknown = new MemberType(null, null, null, null);
29
30     /**
31      * Creates a type representing a member.
32      *
33      * @param dimension
34      * @param hierarchy Hierarchy the member belongs to, or null if not known.
35      * @param level Level the member belongs to, or null if not known
36      * @param member The precise member, or null if not known
37      */

38     public MemberType(
39             Dimension dimension,
40             Hierarchy hierarchy,
41             Level level,
42             Member member) {
43         this.dimension = dimension;
44         this.hierarchy = hierarchy;
45         this.level = level;
46         this.member = member;
47         if (member != null) {
48             Util.assertPrecondition(level != null);
49             Util.assertPrecondition(member.getLevel() == level);
50         }
51         if (level != null) {
52             Util.assertPrecondition(hierarchy != null);
53             Util.assertPrecondition(level.getHierarchy() == hierarchy);
54         }
55         if (hierarchy != null) {
56             Util.assertPrecondition(dimension != null);
57             Util.assertPrecondition(hierarchy.getDimension() == dimension);
58         }
59         StringBuilder JavaDoc buf = new StringBuilder JavaDoc("MemberType<");
60         if (member != null) {
61             buf.append("member=").append(member.getUniqueName());
62         } else if (level != null) {
63             buf.append("level=").append(level.getUniqueName());
64         } else if (hierarchy != null) {
65             buf.append("hierarchy=").append(hierarchy.getUniqueName());
66         } else if (dimension != null) {
67             buf.append("dimension=").append(dimension.getUniqueName());
68         }
69         buf.append(">");
70         this.digest = buf.toString();
71     }
72
73     public static MemberType forDimension(Dimension dimension) {
74         return new MemberType(dimension, null, null, null);
75     }
76
77     public static MemberType forHierarchy(Hierarchy hierarchy) {
78         return new MemberType(hierarchy.getDimension(), hierarchy, null, null);
79     }
80
81     public static MemberType forLevel(Level level) {
82         return new MemberType(level.getDimension(), level.getHierarchy(), level, null);
83     }
84
85     public static MemberType forMember(Member member) {
86         return new MemberType(member.getDimension(), member.getHierarchy(), member.getLevel(), member);
87     }
88
89     public String JavaDoc toString() {
90         return digest;
91     }
92
93     public Hierarchy getHierarchy() {
94         return hierarchy;
95     }
96
97     public Level getLevel() {
98         return level;
99     }
100
101     public boolean usesDimension(Dimension dimension, boolean maybe) {
102         if (this.dimension == null) {
103             return maybe;
104         } else {
105             return this.dimension == dimension ||
106                     (maybe && this.dimension == null);
107         }
108     }
109
110     public Type getValueType() {
111         // todo: when members have more type information (double vs. integer
112
// vs. string), return better type if member != null.
113
return new ScalarType();
114     }
115
116     public Dimension getDimension() {
117         return dimension;
118     }
119
120     public static MemberType forType(Type type) {
121         if (type instanceof MemberType) {
122             return (MemberType) type;
123         } else {
124             return new MemberType(
125                     type.getDimension(),
126                     type.getHierarchy(),
127                     type.getLevel(),
128                     null);
129         }
130     }
131 }
132
133 // End MemberType.java
134
Popular Tags