KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > olap > model > impl > PositionSpan


1 /*
2  * ====================================================================
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) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.olap.model.impl;
14
15 import java.util.List JavaDoc;
16 import java.util.ListIterator JavaDoc;
17
18 import com.tonbeller.jpivot.olap.model.Axis;
19 import com.tonbeller.jpivot.olap.model.Member;
20 import com.tonbeller.jpivot.olap.model.Position;
21
22 /**
23  * finds a group (span) of positions that are related in terms of a start position and a member.
24  * The group consists of the positions before and after the start position, that share equal
25  * members on the member's level and on the higher levels.
26  * <p>
27  * Example 1: returns the first 2 positions. A's and I's are equal within the group
28  * <pre>
29  * A I U <- start position, member = I
30  * A I V
31  * A J U
32  * A J V
33  * </pre>
34  * <p>
35  * Example 2: returns the first 2 positions. A's and I's are equal within the group
36  * <pre>
37  * A I U
38  * A I V <- start position, member = I
39  * B I U
40  * B I V
41  * </pre>
42  */

43
44 public class PositionSpan {
45   Axis axis;
46   Position position;
47   Member member;
48
49   int memberIndex; // index of member in position
50
int startIndex; // index of the first matching position
51
int endIndex; // index + 1 of the last matching position
52

53   /**
54    * Constructor for PositionSpan.
55    */

56   public PositionSpan(Axis axis, Position position, Member member) {
57     this.axis = axis;
58     this.position = position;
59     this.member = member;
60
61     memberIndex = indexOf(position.getMembers(), member);
62     initStartIndex();
63     initEndIndex();
64   }
65   
66   void initStartIndex() {
67     List JavaDoc list = axis.getPositions();
68     int index = list.indexOf(position);
69     ListIterator JavaDoc li = list.listIterator(index);
70     loop: while (li.hasPrevious()) {
71       Position p = (Position)li.previous();
72       if (!match(p))
73         break loop;
74       index -= 1;
75     }
76     startIndex = index;
77   }
78
79   void initEndIndex() {
80     List JavaDoc list = axis.getPositions();
81     int index = list.indexOf(position);
82     ListIterator JavaDoc li = list.listIterator(index);
83     loop: while (li.hasNext()) {
84       Position p = (Position)li.next();
85       if (!match(p))
86         break loop;
87       index += 1;
88     }
89     endIndex = index;
90   }
91
92   boolean match(Position p) {
93     Member[] m1 = p.getMembers();
94     Member[] m2 = position.getMembers();
95     for (int i = 0; i <= memberIndex; i++)
96       if (!m1[i].equals(m2[i]))
97         return false;
98     return true;
99   }
100   
101
102   int indexOf(Object JavaDoc[] array, Object JavaDoc obj) {
103     for (int i = 0; i < array.length; i++)
104       if (array[i] == obj)
105         return i;
106     return -1;
107   }
108
109   /**
110    * Returns the axis.
111    * @return Axis
112    */

113   public Axis getAxis() {
114     return axis;
115   }
116
117   /**
118    * Returns (index + 1) of the last matching position
119    * @return int
120    */

121   public int getEndIndex() {
122     return endIndex;
123   }
124
125   /**
126    * Returns the member.
127    * @return Member
128    */

129   public Member getMember() {
130     return member;
131   }
132
133   /**
134    * Returns the position.
135    * @return Position
136    */

137   public Position getPosition() {
138     return position;
139   }
140
141   /**
142    * Returns the index of the first matching position
143    * @return int
144    */

145   public int getStartIndex() {
146     return startIndex;
147   }
148
149   /**
150    * Returns the index of member in positions
151    * @return int
152    */

153   public int getMemberIndex() {
154     return memberIndex;
155   }
156
157 }
158
Popular Tags