Over a million developers have joined DZone.

Request Validation and Filtering by Flags – Filtering an Item

DZone's Guide to

Request Validation and Filtering by Flags – Filtering an Item

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

On a previous post, I introduced a system requirement of validating and filtering a request by setting flags on it.

Reference: Introduction

In this post I want to show the filtering system.

Here are general UML diagrams of the filtering components and sequence.

Filtering UML Diagram

General Components

public interface Item {
String getName();
public interface Request {
Set getFlags();
List getItems();

Filter Mechanism (as described in the UML above)

public interface Filter extends Predicate {
String errorMessage();

FilterEngine is a cool part, which takes several Filters and apply to each the items. Below you can see the code of it. Above, the sequence diagram shows how it’s done.

public class FiltersEngine {
public FiltersEngine() {
public ItemsFilterResponse applyFilters(List filters, List items) {
List validItems = Lists.newLinkedList(items);
List invalidItemInformations = Lists.newLinkedList();
for (Filter validator : filters) {
ItemsFilterResponse responseFromFilter = responseFromFilter(validItems, validator);
validItems = responseFromFilter.getValidItems();
return new ItemsFilterResponse(validItems, invalidItemInformations);
private ItemsFilterResponse responseFromFilter(List items, Filter filter) {
List validItems = Lists.newLinkedList();
List invalidItemInformations = Lists.newLinkedList();
for (Item item : items) {
if (filter.apply(item)) {
} else {
invalidItemInformations.add(new InvalidItemInformation(item, filter.errorMessage()));
return new ItemsFilterResponse(validItems, invalidItemInformations);

And of course, we need to test it:

Some explanation about the test
You can see that I don’t care about the implementation of Filter. Actually, I don’t even have any implementation of it.
I also don’t have implementation of the Item nor the request.
You can see an example of how to create a BaseMatcher to be used with assertThat(…)

Try to see whether it is ‘clean’. Can you understand the story of the code? Can you tell what the code does by reading it line by line?

On the following post I will show how I applied the flag mapping to select the correct filters for a request.

You can find all the code in: https://github.com/eyalgo/request-validation

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.


Published at DZone with permission of Eyal Golan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}