Skip to content

Commit

Permalink
Remove optional to get features (#177) (#178)
Browse files Browse the repository at this point in the history
Optional is not required since api returns list instead of object.

Signed-off-by: Vijayan Balasubramanian <balasvij@amazon.com>
  • Loading branch information
opensearch-trigger-bot[bot] authored Oct 24, 2022
1 parent 646a0c0 commit bfaba9c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
13 changes: 6 additions & 7 deletions src/main/java/org/opensearch/geospatial/GeospatialParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.xcontent.XContentHelper;
Expand Down Expand Up @@ -77,18 +76,18 @@ public static Map<String, Object> convertToMap(BytesReference content) {
* getFeatures will return features from given map input. This function abstracts the logic to parse given input and returns
* list of Features if exists in Map format.
* @param geoJSON given input which may contain GeoJSON Object
* @return Returns an Optional with the List of Features as map if input is GeoJSON,
* else, returns an empty Optional instance.
* @return Returns List of Features as map if input is GeoJSON,
* else, returns an empty list.
*/
public static Optional<List<Map<String, Object>>> getFeatures(final Map<String, Object> geoJSON) {
public static List<Map<String, Object>> getFeatures(final Map<String, Object> geoJSON) {
final String type = extractValueAsString(geoJSON, TYPE_KEY);
Objects.requireNonNull(type, TYPE_KEY + " cannot be null");
if (Feature.TYPE.equalsIgnoreCase(type)) {
return Optional.of(List.of(geoJSON));
return List.of(geoJSON);
}
if (FeatureCollection.TYPE.equalsIgnoreCase(type)) {
return Optional.of(Collections.unmodifiableList(FeatureCollection.create(geoJSON).getFeatures()));
return Collections.unmodifiableList(FeatureCollection.create(geoJSON).getFeatures());
}
return Optional.empty();
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ private Optional<BulkRequestBuilder> prepareContentRequest(UploadGeoJSONRequestC
.stream()
.map(GeospatialParser::toStringObjectMap)
.map(GeospatialParser::getFeatures)
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(List::stream)
.map(this::createIndexRequestBuilder)
.map(indexRequestBuilder -> indexRequestBuilder.setIndex(content.getIndexName()))
Expand Down
19 changes: 9 additions & 10 deletions src/test/java/org/opensearch/geospatial/GeospatialParserTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.json.JSONArray;
import org.json.JSONObject;
Expand Down Expand Up @@ -77,10 +76,10 @@ public void testConvertToMap() {

public void testGetFeaturesWithGeoJSONFeature() {
Map<String, Object> geoJSON = GeospatialObjectBuilder.randomGeoJSONFeature(new JSONObject()).toMap();
Optional<List<Map<String, Object>>> features = GeospatialParser.getFeatures(geoJSON);
assertTrue(features.isPresent());
assertEquals(1, features.get().size());
assertEquals(features.get().get(0), geoJSON);
List<Map<String, Object>> features = GeospatialParser.getFeatures(geoJSON);
assertFalse(features.isEmpty());
assertEquals(1, features.size());
assertEquals(features.get(0), geoJSON);
}

public void testGetFeaturesWithGeoJSONFeatureCollection() {
Expand All @@ -90,16 +89,16 @@ public void testGetFeaturesWithGeoJSONFeatureCollection() {
features.put(GeospatialObjectBuilder.randomGeoJSONFeature(new JSONObject()));

JSONObject collection = GeospatialObjectBuilder.buildGeoJSONFeatureCollection(features);
Optional<List<Map<String, Object>>> featureList = GeospatialParser.getFeatures(collection.toMap());
assertTrue(featureList.isPresent());
assertEquals(featureList.get().size(), features.length());
List<Map<String, Object>> featureList = GeospatialParser.getFeatures(collection.toMap());
assertFalse(featureList.isEmpty());
assertEquals(featureList.size(), features.length());
}

public void testGetFeaturesWithUnSupportedType() {
Map<String, Object> geoJSON = new HashMap<>();
geoJSON.put(Feature.TYPE_KEY, "invalid-type");
Optional<List<Map<String, Object>>> features = GeospatialParser.getFeatures(geoJSON);
assertFalse(features.isPresent());
List<Map<String, Object>> features = GeospatialParser.getFeatures(geoJSON);
assertTrue(features.isEmpty());
}

}

0 comments on commit bfaba9c

Please sign in to comment.