Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71db077

Browse files
committed
Clarify inclusive vs exclusive for isBetween API
1 parent cc72afd commit 71db077

File tree

6 files changed

+100
-11
lines changed

6 files changed

+100
-11
lines changed

core/src/main/java/tech/tablesaw/columns/ColumnReference.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
import tech.tablesaw.filtering.FloatGreaterThanOrEqualTo;
3030
import tech.tablesaw.filtering.FloatLessThan;
3131
import tech.tablesaw.filtering.FloatLessThanOrEqualTo;
32-
import tech.tablesaw.filtering.IntBetween;
32+
import tech.tablesaw.filtering.IntBetweenExclusive;
33+
import tech.tablesaw.filtering.IntBetweenInclusive;
3334
import tech.tablesaw.filtering.IntEqualTo;
3435
import tech.tablesaw.filtering.IntGreaterThan;
3536
import tech.tablesaw.filtering.IntGreaterThanOrEqualTo;
@@ -38,7 +39,8 @@
3839
import tech.tablesaw.filtering.IntLessThanOrEqualTo;
3940
import tech.tablesaw.filtering.IsMissing;
4041
import tech.tablesaw.filtering.IsNotMissing;
41-
import tech.tablesaw.filtering.LocalDateBetween;
42+
import tech.tablesaw.filtering.LocalDateBetweenExclusive;
43+
import tech.tablesaw.filtering.LocalDateBetweenInclusive;
4244
import tech.tablesaw.filtering.StringEqualTo;
4345
import tech.tablesaw.filtering.StringNotEqualTo;
4446
import tech.tablesaw.filtering.TimeEqualTo;
@@ -136,12 +138,20 @@ public Filter isEqualTo(ColumnReference reference) {
136138
return new ColumnEqualTo(this, reference);
137139
}
138140

139-
public Filter isBetween(int low, int high) {
140-
return new IntBetween(this, low, high);
141+
public Filter isBetweenIncluding(int low, int high) {
142+
return new IntBetweenInclusive(this, low, high);
141143
}
142144

143-
public Filter isBetween(LocalDate low, LocalDate high) {
144-
return new LocalDateBetween(this, low, high);
145+
public Filter isBetweenIncluding(LocalDate low, LocalDate high) {
146+
return new LocalDateBetweenInclusive(this, low, high);
147+
}
148+
149+
public Filter isBetweenExcluding(int low, int high) {
150+
return new IntBetweenExclusive(this, low, high);
151+
}
152+
153+
public Filter isBetweenExcluding(LocalDate low, LocalDate high) {
154+
return new LocalDateBetweenExclusive(this, low, high);
145155
}
146156

147157
public Filter isEqualTo(float value) {

core/src/main/java/tech/tablesaw/filtering/IntBetween.java renamed to core/src/main/java/tech/tablesaw/filtering/IntBetweenExclusive.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
import tech.tablesaw.columns.ColumnReference;
2020
import tech.tablesaw.util.Selection;
2121

22-
public class IntBetween extends ColumnFilter {
22+
public class IntBetweenExclusive extends ColumnFilter {
2323

2424
private final int low;
2525
private final int high;
2626

27-
public IntBetween(ColumnReference reference, int lowValue, int highValue) {
27+
public IntBetweenExclusive(ColumnReference reference, int lowValue, int highValue) {
2828
super(reference);
2929
this.low = lowValue;
3030
this.high = highValue;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package tech.tablesaw.filtering;
16+
17+
import tech.tablesaw.api.IntColumn;
18+
import tech.tablesaw.api.Table;
19+
import tech.tablesaw.columns.ColumnReference;
20+
import tech.tablesaw.util.Selection;
21+
22+
public class IntBetweenInclusive extends ColumnFilter {
23+
24+
private final int low;
25+
private final int high;
26+
27+
public IntBetweenInclusive(ColumnReference reference, int lowValue, int highValue) {
28+
super(reference);
29+
this.low = lowValue;
30+
this.high = highValue;
31+
}
32+
33+
public Selection apply(Table relation) {
34+
IntColumn intColumn = (IntColumn) relation.column(columnReference.getColumnName());
35+
Selection matches = intColumn.isGreaterThanOrEqualTo(low);
36+
matches.toBitmap().and(intColumn.isLessThanOrEqualTo(high).toBitmap());
37+
return matches;
38+
}
39+
}

core/src/main/java/tech/tablesaw/filtering/LocalDateBetween.java renamed to core/src/main/java/tech/tablesaw/filtering/LocalDateBetweenExclusive.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import tech.tablesaw.columns.ColumnReference;
2222
import tech.tablesaw.util.Selection;
2323

24-
public class LocalDateBetween extends ColumnFilter {
24+
public class LocalDateBetweenExclusive extends ColumnFilter {
2525
private final LocalDate low;
2626
private final LocalDate high;
2727

28-
public LocalDateBetween(ColumnReference reference, LocalDate lowValue, LocalDate highValue) {
28+
public LocalDateBetweenExclusive(ColumnReference reference, LocalDate lowValue, LocalDate highValue) {
2929
super(reference);
3030
this.low = lowValue;
3131
this.high = highValue;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package tech.tablesaw.filtering;
16+
17+
import java.time.LocalDate;
18+
19+
import tech.tablesaw.api.DateColumn;
20+
import tech.tablesaw.api.Table;
21+
import tech.tablesaw.columns.ColumnReference;
22+
import tech.tablesaw.util.Selection;
23+
24+
public class LocalDateBetweenInclusive extends ColumnFilter {
25+
private final LocalDate low;
26+
private final LocalDate high;
27+
28+
public LocalDateBetweenInclusive(ColumnReference reference, LocalDate lowValue, LocalDate highValue) {
29+
super(reference);
30+
this.low = lowValue;
31+
this.high = highValue;
32+
}
33+
34+
public Selection apply(Table relation) {
35+
DateColumn column = (DateColumn) relation.column(columnReference.getColumnName());
36+
Selection matches = column.isOnOrAfter(low);
37+
matches.and(column.isOnOrBefore(high));
38+
return matches;
39+
}
40+
}

core/src/test/java/tech/tablesaw/integration/ExamplesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void main(String[] args) throws Exception {
6262

6363
// Lets filtering out some of the rows. We're only interested in records with IDs between 524-624
6464

65-
Table filtered = table.selectWhere(QueryHelper.column("stop_id").isBetween(524, 624));
65+
Table filtered = table.selectWhere(QueryHelper.column("stop_id").isBetweenIncluding(524, 624));
6666
out(filtered.first(5));
6767

6868
// Write out the new CSV file

0 commit comments

Comments
 (0)