-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1826 - CombinedComparator is created. Added builders for comparators. (
- Loading branch information
1 parent
fe00419
commit 6761533
Showing
8 changed files
with
359 additions
and
25 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
48 changes: 48 additions & 0 deletions
48
bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/CombinedComparator.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,48 @@ | ||
package com.epam.indigo.sort; | ||
|
||
import com.epam.indigo.model.IndigoRecord; | ||
import org.elasticsearch.search.sort.SortBuilder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.stream.Collectors; | ||
|
||
public class CombinedComparator<T extends IndigoRecord> extends IndigoComparator<T> { | ||
|
||
protected Collection<IndigoComparator<T>> comparators; | ||
|
||
@Override | ||
public Collection<SortBuilder<?>> toSortBuilders() { | ||
return comparators.stream() | ||
.map(IndigoComparator::toSortBuilders) | ||
.flatMap(Collection::stream) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public static <T extends IndigoRecord> Builder<T> builder() { | ||
return new Builder<>(); | ||
} | ||
|
||
public static class Builder<T extends IndigoRecord> { | ||
private Collection<IndigoComparator<T>> comparators; | ||
|
||
public Builder<T> withComparators(Collection<IndigoComparator<T>> comparators) { | ||
this.comparators = comparators; | ||
return this; | ||
} | ||
|
||
public Builder<T> withComparator(IndigoComparator<T> comparator) { | ||
if (this.comparators == null) | ||
this.comparators = new ArrayList<>(); | ||
this.comparators.add(comparator); | ||
return this; | ||
} | ||
|
||
public CombinedComparator<T> build() { | ||
CombinedComparator<T> combinedComparator = new CombinedComparator<>(); | ||
combinedComparator.comparators = comparators; | ||
return combinedComparator; | ||
} | ||
} | ||
|
||
} |
48 changes: 40 additions & 8 deletions
48
bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/FieldComparator.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 |
---|---|---|
@@ -1,27 +1,59 @@ | ||
package com.epam.indigo.sort; | ||
|
||
import com.epam.indigo.model.IndigoRecord; | ||
import org.elasticsearch.search.sort.FieldSortBuilder; | ||
import org.elasticsearch.search.sort.SortBuilder; | ||
import org.elasticsearch.search.sort.SortBuilders; | ||
import org.elasticsearch.search.sort.SortOrder; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
public class FieldComparator<T extends IndigoRecord> extends IndigoComparator<T> { | ||
|
||
protected String fieldName; | ||
protected SortOrder sortOrder; | ||
|
||
public FieldComparator(final String fieldName, final SortOrder sortOrder) { | ||
super(sortOrder); | ||
this.fieldName = fieldName; | ||
this.sortOrder = sortOrder; | ||
} | ||
|
||
@Override | ||
public int compare(final T o1, final T o2) { | ||
// does not expect to be called | ||
return 0; | ||
public String getFieldName() { | ||
return fieldName; | ||
} | ||
|
||
public SortOrder getSortOrder() { | ||
return sortOrder; | ||
} | ||
|
||
@Override | ||
public SortBuilder<FieldSortBuilder> toSortBuilder() { | ||
return new FieldSortBuilder(this.fieldName).order(this.sortOrder); | ||
public Collection<SortBuilder<?>> toSortBuilders() { | ||
return Collections.singletonList( | ||
SortBuilders.fieldSort(fieldName) | ||
.order(sortOrder) | ||
); | ||
} | ||
|
||
public static <T extends IndigoRecord> Builder<T> builder() { | ||
return new Builder<>(); | ||
} | ||
|
||
public static class Builder<T extends IndigoRecord> { | ||
private String fieldName; | ||
private SortOrder sortOrder; | ||
|
||
public Builder<T> withFieldName(String fieldName) { | ||
this.fieldName = fieldName; | ||
return this; | ||
} | ||
|
||
public Builder<T> withSortOrder(SortOrder sortOrder) { | ||
this.sortOrder = sortOrder; | ||
return this; | ||
} | ||
|
||
public FieldComparator<T> build() { | ||
return new FieldComparator<>(fieldName, sortOrder); | ||
} | ||
} | ||
} |
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
39 changes: 30 additions & 9 deletions
39
bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/ScoreComparator.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 |
---|---|---|
@@ -1,28 +1,49 @@ | ||
package com.epam.indigo.sort; | ||
|
||
import com.epam.indigo.model.IndigoRecord; | ||
import org.elasticsearch.search.sort.ScoreSortBuilder; | ||
import org.elasticsearch.search.sort.SortBuilder; | ||
import org.elasticsearch.search.sort.SortBuilders; | ||
import org.elasticsearch.search.sort.SortOrder; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
public class ScoreComparator<T extends IndigoRecord> extends IndigoComparator<T> { | ||
|
||
protected SortOrder sortOrder; | ||
|
||
public ScoreComparator() { | ||
super(SortOrder.DESC); | ||
this(SortOrder.DESC); | ||
} | ||
|
||
public ScoreComparator(SortOrder sortOrder) { | ||
super(sortOrder); | ||
this.sortOrder = sortOrder; | ||
} | ||
|
||
@Override | ||
public SortBuilder<ScoreSortBuilder> toSortBuilder() { | ||
return new ScoreSortBuilder().order(sortOrder); | ||
public SortOrder getSortOrder() { | ||
return sortOrder; | ||
} | ||
|
||
@Override | ||
public int compare(final T o1, final T o2) { | ||
// does not expect to be called | ||
return 0; | ||
public Collection<SortBuilder<?>> toSortBuilders() { | ||
return Collections.singletonList(SortBuilders.scoreSort().order(sortOrder)); | ||
} | ||
|
||
public static <T extends IndigoRecord> Builder<T> builder() { | ||
return new Builder<>(); | ||
} | ||
|
||
public static class Builder<T extends IndigoRecord> { | ||
private SortOrder sortOrder; | ||
|
||
public Builder<T> withSortOrder(SortOrder sortOrder) { | ||
this.sortOrder = sortOrder; | ||
return this; | ||
} | ||
|
||
public ScoreComparator<T> build() { | ||
return new ScoreComparator<>(sortOrder); | ||
} | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
bingo/bingo-elastic/java/src/test/java/com/epam/indigo/sort/CombinedComparatorTest.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,57 @@ | ||
package com.epam.indigo.sort; | ||
|
||
import com.epam.indigo.model.IndigoRecord; | ||
import org.elasticsearch.search.sort.FieldSortBuilder; | ||
import org.elasticsearch.search.sort.ScoreSortBuilder; | ||
import org.elasticsearch.search.sort.SortBuilder; | ||
import org.elasticsearch.search.sort.SortOrder; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
public class CombinedComparatorTest { | ||
|
||
@Test | ||
public void testToSortBuilders() { | ||
FieldComparator<IndigoRecord> fieldComparator = new FieldComparator<>("testField", SortOrder.ASC); | ||
ScoreComparator<IndigoRecord> scoreComparator = new ScoreComparator<>(SortOrder.DESC); | ||
|
||
CombinedComparator<IndigoRecord> combinedComparator = CombinedComparator.builder() | ||
.withComparator(fieldComparator) | ||
.withComparator(scoreComparator) | ||
.build(); | ||
|
||
Collection<SortBuilder<?>> sortBuilders = combinedComparator.toSortBuilders(); | ||
assertNotNull(sortBuilders); | ||
assertEquals(2, sortBuilders.size()); | ||
|
||
List<SortBuilder<?>> sortBuilderList = (List<SortBuilder<?>>) sortBuilders; | ||
FieldSortBuilder fieldSortBuilder = (FieldSortBuilder) sortBuilderList.get(0); | ||
assertEquals("testField", fieldSortBuilder.getFieldName()); | ||
assertEquals(SortOrder.ASC, fieldSortBuilder.order()); | ||
|
||
assertInstanceOf(ScoreSortBuilder.class, sortBuilderList.get(1)); | ||
} | ||
|
||
@Test | ||
public void testBuilderWithComparators() { | ||
FieldComparator<IndigoRecord> fieldComparator = new FieldComparator<>("testField1", SortOrder.ASC); | ||
ScoreComparator<IndigoRecord> scoreComparator = new ScoreComparator<>(SortOrder.DESC); | ||
|
||
List<IndigoComparator<IndigoRecord>> comparators = Arrays.asList(fieldComparator, scoreComparator); | ||
CombinedComparator<IndigoRecord> combinedComparator = CombinedComparator.builder() | ||
.withComparators(comparators) | ||
.build(); | ||
|
||
Collection<SortBuilder<?>> sortBuilders = combinedComparator.toSortBuilders(); | ||
assertNotNull(sortBuilders); | ||
assertEquals(2, sortBuilders.size()); | ||
} | ||
} | ||
|
92 changes: 92 additions & 0 deletions
92
bingo/bingo-elastic/java/src/test/java/com/epam/indigo/sort/FieldComparatorTest.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,92 @@ | ||
package com.epam.indigo.sort; | ||
|
||
import com.epam.indigo.model.IndigoRecord; | ||
import org.elasticsearch.search.sort.FieldSortBuilder; | ||
import org.elasticsearch.search.sort.SortBuilder; | ||
import org.elasticsearch.search.sort.SortOrder; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Collection; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
|
||
public class FieldComparatorTest { | ||
|
||
@Test | ||
public void testConstructor() { | ||
String fieldName = "testField"; | ||
SortOrder sortOrder = SortOrder.ASC; | ||
FieldComparator<IndigoRecord> comparator = new FieldComparator<>(fieldName, sortOrder); | ||
|
||
assertEquals(fieldName, comparator.getFieldName()); | ||
assertEquals(sortOrder, comparator.getSortOrder()); | ||
} | ||
|
||
@Test | ||
public void testToSortBuilders() { | ||
String fieldName = "testField"; | ||
SortOrder sortOrder = SortOrder.ASC; | ||
FieldComparator<IndigoRecord> comparator = new FieldComparator<>(fieldName, sortOrder); | ||
|
||
Collection<SortBuilder<?>> sortBuilders = comparator.toSortBuilders(); | ||
assertNotNull(sortBuilders); | ||
assertEquals(1, sortBuilders.size()); | ||
|
||
SortBuilder<?> sortBuilder = sortBuilders.iterator().next(); | ||
assertNotNull(sortBuilder); | ||
assertInstanceOf(FieldSortBuilder.class, sortBuilder); | ||
|
||
FieldSortBuilder fieldSortBuilder = (FieldSortBuilder) sortBuilder; | ||
assertEquals(fieldName, fieldSortBuilder.getFieldName()); | ||
assertEquals(sortOrder, fieldSortBuilder.order()); | ||
} | ||
|
||
@Test | ||
public void testBuilderWithFieldName() { | ||
String fieldName = "testField"; | ||
FieldComparator.Builder<IndigoRecord> builder = FieldComparator.builder(); | ||
builder.withFieldName(fieldName); | ||
FieldComparator<IndigoRecord> comparator = builder.build(); | ||
|
||
assertEquals(fieldName, comparator.getFieldName()); | ||
} | ||
|
||
@Test | ||
public void testBuilderWithSortOrder() { | ||
SortOrder sortOrder = SortOrder.ASC; | ||
FieldComparator.Builder<IndigoRecord> builder = FieldComparator.builder(); | ||
builder.withSortOrder(sortOrder); | ||
FieldComparator<IndigoRecord> comparator = builder.build(); | ||
|
||
assertEquals(sortOrder, comparator.getSortOrder()); | ||
} | ||
|
||
@Test | ||
public void testBuilderCompleteFlow() { | ||
String fieldName = "testField"; | ||
SortOrder sortOrder = SortOrder.DESC; | ||
FieldComparator<IndigoRecord> comparator = FieldComparator.builder() | ||
.withFieldName(fieldName) | ||
.withSortOrder(sortOrder) | ||
.build(); | ||
|
||
assertNotNull(comparator); | ||
assertEquals(fieldName, comparator.getFieldName()); | ||
assertEquals(sortOrder, comparator.getSortOrder()); | ||
|
||
Collection<SortBuilder<?>> sortBuilders = comparator.toSortBuilders(); | ||
assertNotNull(sortBuilders); | ||
assertEquals(1, sortBuilders.size()); | ||
|
||
SortBuilder<?> sortBuilder = sortBuilders.iterator().next(); | ||
assertNotNull(sortBuilder); | ||
assertInstanceOf(FieldSortBuilder.class, sortBuilder); | ||
|
||
FieldSortBuilder fieldSortBuilder = (FieldSortBuilder) sortBuilder; | ||
assertEquals(fieldName, fieldSortBuilder.getFieldName()); | ||
assertEquals(sortOrder, fieldSortBuilder.order()); | ||
} | ||
} |
Oops, something went wrong.