use of dev.hawala.xns.level4.filing.FilingCommon.FilterType in project dodo by devhawala.
the class AttributeUtils method buildPredicate.
public static iValueFilter buildPredicate(CHOICE<FilterType> filter) {
// handle structural filter types first
switch(filter.getChoice()) {
case all:
return fe -> true;
case none:
return fe -> false;
case not:
{
FilterRecord tmpFilter = (FilterRecord) filter.getContent();
iValueFilter tmpPredicate = buildPredicate(tmpFilter.value);
return fe -> !tmpPredicate.isElligible(fe);
}
case or:
case and:
{
FilterSequenceRecord tmpFilters = (FilterSequenceRecord) filter.getContent();
if (tmpFilters.seq.size() == 0) {
return fe -> false;
}
iValueFilter[] tmpPredicates = new iValueFilter[tmpFilters.seq.size()];
for (int i = 0; i < tmpPredicates.length; i++) {
tmpPredicates[i] = buildPredicate(tmpFilters.seq.get(i));
}
if (filter.getChoice() == FilterType.or) {
return fe -> {
for (iValueFilter p : tmpPredicates) {
if (p.isElligible(fe)) {
return true;
}
}
return false;
};
} else {
return fe -> {
for (iValueFilter p : tmpPredicates) {
if (!p.isElligible(fe)) {
return false;
}
}
return true;
};
}
}
default:
}
// value based filter, special case pattern-matching
if (filter.getChoice() == FilterType.matches) {
Attribute attr = (Attribute) filter.getContent();
if (attr.type.get() == FilingCommon.atName) {
String pattern = convertPattern(attr.getAsString());
return fe -> fe.getLcName().matches(pattern);
} else if (attr.type.get() == FilingCommon.atPathname) {
// TODO: build special pathname pattern matcher
// temp: use a simple (non-path-capable) matcher...
String pattern = convertPattern(attr.getAsString());
return fe -> fe.getPathname().toLowerCase().matches(pattern);
} else {
new ScopeTypeErrorRecord(ArgumentProblem.unreasonable, ScopeType.filter).raise();
}
}
// value comparison based filter
final Predicate<Integer> evaluator;
switch(filter.getChoice()) {
case less:
evaluator = i -> (i < 0);
break;
case lessOrEqual:
evaluator = i -> (i <= 0);
break;
case equal:
evaluator = i -> (i == 0);
break;
case notEqual:
evaluator = i -> (i != 0);
break;
case greaterOrEqual:
evaluator = i -> (i >= 0);
break;
case greater:
evaluator = i -> (i > 0);
break;
default:
// none of the comparators...
evaluator = i -> false;
}
Attribute attr = ((FilterAttribute) filter.getContent()).attribute;
int type = (int) (attr.type.get() & 0xFFFF);
switch(type) {
case FilingCommon.atChecksum:
{
int val = attr.getAsCardinal();
return fe -> compare(val, fe.getChecksum(), evaluator);
}
case FilingCommon.atChildrenUniquelyNamed:
{
boolean val = attr.getAsBoolean();
return fe -> compare(val, fe.isChildrenUniquelyNamed(), evaluator);
}
case FilingCommon.atCreatedBy:
{
String val = attr.getAsString().toLowerCase();
return fe -> compare(val, fe.getCreatedBy().toLowerCase(), evaluator);
}
case FilingCommon.atCreatedOn:
{
long val = attr.getAsTime();
return fe -> compare(val, fe.getCreatedOn(), evaluator);
}
case FilingCommon.atFileID:
{
long val = attr.getAsFileID();
return fe -> compare(val, fe.getFileID(), evaluator);
}
case FilingCommon.atIsDirectory:
{
boolean val = attr.getAsBoolean();
return fe -> compare(val, fe.isDirectory(), evaluator);
}
case FilingCommon.atModifiedBy:
{
String val = attr.getAsString().toLowerCase();
return fe -> compare(val, fe.getModifiedBy().toLowerCase(), evaluator);
}
case FilingCommon.atModifiedOn:
{
long val = attr.getAsTime();
return fe -> compare(val, fe.getModifiedOn(), evaluator);
}
case FilingCommon.atName:
{
String val = attr.getAsString().toLowerCase();
return fe -> compare(val, fe.getName().toLowerCase(), evaluator);
}
case FilingCommon.atNumberOfChildren:
{
int val = attr.getAsCardinal();
return fe -> compare(val, fe.getNumberOfChildren(), evaluator);
}
case FilingCommon.atParentID:
{
long val = attr.getAsFileID();
return fe -> compare(val, fe.getParentID(), evaluator);
}
case FilingCommon.atPosition:
{
long val = attr.getAsPosition();
return fe -> compare(val, fe.getPosition(), evaluator);
}
case FilingCommon.atReadBy:
{
String val = attr.getAsString().toLowerCase();
return fe -> compare(val, fe.getReadBy().toLowerCase(), evaluator);
}
case FilingCommon.atReadOn:
{
long val = attr.getAsTime();
return fe -> compare(val, fe.getReadOn(), evaluator);
}
case FilingCommon.atDataSize:
{
long val = attr.getAsLongCardinal();
return fe -> compare(val, fe.getDataSize(), evaluator);
}
case FilingCommon.atType:
{
long val = attr.getAsLongCardinal();
return fe -> compare(val, fe.getType(), evaluator);
}
case FilingCommon.atVersion:
{
int val = attr.getAsCardinal();
// will temporarily match any version
if (val == 0 || val == 0xFFFF) {
return fe -> true;
}
// end temp
return fe -> compare(val, fe.getVersion(), evaluator);
}
case FilingCommon.atPathname:
{
String val = attr.getAsString().toLowerCase();
return fe -> compare(val, fe.getPathname().toLowerCase(), evaluator);
}
case FilingCommon.atStoredSize:
{
long val = attr.getAsLongCardinal();
return fe -> compare(val, fe.getStoredSize(), evaluator);
}
case FilingCommon.atSubtreeSize:
{
long val = attr.getAsLongCardinal();
return fe -> compare(val, fe.getSubtreeSize(), evaluator);
}
case FilingCommon.atSubtreeSizeLimit:
{
long val = attr.getAsLongCardinal();
return fe -> compare(val, fe.getSubtreeSizeLimit(), evaluator);
}
default:
break;
}
// or unsupported (ordering, accessLists, uninterpreted) or meaningless (e.g. isTemporary) attribute
return fe -> false;
}
Aggregations