KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > column > BooleanColumn


1 package prefuse.data.column;
2
3 import java.util.BitSet JavaDoc;
4
5 import prefuse.data.DataReadOnlyException;
6 import prefuse.data.DataTypeException;
7
8 /**
9  * Column implementation storing boolean values. Uses a BitSet representation
10  * for space efficient storage.
11  *
12  * @author <a HREF="http://jheer.org">jeffrey heer</a>
13  */

14 public class BooleanColumn extends AbstractColumn {
15
16     private BitSet JavaDoc m_bits;
17     private int m_size;
18     
19     /**
20      * Create an empty BooleanColumn.
21      */

22     public BooleanColumn() {
23         this(0, 10, false);
24     }
25     
26     /**
27      * Create a new BooleanColumn.
28      * @param nrows the initial size of the column
29      */

30     public BooleanColumn(int nrows) {
31         this(nrows, nrows, false);
32     }
33
34     /**
35      * Create a new BooleanColumn.
36      * @param nrows the initial size of the column
37      * @param capacity the initial capacity of the column
38      * @param defaultValue the default value for the column
39      */

40     public BooleanColumn(int nrows, int capacity, boolean defaultValue) {
41         super(boolean.class, new Boolean JavaDoc(defaultValue));
42         if ( capacity < nrows ) {
43             throw new IllegalArgumentException JavaDoc(
44                 "Capacity value can not be less than the row count.");
45         }
46         m_bits = new BitSet JavaDoc(capacity);
47         m_bits.set(0, capacity, defaultValue);
48         m_size = nrows;
49     }
50     
51     // ------------------------------------------------------------------------
52
// Column Metadata
53

54     /**
55      * @see prefuse.data.column.Column#getRowCount()
56      */

57     public int getRowCount() {
58         return m_size;
59     }
60     
61     /**
62      * @see prefuse.data.column.Column#setMaximumRow(int)
63      */

64     public void setMaximumRow(int nrows) {
65         if ( nrows > m_size ) {
66             m_bits.set(m_size, nrows,
67                 ((Boolean JavaDoc)m_defaultValue).booleanValue());
68         }
69         m_size = nrows;
70     }
71
72     // ------------------------------------------------------------------------
73
// Data Access Methods
74

75     /**
76      * @see prefuse.data.column.Column#get(int)
77      */

78     public Object JavaDoc get(int row) {
79         return new Boolean JavaDoc(getBoolean(row));
80     }
81
82     /**
83      * @see prefuse.data.column.Column#set(java.lang.Object, int)
84      */

85     public void set(Object JavaDoc val, int row) throws DataTypeException {
86         if ( m_readOnly ) {
87             throw new DataReadOnlyException();
88         } else if ( val != null ) {
89             if ( val instanceof Boolean JavaDoc ) {
90                 setBoolean(((Boolean JavaDoc)val).booleanValue(), row);
91             } else if ( val instanceof String JavaDoc ) {
92                 setString((String JavaDoc)val, row);
93             } else {
94                 throw new DataTypeException(val.getClass());
95             }
96         } else {
97             throw new DataTypeException("Column does not accept null values");
98         }
99     }
100
101     // ------------------------------------------------------------------------
102
// Data Type Convenience Methods
103

104     /**
105      * @see prefuse.data.column.AbstractColumn#getBoolean(int)
106      */

107     public boolean getBoolean(int row) throws DataTypeException {
108         if ( row < 0 || row > m_size ) {
109             throw new IllegalArgumentException JavaDoc("Row index out of bounds: "+row);
110         }
111         return m_bits.get(row);
112     }
113
114     /**
115      * @see prefuse.data.column.AbstractColumn#setBoolean(boolean, int)
116      */

117     public void setBoolean(boolean val, int row) throws DataTypeException {
118         if ( m_readOnly ) {
119             throw new DataReadOnlyException();
120         } else if ( row < 0 || row >= m_size ) {
121             throw new IllegalArgumentException JavaDoc("Row index out of bounds: "+row);
122         }
123         // get the previous value
124
boolean prev = m_bits.get(row);
125         
126         // exit early if no change
127
if ( prev == val ) return;
128         
129         // set the new value
130
m_bits.set(row, val);
131         
132         // fire a change event
133
fireColumnEvent(row, prev);
134     }
135     
136 // /**
137
// * @see prefuse.data.column.AbstractColumn#getString(int)
138
// */
139
// public String getString(int row) throws DataTypeException {
140
// return String.valueOf(getBoolean(row));
141
// }
142
//
143
// /**
144
// * @see prefuse.data.column.AbstractColumn#setString(java.lang.String, int)
145
// */
146
// public void setString(String val, int row) throws DataTypeException {
147
// boolean b;
148
// if ( "true".equalsIgnoreCase(val) ) {
149
// b = true;
150
// } else if ( "false".equalsIgnoreCase(val) ) {
151
// b = false;
152
// } else {
153
// throw new IllegalArgumentException(
154
// "Input string does not represent a boolean value.");
155
// }
156
// setBoolean(b, row);
157
// }
158

159 } // end of class BooleanColumn
160
Popular Tags