FeatureFilter { public FeatureCollection filter() { List<Feature> featuresList = builder.getFeatureCollection().features(); ArrayList<Feature> filteredFeatures = new ArrayList<>(); HashMap<String, Comparison> comparisons = new HashMap<>(); EqualToComparison equalToComparison = new EqualToComparison(); RegexComparison regexComparison = new RegexComparison(); comparisons.put(equalToComparison.getFunctionName(), equalToComparison); comparisons.put(regexComparison.getFunctionName(), regexComparison); if (featuresList != null) { ArrayList<FilterCondition> filterConditions = builder.getFilterConditions(); if (filterConditions.size() > 0) { for (Feature feature : featuresList) { boolean skipFeature = false; if (builder.getSortProperty() != null && !feature.hasProperty(builder.getSortProperty())) { continue; } for (FilterCondition filterCondition : filterConditions) { String propertyName = filterCondition.getPropertyName(); if (feature.hasProperty(propertyName)) { Comparison comparison = comparisons.get(filterCondition.getComparisionType()); if (comparison != null && !comparison.compare( feature.getStringProperty(propertyName), filterCondition.getValueType(), (String) filterCondition.getValue())) { skipFeature = true; break; } } else { skipFeature = true; } } if (!skipFeature) { filteredFeatures.add(feature); } } } else { filteredFeatures = new ArrayList<>(featuresList); } } return FeatureCollection.fromFeatures(filteredFeatures); } private  FeatureFilter(@NonNull Builder builder); FeatureCollection filter(); FeatureCollection getFeatureCollection();  }
@Test public void filterShouldReturnOnlyFeaturesWithFilterProperty() { ArrayList<Feature> featuresList = new ArrayList<>(); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "efgh") , new GeoJSONFeature.Property("task-status", "positive") , new GeoJSONFeature.Property("position", 0))); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "defg") , new GeoJSONFeature.Property("position", 1))); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "abcd") , new GeoJSONFeature.Property("position", 2))); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "cdef") , new GeoJSONFeature.Property("task-status", "positive") , new GeoJSONFeature.Property("position", 3))); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "bcde") , new GeoJSONFeature.Property("task-status", "positive") , new GeoJSONFeature.Property("position", 4))); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "bcde") , new GeoJSONFeature.Property("task-status", "positive") , new GeoJSONFeature.Property("position", 5))); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("task-status", "positive") , new GeoJSONFeature.Property("position", 6))); FeatureCollection actualFeatureCollection = new FeatureFilter.Builder(FeatureCollection.fromFeatures(featuresList)) .whereRegex("sample-string", "[a-zA-z]*") .whereRegex("task-status", "[a-zA-Z]*") .build() .filter(); List<Feature> actualFeatures = actualFeatureCollection.features(); Assert.assertEquals(4, actualFeatures.size()); Assert.assertEquals(0, (int) actualFeatures.get(0).getNumberProperty("position")); Assert.assertEquals(3, (int) actualFeatures.get(1).getNumberProperty("position")); Assert.assertEquals(4, (int) actualFeatures.get(2).getNumberProperty("position")); Assert.assertEquals(5, (int) actualFeatures.get(3).getNumberProperty("position")); }
@Test public void filterShouldReturnAllFeaturesIfThereAreNoConditions() { int expectedFeaturesLen = 10; ArrayList<Feature> featuresList = new ArrayList<>(); for (int i = 0; i < expectedFeaturesLen; i++) { featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "efgh") , new GeoJSONFeature.Property("task-status", "positive") , new GeoJSONFeature.Property("position", 0))); } FeatureCollection actualFeatureCollection = new FeatureFilter.Builder(FeatureCollection.fromFeatures(featuresList)) .build() .filter(); Assert.assertEquals(expectedFeaturesLen, actualFeatureCollection.features().size()); }
@Test public void filterShouldReturnEmptyFeatureCollectionWhenGivenEmptyFeatureCollection() { int expectedFeaturesLen = 0; ArrayList<Feature> featuresList = new ArrayList<>(); FeatureCollection actualFeatureCollection = new FeatureFilter.Builder(FeatureCollection.fromFeatures(featuresList)) .build() .filter(); Assert.assertEquals(expectedFeaturesLen, actualFeatureCollection.features().size()); }
@Test public void filterShouldFilterWithChainedProperties() { ArrayList<Feature> featuresList = new ArrayList<>(); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "efgh") , new GeoJSONFeature.Property("task-status", "positive") , new GeoJSONFeature.Property("position", 0))); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "defg") , new GeoJSONFeature.Property("position", 1))); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "abcd") , new GeoJSONFeature.Property("position", 2))); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "cdef") , new GeoJSONFeature.Property("task-status", "positive") , new GeoJSONFeature.Property("position", 3))); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "bcde") , new GeoJSONFeature.Property("task-status", "positive") , new GeoJSONFeature.Property("position", 4))); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("sample-string", "bcde") , new GeoJSONFeature.Property("task-status", "positive") , new GeoJSONFeature.Property("position", 5))); featuresList.add(generateRandomFeatureWithProperties(new GeoJSONFeature.Property("task-status", "positive") , new GeoJSONFeature.Property("position", 6))); FeatureCollection actualFeatureCollection = new FeatureFilter.Builder(FeatureCollection.fromFeatures(featuresList)) .whereRegex("sample-string", ".*[abc]+.*") .whereEq("task-status", "positive") .build() .filter(); List<Feature> actualFeatures = actualFeatureCollection.features(); Assert.assertEquals(3, actualFeatures.size()); Assert.assertEquals(3, (int) actualFeatures.get(0).getNumberProperty("position")); Assert.assertEquals(4, (int) actualFeatures.get(1).getNumberProperty("position")); Assert.assertEquals(5, (int) actualFeatures.get(2).getNumberProperty("position")); }