1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| public class WildcardRowFilter<M,I> extends RowFilter<M,I> {
private final boolean endsWith;
private final boolean startsWith;
private final String filter;
private final int[] columns;
private final boolean casesensitive;
public WildcardRowFilter(String filter, boolean casesensitive, int...columns) {
this.casesensitive=casesensitive;
if ( filter.startsWith("*") ) {
endsWith=true;
filter = filter.substring(1);
}
else {
endsWith=false;
}
if ( filter.endsWith("*") ) {
startsWith=true;
filter = filter.substring(0,filter.length()-1);
}
else {
startsWith=false;
}
if ( !casesensitive ) {
filter = filter.toLowerCase();
}
this.filter=filter;
this.columns=checkIndices(columns);
}
@Override
public boolean include(Entry<? extends M, ? extends I> value){
int count = value.getValueCount();
if (columns.length > 0) {
for (int i = columns.length - 1; i >= 0; i--) {
int index = columns[i];
if (index < count) {
if (include(value, index)) {
return true;
}
}
}
} else {
while (--count >= 0) {
if (include(value, count)) {
return true;
}
}
}
return false;
}
private boolean include(Entry<? extends M, ? extends I> value, int count) {
String column = value.getStringValue(count);
if ( column==null ) {
return false;
}
if ( !casesensitive ) {
column = column.toLowerCase();
}
if ( startsWith ) {
if ( endsWith ) {
return column.contains(filter);
}
else {
return column.startsWith(filter);
}
}
else if ( endsWith ) {
return column.endsWith(filter);
}
else {
return column.equals(filter);
}
}
private static int[] checkIndices(int[] columns) {
for (int i = columns.length - 1; i >= 0; i--) {
if (columns[i] < 0) {
throw new IllegalArgumentException("Index must be >= 0");
}
}
return columns;
}
} |
Partager