Source:
public QueryResult<Alert> query(final AlertQuery query) { final Mapper<models.cassandra.Alert> mapper = _mappingManager.mapper(models.cassandra.Alert.class); final models.cassandra.Alert.AlertQueries accessor = mapper.getManager().createAccessor(models.cassandra.Alert.AlertQueries.class); final Result<models.cassandra.Alert> result = accessor.getAlertsForOrganization(query.getOrganization().getId()); final List<models.cassandra.Alert> allAlerts = result.all(); final int start = query.getOffset().orElse(0); Stream<models.cassandra.Alert> alertStream = allAlerts.stream(); if (query.getCluster().isPresent()) { alertStream = alertStream.filter(alert -> alert.getCluster().equals(query.getCluster().get())); } if (query.getService().isPresent()) { alertStream = alertStream.filter(alert -> alert.getService().equals(query.getService().get())); } if (query.getContext().isPresent()) { alertStream = alertStream.filter(alert -> alert.getContext().equals(query.getContext().get())); } if (query.getContains().isPresent()) { alertStream = alertStream.filter(alert -> { final String contains = query.getContains().get(); return alert.getService().contains(contains) || alert.getCluster().contains(contains) || alert.getMetric().contains(contains) || alert.getOperator().toString().contains(contains) || alert.getName().contains(contains) || alert.getStatistic().contains(contains); }); } final List<Alert> alerts = alertStream .map(this::convertFromCassandraAlert) .collect(Collectors.toList()); final List<Alert> paginated = alerts.stream().skip(start).limit(query.getLimit()).collect(Collectors.toList()); return new DefaultQueryResult<>(paginated, alerts.size()); }

Target:
public QueryResult<Alert> query(final AlertQuery query) { final Mapper<models.cassandra.Alert> mapper = _mappingManager.mapper(models.cassandra.Alert.class); final models.cassandra.Alert.AlertQueries accessor = mapper.getManager().createAccessor(models.cassandra.Alert.AlertQueries.class); final Result<models.cassandra.Alert> result = accessor.getAlertsForOrganization(query.getOrganization().getId()); final Spliterator<models.cassandra.Alert> allAlerts = result.spliterator(); final int start = query.getOffset().orElse(0); Stream<models.cassandra.Alert> alertStream = StreamSupport.stream(allAlerts, false); if (query.getCluster().isPresent()) { alertStream = alertStream.filter(alert -> alert.getCluster().equals(query.getCluster().get())); } if (query.getService().isPresent()) { alertStream = alertStream.filter(alert -> alert.getService().equals(query.getService().get())); } if (query.getContext().isPresent()) { alertStream = alertStream.filter(alert -> alert.getContext().equals(query.getContext().get())); } if (query.getContains().isPresent()) { alertStream = alertStream.filter(alert -> { final String contains = query.getContains().get(); return alert.getService().contains(contains) || alert.getCluster().contains(contains) || alert.getMetric().contains(contains) || alert.getOperator().toString().contains(contains) || alert.getName().contains(contains) || alert.getStatistic().contains(contains); }); } final List<Alert> alerts = alertStream .map(this::convertFromCassandraAlert) .collect(Collectors.toList()); final List<Alert> paginated = alerts.stream().skip(start).limit(query.getLimit()).collect(Collectors.toList()); return new DefaultQueryResult<>(paginated, alerts.size()); }