com.softicar.platform.common.container.data.table.in.memory.InMemoryDataTableStringFilter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.container.data.table.in.memory;
import com.softicar.platform.common.container.data.table.DataTableStringFilterOperator;
import com.softicar.platform.common.container.data.table.IDataTableColumn;
import com.softicar.platform.common.core.exceptions.SofticarUnknownEnumConstantException;
/**
* Implementation of {@link IInMemoryDataTableFilter} for {@link String}.
*
* @author Alexander Schmidt
*/
class InMemoryDataTableStringFilter implements IInMemoryDataTableFilter {
private final IDataTableColumn column;
private final DataTableStringFilterOperator operator;
private final String string;
private final String lowerCaseString;
public InMemoryDataTableStringFilter(IDataTableColumn column, DataTableStringFilterOperator operator, String string) {
this.column = column;
this.operator = operator;
this.string = stripSqlWildcards(string);
this.lowerCaseString = this.string.toLowerCase();
}
@Override
public boolean applyFilter(R row) {
switch (operator) {
case LIKE:
return getValueOrEmptyString(column, row).toLowerCase().contains(lowerCaseString);
case NOT_LIKE:
return !getValueOrEmptyString(column, row).toLowerCase().contains(lowerCaseString);
case REGEXP:
return getValueOrEmptyString(column, row).matches(string);
case NOT_REGEXP:
return !getValueOrEmptyString(column, row).matches(string);
default:
throw new SofticarUnknownEnumConstantException(operator);
}
}
private String getValueOrEmptyString(IDataTableColumn column, R row) {
String value = column.getValue(row);
if (value == null) {
value = "";
}
return value;
}
/**
* FIXME: this should not be necessary - why are those wildcards added in
* the first place?
*/
private String stripSqlWildcards(String string) {
if (string.startsWith("%") && string.endsWith("%")) {
return string.substring(1, string.length() - 1);
} else {
return string;
}
}
}