Skip to content

Commit 5ae21aa

Browse files
committed
Use collection interfaces instead of concrete classes
Update client classes to use base collection interfaces in method signatures. This allows for more flexibility for end users than the concrete implementations `HashMap` and `ArrayList`. Change is backwards compatible with previous version client users
1 parent f17d863 commit 5ae21aa

File tree

14 files changed

+65
-66
lines changed

14 files changed

+65
-66
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import org.typesense.resources.*;
2424

2525
### Create a new client
2626
```java
27-
ArrayList<Node> nodes = new ArrayList<>();
27+
List<Node> nodes = new ArrayList<>();
2828
nodes.add(
2929
new Node(
3030
"http", // For Typesense Cloud use https
@@ -40,7 +40,7 @@ Client client = new Client(configuration);
4040

4141
### Create a new collection
4242
```java
43-
ArrayList<Field> fields = new ArrayList<>();
43+
List<Field> fields = new ArrayList<>();
4444
fields.add(new Field().name("countryName").type(FieldTypes.STRING));
4545
fields.add(new Field().name("capital").type(FieldTypes.STRING));
4646
fields.add(new Field().name("gdp").type(FieldTypes.INT32).facet(true).sort(true));
@@ -53,7 +53,7 @@ client.collections().create(collectionSchema);
5353

5454
### Index a document
5555
```java
56-
HashMap<String, Object> hmap = new HashMap<>();
56+
Map<String, Object> hmap = new HashMap<>();
5757
hmap.put("countryName","India");
5858
hmap.put("capital","Delhi");
5959
hmap.put("gdp", 10);
@@ -63,7 +63,7 @@ client.collections("contryName").documents().create(hmap);
6363

6464
### Upserting a document
6565
```java
66-
HashMap<String, Object> hmap = new HashMap<>();
66+
Map<String, Object> hmap = new HashMap<>();
6767
hmap.put("countryName","India");
6868
hmap.put("capital","Delhi");
6969
hmap.put("gdp", 5);
@@ -93,7 +93,7 @@ SearchResult searchResult = client.collections("countries").documents().search(s
9393

9494
### Update a document
9595
```java
96-
HashMap<String, Object> hmap = new HashMap<>();
96+
Map<String, Object> hmap = new HashMap<>();
9797
hmap.put("gdp", 8);
9898
client.collections("countries").documents("28").update(hmap);
9999
```
@@ -268,7 +268,7 @@ client.collections("countries").synonyms("continent-synonyms").delete();
268268

269269
### Create snapshot (for backups)
270270
```java
271-
HashMap<String, String> query = new HashMap<>();
271+
Map<String, String> query = new HashMap<>();
272272
query.put("snapshot_path","/tmp/typesense-data-snapshot");
273273

274274
client.operations.perform("snapshot",query);

src/main/java/org/typesense/api/Client.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package org.typesense.api;
22

33
import java.util.HashMap;
4+
import java.util.Map;
45

56
public class Client {
67
private Configuration configuration;
78

89
private ApiCall apiCall;
910

1011
private Collections collections;
11-
private HashMap<String, Collection> individualCollections;
12+
private Map<String, Collection> individualCollections;
1213

1314
private Aliases aliases;
14-
private HashMap<String, Alias> individualAliases;
15+
private Map<String, Alias> individualAliases;
1516

1617
private Keys keys;
17-
private HashMap<Long, Key> individualKeys;
18+
private Map<Long, Key> individualKeys;
1819

1920
public Health health;
2021
public Operations operations;
@@ -30,7 +31,7 @@ public Client(Configuration configuration){
3031
this.aliases = new Aliases(this.apiCall);
3132
this.individualAliases = new HashMap<>();
3233
this.keys = new Keys(this.apiCall);
33-
this.individualKeys = new HashMap<Long, Key>();
34+
this.individualKeys = new HashMap<>();
3435
this.health = new Health(this.apiCall);
3536
this.operations = new Operations(this.apiCall);
3637
this.metrics = new Metrics(this.apiCall);

src/main/java/org/typesense/api/Collection.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.typesense.model.CollectionUpdateSchema;
55

66
import java.util.HashMap;
7+
import java.util.Map;
78

89
public class Collection {
910

@@ -14,13 +15,13 @@ public class Collection {
1415
private final String name;
1516

1617
private Documents documents;
17-
private HashMap<String, Document> individualDocuments;
18+
private Map<String, Document> individualDocuments;
1819

1920
private Synonyms synonyms;
20-
private HashMap<String, Synonym> individualSynonyms;
21+
private Map<String, Synonym> individualSynonyms;
2122

2223
private Overrides overrides;
23-
private HashMap<String, Override> individualOverrides;
24+
private Map<String, Override> individualOverrides;
2425

2526
private final String endpoint;
2627

@@ -30,11 +31,11 @@ public class Collection {
3031
this.configuration = configuration;
3132
this.endpoint = Collections.RESOURCE_PATH + "/" + this.name;
3233
this.documents = new Documents(this.name, this.apiCall, this.configuration);
33-
this.individualDocuments = new HashMap<String, Document>();
34+
this.individualDocuments = new HashMap<>();
3435
this.synonyms = new Synonyms(this.name, this.apiCall);
35-
this.individualSynonyms = new HashMap<String, Synonym>();
36+
this.individualSynonyms = new HashMap<>();
3637
this.overrides = new Overrides(this.name, this.apiCall);
37-
this.individualOverrides = new HashMap<String, Override>();
38+
this.individualOverrides = new HashMap<>();
3839
}
3940

4041
public CollectionResponse retrieve() throws Exception {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.typesense.api;
22

3-
import java.util.HashMap;
3+
import java.util.Map;
44

55
public class Debug {
66

@@ -11,7 +11,7 @@ public Debug(ApiCall apiCall) {
1111
this.apiCall = apiCall;
1212
}
1313

14-
public HashMap<String, String> retrieve() throws Exception {
15-
return this.apiCall.get(RESOURCEPATH, null, HashMap.class);
14+
public Map<String, String> retrieve() throws Exception {
15+
return this.apiCall.get(RESOURCEPATH, null, Map.class);
1616
}
1717
}

src/main/java/org/typesense/api/Document.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.typesense.api;
22

3-
import java.util.HashMap;
3+
import java.util.Map;
44

55
public class Document {
66
private String collectionName;
@@ -16,16 +16,16 @@ public class Document {
1616
this.endpoint = Collections.RESOURCE_PATH + "/" + this.collectionName + Documents.RESOURCE_PATH + "/" + this.documentId;
1717
}
1818

19-
public HashMap<String,Object> retrieve() throws Exception {
20-
return this.apiCall.get(endpoint, null, HashMap.class);
19+
public Map<String,Object> retrieve() throws Exception {
20+
return this.apiCall.get(endpoint, null, Map.class);
2121
}
2222

23-
public HashMap<String, Object> delete() throws Exception {
24-
return this.apiCall.delete(this.endpoint, null, HashMap.class);
23+
public Map<String, Object> delete() throws Exception {
24+
return this.apiCall.delete(this.endpoint, null, Map.class);
2525
}
2626

27-
public HashMap<String , Object> update(HashMap<String, Object> document) throws Exception {
28-
return this.apiCall.patch(this.endpoint, document, null, HashMap.class);
27+
public Map<String , Object> update(Map<String, Object> document) throws Exception {
28+
return this.apiCall.patch(this.endpoint, document, null, Map.class);
2929
}
3030

3131
}

src/main/java/org/typesense/api/Documents.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import org.typesense.model.*;
55

6-
import java.util.ArrayList;
7-
import java.util.HashMap;
6+
import java.util.Map;
87

98
public class Documents {
109

@@ -20,31 +19,31 @@ public class Documents {
2019
this.configuration = configuration;
2120
}
2221

23-
public HashMap<String, Object> create(HashMap<String, Object> document) throws Exception {
24-
return this.apiCall.post(getEndPoint("/"), document, null, HashMap.class);
22+
public Map<String, Object> create(Map<String, Object> document) throws Exception {
23+
return this.apiCall.post(getEndPoint("/"), document, null, Map.class);
2524
}
2625

2726
public String create(String document) throws Exception {
2827
return this.apiCall.post(getEndPoint("/"),document,null,String.class);
2928
}
3029

31-
public String create(HashMap<String, Object> document, ImportDocumentsParameters queryParameters) throws Exception {
30+
public String create(Map<String, Object> document, ImportDocumentsParameters queryParameters) throws Exception {
3231
return this.apiCall.post(getEndPoint("/"),document,queryParameters,String.class);
3332
}
3433

35-
public HashMap<String, Object> upsert(HashMap<String, Object> document) throws Exception {
34+
public Map<String, Object> upsert(Map<String, Object> document) throws Exception {
3635
ImportDocumentsParameters queryParameters = new ImportDocumentsParameters();
3736
queryParameters.action("upsert");
3837

39-
return this.apiCall.post(getEndPoint("/"),document,queryParameters,HashMap.class);
38+
return this.apiCall.post(getEndPoint("/"),document,queryParameters,Map.class);
4039
}
4140

4241
public SearchResult search(SearchParameters searchParameters) throws Exception {
4342
return this.apiCall.get(getEndPoint("search"), searchParameters,org.typesense.model.SearchResult.class);
4443
}
4544

46-
public HashMap<String, Object> delete(DeleteDocumentsParameters queryParameters) throws Exception {
47-
return this.apiCall.delete(getEndPoint("/"), queryParameters, HashMap.class);
45+
public Map<String, Object> delete(DeleteDocumentsParameters queryParameters) throws Exception {
46+
return this.apiCall.delete(getEndPoint("/"), queryParameters, Map.class);
4847
}
4948

5049
public String export() throws Exception {
@@ -59,11 +58,10 @@ public String import_(String document, ImportDocumentsParameters queryParameters
5958
return this.apiCall.post(this.getEndPoint("import"),document, queryParameters,String.class);
6059
}
6160

62-
public String import_(ArrayList<HashMap<String, Object>> documents, ImportDocumentsParameters queryParameters) throws Exception {
61+
public String import_(java.util.Collection<Map<String, Object>> documents, ImportDocumentsParameters queryParameters) throws Exception {
6362
ObjectMapper mapper = new ObjectMapper();
6463
String json="";
65-
for(int i=0;i<documents.size();i++){
66-
HashMap<String, Object> document = documents.get(i);
64+
for(Map<String, Object> document : documents){
6765
try {
6866
//Convert Map to JSON
6967
json = json.concat(mapper.writeValueAsString(document) + "\n");
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.typesense.api;
22

3-
import java.util.HashMap;
3+
import java.util.Map;
44

55
public class Health {
66

@@ -11,7 +11,7 @@ public Health(ApiCall apiCall) {
1111
this.apiCall = apiCall;
1212
}
1313

14-
public HashMap<String, Object> retrieve() throws Exception {
15-
return this.apiCall.get(RESOURCEPATH, null, HashMap.class);
14+
public Map<String, Object> retrieve() throws Exception {
15+
return this.apiCall.get(RESOURCEPATH, null, Map.class);
1616
}
1717
}

src/main/java/org/typesense/api/Keys.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import javax.crypto.spec.SecretKeySpec;
1111
import java.nio.charset.StandardCharsets;
1212
import java.util.Base64;
13-
import java.util.HashMap;
13+
import java.util.Map;
1414

1515
public class Keys {
1616

@@ -32,7 +32,7 @@ public ApiKeysResponse retrieve() throws Exception {
3232
return this.apiCall.get(Keys.RESOURCEPATH, null, ApiKeysResponse.class);
3333
}
3434

35-
public String generateScopedSearchKey(String searchKey, HashMap<String, Object> parameters){
35+
public String generateScopedSearchKey(String searchKey, Map<String, Object> parameters){
3636
ObjectMapper mapper = new ObjectMapper();
3737
String params = "";
3838
try {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.typesense.api;
22

3-
import java.util.HashMap;
3+
import java.util.Map;
44

55
public class Metrics {
66

@@ -11,7 +11,7 @@ public Metrics(ApiCall apiCall) {
1111
this.apiCall = apiCall;
1212
}
1313

14-
public HashMap<String, String> retrieve() throws Exception {
15-
return this.apiCall.get(RESOURCEPATH, null, HashMap.class);
14+
public Map<String, String> retrieve() throws Exception {
15+
return this.apiCall.get(RESOURCEPATH, null, Map.class);
1616
}
1717
}

src/main/java/org/typesense/api/MultiSearch.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.typesense.model.MultiSearchResponse;
44

5-
import java.util.HashMap;
5+
import java.util.Map;
66
import java.util.List;
77

88
public class MultiSearch {
@@ -14,7 +14,7 @@ public MultiSearch(ApiCall apiCall) {
1414
this.apiCall = apiCall;
1515
}
1616

17-
public MultiSearchResponse perform(HashMap<String , List<HashMap<String,String>>> multiSearchParameters, HashMap<String, String> common_params) throws Exception {
17+
public MultiSearchResponse perform(Map<String , List<Map<String,String>>> multiSearchParameters, Map<String, String> common_params) throws Exception {
1818
return this.apiCall.post(MultiSearch.RESOURCEPATH, multiSearchParameters, common_params, MultiSearchResponse.class);
1919
}
2020
}

0 commit comments

Comments
 (0)