-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
177 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,5 @@ hs_err_pid* | |
*.iml | ||
.DS_Store | ||
*.logQueryTest | ||
|
||
.factorypath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
algoliasearch-common/src/main/java/com/algolia/search/objects/FacetFilters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.algolia.search.objects; | ||
|
||
import com.algolia.search.Defaults; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
@JsonDeserialize(using = FacetFiltersJsonDeserializer.class) | ||
@JsonSerialize(using = FacetFiltersJsonSerializer.class) | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public abstract class FacetFilters implements Serializable { | ||
|
||
public static FacetFilters ofList(List<String> filters) { | ||
return new FacetFiltersAsListOfString(filters); | ||
} | ||
|
||
public static FacetFilters ofListOfList(List<List<String>> filters) { | ||
return new FacetFiltersAsListOfList(filters); | ||
} | ||
|
||
@JsonIgnore | ||
public abstract Object getInsideValue(); | ||
} | ||
|
||
class FacetFiltersJsonDeserializer extends JsonDeserializer { | ||
|
||
@Override | ||
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
List list = p.readValueAs(List.class); | ||
if (list.isEmpty()) { | ||
return new FacetFiltersAsListOfString(); | ||
} | ||
if (list.get(0) instanceof String) { | ||
return new FacetFiltersAsListOfString(list); | ||
} | ||
return new FacetFiltersAsListOfList(list); | ||
} | ||
} | ||
|
||
class FacetFiltersJsonSerializer extends JsonSerializer<FacetFilters> { | ||
|
||
@Override | ||
public void serialize(FacetFilters value, JsonGenerator gen, SerializerProvider serializers) | ||
throws IOException { | ||
|
||
ObjectMapper objectMapper = Defaults.DEFAULT_OBJECT_MAPPER; | ||
|
||
if (value instanceof FacetFiltersAsListOfString) { | ||
FacetFiltersAsListOfString filters = (FacetFiltersAsListOfString) value; | ||
objectMapper.writeValue(gen, filters.getFilters()); | ||
|
||
} else if (value instanceof FacetFiltersAsListOfList) { | ||
FacetFiltersAsListOfList filters = (FacetFiltersAsListOfList) value; | ||
objectMapper.writeValue(gen, filters.getFilters()); | ||
|
||
} else { | ||
throw new IOException( | ||
"cannot serialize facetFilters: unknown input type '" + value.getClass().getName() + "'"); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
algoliasearch-common/src/main/java/com/algolia/search/objects/FacetFiltersAsListOfList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.algolia.search.objects; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import java.util.List; | ||
|
||
@JsonDeserialize(as = FacetFiltersAsListOfList.class) | ||
public class FacetFiltersAsListOfList extends FacetFilters { | ||
|
||
private List<List<String>> filters; | ||
|
||
FacetFiltersAsListOfList(List<List<String>> filters) { | ||
this.filters = filters; | ||
} | ||
|
||
@Override | ||
@JsonIgnore | ||
public Object getInsideValue() { | ||
return this.filters; | ||
} | ||
|
||
List<List<String>> getFilters() { | ||
return this.filters; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...liasearch-common/src/main/java/com/algolia/search/objects/FacetFiltersAsListOfString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.algolia.search.objects; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@JsonDeserialize(as = FacetFiltersAsListOfString.class) | ||
public class FacetFiltersAsListOfString extends FacetFilters { | ||
|
||
private List<String> filters; | ||
|
||
FacetFiltersAsListOfString() { | ||
this.filters = new ArrayList<String>(); | ||
} | ||
|
||
FacetFiltersAsListOfString(List<String> filters) { | ||
this.filters = filters; | ||
} | ||
|
||
@Override | ||
@JsonIgnore | ||
public Object getInsideValue() { | ||
return this.filters; | ||
} | ||
|
||
List<String> getFilters() { | ||
return this.filters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
algoliasearch-tests/src/test/java/com/algolia/search/objects/FacetFiltersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.algolia.search.objects; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.algolia.search.Defaults; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import org.junit.Test; | ||
|
||
public class FacetFiltersTest { | ||
|
||
private ObjectMapper objectMapper = Defaults.DEFAULT_OBJECT_MAPPER; | ||
|
||
private FacetFilters serializeDeserialize(FacetFilters facetFilters) throws IOException { | ||
String serialized = objectMapper.writeValueAsString(facetFilters); | ||
return objectMapper.readValue( | ||
serialized, objectMapper.getTypeFactory().constructType(FacetFilters.class)); | ||
} | ||
|
||
@Test | ||
public void facetFiltersAsListOfString() throws IOException { | ||
FacetFilters facetFilters = new FacetFiltersAsListOfString(Arrays.asList("filter1", "filter2")); | ||
FacetFilters result = serializeDeserialize(facetFilters); | ||
assertThat(result).isEqualToComparingFieldByField(facetFilters); | ||
} | ||
|
||
@Test | ||
public void facetFiltersAsListOfList() throws IOException { | ||
FacetFilters facetFilters = | ||
new FacetFiltersAsListOfList( | ||
Arrays.asList(Arrays.asList("filter1", "filter2"), Arrays.asList("filter3"))); | ||
FacetFilters result = serializeDeserialize(facetFilters); | ||
assertThat(result).isEqualToComparingFieldByField(facetFilters); | ||
} | ||
} |