Skip to content

Commit

Permalink
Document class base type (Azure#306)
Browse files Browse the repository at this point in the history
* Document class extends HashMap instead of LinkedHashMap

* updates

* remove LinkedHashMap from async tests
  • Loading branch information
navalev authored and CodeRunRepeat committed Nov 27, 2019
1 parent 070b0f2 commit f772853
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Licensed under the MIT License.
package com.azure.search;

import java.util.LinkedHashMap;
import java.util.HashMap;

/**
* Represents a document
* A property bag is used for scenarios where the index schema is only known at run-time
* If the schema is known, user can convert the properties to a specific object type
*/
public class Document extends LinkedHashMap<String, Object> {
public class Document extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -122,12 +121,12 @@ public void canSearchWithSelectedFields() throws IOException {
HashMap<String, Object> expectedHotel2 = new HashMap<>();
expectedHotel2.put("HotelName", "Secret Point Motel");
expectedHotel2.put("Rating", 4);
HashMap<String, Object> address = new LinkedHashMap<>();
HashMap<String, Object> address = new HashMap<>();
address.put("City", "New York");
expectedHotel2.put("Address", address);
HashMap<String, Object> rooms = new LinkedHashMap<>();
HashMap<String, Object> rooms = new HashMap<>();
rooms.put("Type", "Budget Room");
HashMap<String, Object> rooms2 = new LinkedHashMap<>();
HashMap<String, Object> rooms2 = new HashMap<>();
rooms2.put("Type", "Budget Room");
expectedHotel2.put("Rooms", Arrays.asList(rooms, rooms2));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -271,12 +270,12 @@ public void canSearchWithSelectedFields() throws IOException {
HashMap<String, Object> expectedHotel2 = new HashMap<>();
expectedHotel2.put("HotelName", "Secret Point Motel");
expectedHotel2.put("Rating", 4);
HashMap<String, Object> address = new LinkedHashMap<>();
HashMap<String, Object> address = new HashMap<>();
address.put("City", "New York");
expectedHotel2.put("Address", address);
HashMap<String, Object> rooms = new LinkedHashMap<>();
HashMap<String, Object> rooms = new HashMap<>();
rooms.put("Type", "Budget Room");
HashMap<String, Object> rooms2 = new LinkedHashMap<>();
HashMap<String, Object> rooms2 = new HashMap<>();
rooms2.put("Type", "Budget Room");
expectedHotel2.put("Rooms", Arrays.asList(rooms, rooms2));

Expand Down Expand Up @@ -700,33 +699,34 @@ private List<Map<String, Object>> getSearchResults(PagedIterableBase<SearchResul
}

private Map<String, Object> extractAndTransformSingleResult(SearchResult result) {
return dropUnnecessaryFields(convertLinkedHashMapToMap(
return dropUnnecessaryFields(convertHashMapToMap(
(result.getDocument())));
}

/**
* Convert a Linked HashMap object to Map object
* Convert a HashMap object to Map object
*
* @param linkedMapObject object to convert
* @param mapObject object to convert
* @return {@link Map}{@code <}{@link String}{@code ,}{@link Object}{@code >}
*/
private static Map<String, Object> convertLinkedHashMapToMap(Object linkedMapObject) {
private static Map<String, Object> convertHashMapToMap(Object mapObject) {
/** This SuppressWarnings is for the checkstyle
it is used because api return type can be anything and therefore is an Object
in our case we know and we use it only when the return type is LinkedHashMap
in our case we know and we use it only when the return type is LinkedHashMap.
The object is converted into HashMap (which LinkedHashMap extends)
**/
@SuppressWarnings(value = "unchecked")
LinkedHashMap<String, Object> linkedMap = (LinkedHashMap<String, Object>) linkedMapObject;
HashMap<String, Object> map = (HashMap<String, Object>) mapObject;

Set<Map.Entry<String, Object>> entries = linkedMap.entrySet();
Set<Map.Entry<String, Object>> entries = map.entrySet();

Map<String, Object> convertedMap = new HashMap<>();

for (Map.Entry<String, Object> entry : entries) {
Object value = entry.getValue();

if (value instanceof LinkedHashMap) {
value = convertLinkedHashMapToMap(entry.getValue());
if (value instanceof HashMap) {
value = convertHashMapToMap(entry.getValue());
}
if (value instanceof ArrayList) {
value = convertArray((ArrayList) value);
Expand All @@ -748,8 +748,8 @@ private static Map<String, Object> convertLinkedHashMapToMap(Object linkedMapObj
private static ArrayList<Object> convertArray(ArrayList<Object> array) {
ArrayList<Object> convertedArray = new ArrayList<>();
for (Object arrayValue : array) {
if (arrayValue instanceof LinkedHashMap) {
convertedArray.add(convertLinkedHashMapToMap(arrayValue));
if (arrayValue instanceof HashMap) {
convertedArray.add(convertHashMapToMap(arrayValue));
} else {
convertedArray.add(arrayValue);
}
Expand Down

0 comments on commit f772853

Please sign in to comment.