Skip to content

Commit 4465452

Browse files
authored
Remove optional to get features (#177)
Optional is not required since api returns list instead of object. Signed-off-by: Vijayan Balasubramanian <[email protected]>
1 parent 63c97b0 commit 4465452

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

src/main/java/org/opensearch/geospatial/GeospatialParser.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.util.List;
1313
import java.util.Map;
1414
import java.util.Objects;
15-
import java.util.Optional;
1615

1716
import org.opensearch.common.bytes.BytesReference;
1817
import org.opensearch.common.xcontent.XContentHelper;
@@ -77,18 +76,18 @@ public static Map<String, Object> convertToMap(BytesReference content) {
7776
* getFeatures will return features from given map input. This function abstracts the logic to parse given input and returns
7877
* list of Features if exists in Map format.
7978
* @param geoJSON given input which may contain GeoJSON Object
80-
* @return Returns an Optional with the List of Features as map if input is GeoJSON,
81-
* else, returns an empty Optional instance.
79+
* @return Returns List of Features as map if input is GeoJSON,
80+
* else, returns an empty list.
8281
*/
83-
public static Optional<List<Map<String, Object>>> getFeatures(final Map<String, Object> geoJSON) {
82+
public static List<Map<String, Object>> getFeatures(final Map<String, Object> geoJSON) {
8483
final String type = extractValueAsString(geoJSON, TYPE_KEY);
8584
Objects.requireNonNull(type, TYPE_KEY + " cannot be null");
8685
if (Feature.TYPE.equalsIgnoreCase(type)) {
87-
return Optional.of(List.of(geoJSON));
86+
return List.of(geoJSON);
8887
}
8988
if (FeatureCollection.TYPE.equalsIgnoreCase(type)) {
90-
return Optional.of(Collections.unmodifiableList(FeatureCollection.create(geoJSON).getFeatures()));
89+
return Collections.unmodifiableList(FeatureCollection.create(geoJSON).getFeatures());
9190
}
92-
return Optional.empty();
91+
return List.of();
9392
}
9493
}

src/main/java/org/opensearch/geospatial/action/upload/geojson/ContentBuilder.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ private Optional<BulkRequestBuilder> prepareContentRequest(UploadGeoJSONRequestC
4545
.stream()
4646
.map(GeospatialParser::toStringObjectMap)
4747
.map(GeospatialParser::getFeatures)
48-
.filter(Optional::isPresent)
49-
.map(Optional::get)
5048
.flatMap(List::stream)
5149
.map(this::createIndexRequestBuilder)
5250
.map(indexRequestBuilder -> indexRequestBuilder.setIndex(content.getIndexName()))

src/test/java/org/opensearch/geospatial/GeospatialParserTests.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.HashMap;
1010
import java.util.List;
1111
import java.util.Map;
12-
import java.util.Optional;
1312

1413
import org.json.JSONArray;
1514
import org.json.JSONObject;
@@ -77,10 +76,10 @@ public void testConvertToMap() {
7776

7877
public void testGetFeaturesWithGeoJSONFeature() {
7978
Map<String, Object> geoJSON = GeospatialObjectBuilder.randomGeoJSONFeature(new JSONObject()).toMap();
80-
Optional<List<Map<String, Object>>> features = GeospatialParser.getFeatures(geoJSON);
81-
assertTrue(features.isPresent());
82-
assertEquals(1, features.get().size());
83-
assertEquals(features.get().get(0), geoJSON);
79+
List<Map<String, Object>> features = GeospatialParser.getFeatures(geoJSON);
80+
assertFalse(features.isEmpty());
81+
assertEquals(1, features.size());
82+
assertEquals(features.get(0), geoJSON);
8483
}
8584

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

9291
JSONObject collection = GeospatialObjectBuilder.buildGeoJSONFeatureCollection(features);
93-
Optional<List<Map<String, Object>>> featureList = GeospatialParser.getFeatures(collection.toMap());
94-
assertTrue(featureList.isPresent());
95-
assertEquals(featureList.get().size(), features.length());
92+
List<Map<String, Object>> featureList = GeospatialParser.getFeatures(collection.toMap());
93+
assertFalse(featureList.isEmpty());
94+
assertEquals(featureList.size(), features.length());
9695
}
9796

9897
public void testGetFeaturesWithUnSupportedType() {
9998
Map<String, Object> geoJSON = new HashMap<>();
10099
geoJSON.put(Feature.TYPE_KEY, "invalid-type");
101-
Optional<List<Map<String, Object>>> features = GeospatialParser.getFeatures(geoJSON);
102-
assertFalse(features.isPresent());
100+
List<Map<String, Object>> features = GeospatialParser.getFeatures(geoJSON);
101+
assertTrue(features.isEmpty());
103102
}
104103

105104
}

0 commit comments

Comments
 (0)