KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > xmla > XMLA_PropValAssign


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.xmla;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.log4j.Logger;
22
23 /**
24  * Bean
25  * assign a property values to binary coded integers
26  */

27 public class XMLA_PropValAssign {
28
29   private Map JavaDoc propMap = new HashMap JavaDoc();
30   private int firstBit = 0;
31   static private int LASTBIT = 15; // use FONTSIZE, < 2**16
32

33   static Logger logger = Logger.getLogger(XMLA_PropValAssign.class);
34
35   /**
36    * @param values - possible value assignments
37    */

38   public void addProp(String JavaDoc prop, List JavaDoc values) {
39     // how many bits do we need ? log(2, #values)
40
int nValues = values.size();
41     if (nValues == 0)
42       return;
43     int nBits = 1;
44     int n = (nValues - 1) / 2;
45     while (n > 0) {
46       ++nBits;
47       n = n / 2;
48     }
49
50     int mask = 1 << nBits;
51     mask = mask - 1; // 2**nBits -1
52
mask = mask << firstBit;
53
54     if (firstBit + nBits > LASTBIT) {
55       // not enough bits to encode the property values (FONT_SIZE)
56
logger.error("could not encode property values " + prop + " #" + nValues);
57       return;
58     }
59     
60     List JavaDoc vAssignList = new ArrayList JavaDoc();
61     int iBitVal = 0;
62     for (Iterator JavaDoc iter = values.iterator(); iter.hasNext();) {
63       String JavaDoc val = (String JavaDoc) iter.next();
64       ValAssign vAssign = new ValAssign();
65       vAssign.setVal(val);
66       vAssign.setBitMask(mask);
67       int bitVal = iBitVal << firstBit;
68       vAssign.setBitVal(bitVal);
69       vAssignList.add(vAssign);
70       ++iBitVal;
71     }
72     propMap.put(prop, vAssignList);
73     firstBit += nBits;
74   }
75
76   /**
77    * @param prop
78    * @return the value assignment list
79    */

80   public List JavaDoc getValAssignList(String JavaDoc prop) {
81     return (List JavaDoc) propMap.get(prop);
82   }
83
84   /**
85    * @return
86    */

87   public Map JavaDoc getPropMap() {
88     return propMap;
89   }
90
91   /**
92    * @param map
93    */

94   public void setPropMap(Map JavaDoc map) {
95     propMap = map;
96   }
97   
98   /**
99    * @return
100    */

101   public int getFirstBit() {
102     return firstBit;
103   }
104
105   /**
106    * @param i
107    */

108   public void setFirstBit(int i) {
109     firstBit = i;
110   }
111
112   /**
113    * Bean - assigned property value
114    */

115   public static class ValAssign {
116     private String JavaDoc val;
117     private int bitMask;
118     private int bitVal;
119
120     /**
121      * @return
122      */

123     public String JavaDoc getVal() {
124       return val;
125     }
126
127     /**
128      * @param string
129      */

130     public void setVal(String JavaDoc string) {
131       val = string;
132     }
133
134     /**
135      * @return
136      */

137     public int getBitMask() {
138       return bitMask;
139     }
140
141     /**
142      * @param bitMask
143      */

144     public void setBitMask(int bitMask) {
145       this.bitMask = bitMask;
146     }
147
148     /**
149      * @return
150      */

151     public int getBitVal() {
152       return bitVal;
153     }
154
155     /**
156      * @param i
157      */

158     public void setBitVal(int i) {
159       bitVal = i;
160     }
161
162   } // ValAssign
163

164
165 } // XMLA_PropValAssign
166
Popular Tags