Search in sources :

Example 1 with FilterType

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;
}
Also used : Arrays(java.util.Arrays) FilterSequenceRecord(dev.hawala.xns.level4.filing.FilingCommon.FilterSequenceRecord) ScopeType(dev.hawala.xns.level4.filing.FilingCommon.ScopeType) CHOICE(dev.hawala.xns.level3.courier.CHOICE) FilterAttribute(dev.hawala.xns.level4.filing.FilingCommon.FilterAttribute) Ordering(dev.hawala.xns.level4.filing.FilingCommon.Ordering) dev.hawala.xns.level4.filing.fs.iValueGetter(dev.hawala.xns.level4.filing.fs.iValueGetter) AccessList(dev.hawala.xns.level4.filing.FilingCommon.AccessList) dev.hawala.xns.level4.filing.fs.iValueSetter(dev.hawala.xns.level4.filing.fs.iValueSetter) FileEntry(dev.hawala.xns.level4.filing.fs.FileEntry) ArrayList(java.util.ArrayList) FilterRecord(dev.hawala.xns.level4.filing.FilingCommon.FilterRecord) ArgumentProblem(dev.hawala.xns.level4.filing.FilingCommon.ArgumentProblem) Attribute(dev.hawala.xns.level4.filing.FilingCommon.Attribute) Predicate(java.util.function.Predicate) AccessType(dev.hawala.xns.level4.filing.FilingCommon.AccessType) Interpretation(dev.hawala.xns.level4.filing.FilingCommon.Interpretation) FsConstants(dev.hawala.xns.level4.filing.fs.FsConstants) ScopeTypeErrorRecord(dev.hawala.xns.level4.filing.FilingCommon.ScopeTypeErrorRecord) List(java.util.List) UndefinedErrorRecord(dev.hawala.xns.level4.filing.FilingCommon.UndefinedErrorRecord) FilterType(dev.hawala.xns.level4.filing.FilingCommon.FilterType) UninterpretedAttribute(dev.hawala.xns.level4.filing.fs.UninterpretedAttribute) AttributeSequence(dev.hawala.xns.level4.filing.FilingCommon.AttributeSequence) dev.hawala.xns.level4.filing.fs.iValueFilter(dev.hawala.xns.level4.filing.fs.iValueFilter) AccessEntry(dev.hawala.xns.level4.filing.fs.AccessEntry) dev.hawala.xns.level4.filing.fs.iValueFilter(dev.hawala.xns.level4.filing.fs.iValueFilter) FilterAttribute(dev.hawala.xns.level4.filing.FilingCommon.FilterAttribute) FilterSequenceRecord(dev.hawala.xns.level4.filing.FilingCommon.FilterSequenceRecord) FilterAttribute(dev.hawala.xns.level4.filing.FilingCommon.FilterAttribute) Attribute(dev.hawala.xns.level4.filing.FilingCommon.Attribute) UninterpretedAttribute(dev.hawala.xns.level4.filing.fs.UninterpretedAttribute) ScopeTypeErrorRecord(dev.hawala.xns.level4.filing.FilingCommon.ScopeTypeErrorRecord) FilterRecord(dev.hawala.xns.level4.filing.FilingCommon.FilterRecord)

Aggregations

CHOICE (dev.hawala.xns.level3.courier.CHOICE)1 AccessList (dev.hawala.xns.level4.filing.FilingCommon.AccessList)1 AccessType (dev.hawala.xns.level4.filing.FilingCommon.AccessType)1 ArgumentProblem (dev.hawala.xns.level4.filing.FilingCommon.ArgumentProblem)1 Attribute (dev.hawala.xns.level4.filing.FilingCommon.Attribute)1 AttributeSequence (dev.hawala.xns.level4.filing.FilingCommon.AttributeSequence)1 FilterAttribute (dev.hawala.xns.level4.filing.FilingCommon.FilterAttribute)1 FilterRecord (dev.hawala.xns.level4.filing.FilingCommon.FilterRecord)1 FilterSequenceRecord (dev.hawala.xns.level4.filing.FilingCommon.FilterSequenceRecord)1 FilterType (dev.hawala.xns.level4.filing.FilingCommon.FilterType)1 Interpretation (dev.hawala.xns.level4.filing.FilingCommon.Interpretation)1 Ordering (dev.hawala.xns.level4.filing.FilingCommon.Ordering)1 ScopeType (dev.hawala.xns.level4.filing.FilingCommon.ScopeType)1 ScopeTypeErrorRecord (dev.hawala.xns.level4.filing.FilingCommon.ScopeTypeErrorRecord)1 UndefinedErrorRecord (dev.hawala.xns.level4.filing.FilingCommon.UndefinedErrorRecord)1 AccessEntry (dev.hawala.xns.level4.filing.fs.AccessEntry)1 FileEntry (dev.hawala.xns.level4.filing.fs.FileEntry)1 FsConstants (dev.hawala.xns.level4.filing.fs.FsConstants)1 UninterpretedAttribute (dev.hawala.xns.level4.filing.fs.UninterpretedAttribute)1 dev.hawala.xns.level4.filing.fs.iValueFilter (dev.hawala.xns.level4.filing.fs.iValueFilter)1