Home / java sweetener restrictions collections criteria / Sweetener - Collection Restrictions - Part III - Custom restrictions

Sweetener - Collection Restrictions - Part III - Custom restrictions

21 May 2015 on java, sweetener, restrictions, collections, and criteria 2 minutes

Sweetener contains many pre-defined restrictions, but we cannot predict all use-cases of our mechanism. For this reason we have prepared CustomRestriction. If none of the restrictions prepared by us is what you are looking for, you can create your own restriction. Preparing CustomRestriction is simple and intuitive. To create your own Restriction you need to create new class which extends CustomRestriction class. Let’s assume that we want to create restriction which checks whether string starts with some substring. The example restriction might look as follows:

public class StartsWithRestriction extends CustomRestriction {

    private String prefix;

    public StartsWithRestriction(String fieldName, String prefix) {
        super(fieldName);
        this.prefix = prefix;
    }

    @Override
    public boolean satisfies(Object fieldValue) {

        if (!(fieldValue instanceof String)) {
            throw new AccessToFieldException("Type mismatch. Expected String but was "
                    + fieldValue.getClass().getCanonicalName());
        }

        return ((String) fieldValue).startsWith(prefix);
    }

}

As you can see, StartsWithRestriction extends CustomRestriction. CustomRestriction expects fieldName as a constructor parameter. As other constructor parameters you can pass values that will be necessary in your restrictions. In our example, this is the prefix. CustomRestriction requires implementation of one method: satisfies(Object fieldValue). As the parameter of this method you can expect fieldValue which is indicate by first parameter in the constructor (String fieldName). In body of public boolean satisfies(Object fieldValue) method you can check whether value has correct format, correct type. As result of this method you should return one simple boolean information: whether the restriction is satisfied. In our example we check whether fieldValue starts with prefix.

To use such restriction just invoke add method on Criteria object.

Collection<Person> filteredList = Collections.filter(people,
                Criteria.newCriteria().add(new StartsWithRestriction("name", "J")));

Thanks to this mechanism, you can easily extend the functionality of criteria / restrictions.
Simple, fast and extensible mechanism


Previous

Sweetener - Collection Restrictions - Part II - Filtering by date

Filtering objects by using Date class is a very common problem. There are times when we want find objects, for which one of the field is in some time range. Such an example may be find all people who were born before the year 1988. However, there is problem with...

Next

Jsolve - Projects in Maven Central

Jsolve - Projects in Maven Central! From now Sweetener, Oven and Type-converter are in Maven Cetral. Current versions of our libraries are: sweetener: 1.0.0, oven: 1.0.1, type-converter: 1.0.1. In order to use it in maven project just add the below dependency to your pom file.