File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed
main/java/com/github/lwhite1/tablesaw
test/java/com/github/lwhite1/tablesaw/api Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change @@ -628,4 +628,24 @@ public int byteSize() {
628
628
public byte [] asBytes (int rowNumber ) {
629
629
return ByteBuffer .allocate (4 ).putInt (getInt (rowNumber )).array ();
630
630
}
631
+
632
+ public Selection isIn (String ... strings ) {
633
+ IntArrayList keys = new IntArrayList ();
634
+ for (String string : strings ) {
635
+ int key = lookupTable .get (string );
636
+ if (key >= 0 ) {
637
+ keys .add (key );
638
+ }
639
+ }
640
+
641
+ int i = 0 ;
642
+ Selection results = new BitmapBackedSelection ();
643
+ for (int next : values ) {
644
+ if (keys .contains (next )) {
645
+ results .add (i );
646
+ }
647
+ i ++;
648
+ }
649
+ return results ;
650
+ }
631
651
}
Original file line number Diff line number Diff line change
1
+ package com .github .lwhite1 .tablesaw .filtering .text ;
2
+
3
+ import com .github .lwhite1 .tablesaw .api .CategoryColumn ;
4
+ import com .github .lwhite1 .tablesaw .api .Table ;
5
+ import com .github .lwhite1 .tablesaw .columns .Column ;
6
+ import com .github .lwhite1 .tablesaw .columns .ColumnReference ;
7
+ import com .github .lwhite1 .tablesaw .filtering .ColumnFilter ;
8
+ import com .github .lwhite1 .tablesaw .util .Selection ;
9
+
10
+ import javax .annotation .concurrent .Immutable ;
11
+
12
+ /**
13
+ * A filtering that selects cells in which the string value is in the given array of strings
14
+ */
15
+ @ Immutable
16
+ public class TextIsIn extends ColumnFilter {
17
+
18
+ private String [] strings ;
19
+
20
+ public TextIsIn (ColumnReference reference , String ... strings ) {
21
+ super (reference );
22
+ this .strings = strings ;
23
+ }
24
+
25
+ @ Override
26
+ public Selection apply (Table relation ) {
27
+ Column column = relation .column (columnReference ().getColumnName ());
28
+ CategoryColumn textColumn = (CategoryColumn ) column ;
29
+ return textColumn .isIn (strings );
30
+ }
31
+ }
Original file line number Diff line number Diff line change @@ -83,6 +83,15 @@ public void testStartsWith() {
83
83
selection = categoryColumn .startsWith ("T" );
84
84
assertEquals ("Tennessee" , categoryColumn .get (selection .get (0 )));
85
85
assertEquals ("Texas" , categoryColumn .get (selection .get (1 )));
86
+ }
86
87
88
+ @ Test
89
+ public void testIsIn () {
90
+ CategoryColumn categoryColumn = CategoryColumn .create ("US States" );
91
+ categoryColumn .addAll (TestDataUtil .usStates ());
92
+ Selection selection = categoryColumn .isIn ("Alabama" , "Texas" );
93
+ assertEquals ("Alabama" , categoryColumn .get (selection .get (0 )));
94
+ assertEquals ("Texas" , categoryColumn .get (selection .get (1 )));
95
+ assertEquals (2 , selection .size ());
87
96
}
88
97
}
You can’t perform that action at this time.
0 commit comments