Skip to content

Commit b00bd4b

Browse files
Polishing.
Reduce duplicate code by introducing common interface. Fix ANY flag computation. Original Pull Request: #2113
1 parent 1bf31c0 commit b00bd4b

File tree

4 files changed

+86
-99
lines changed

4 files changed

+86
-99
lines changed

src/main/asciidoc/appendix/appendix-command-reference.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@
4848
|EXPIREAT |X
4949
|FLUSHALL |X
5050
|FLUSHDB |X
51+
|GEOADD |X
52+
|GEODIST |X
53+
|GEOHASH |X
54+
|GEOPOS |X
55+
|GEORADIUS |X
56+
|GEORADIUSBYMEMBER |X
57+
|GEOSEARCH |X
58+
|GEOSEARCHSTORE |X
5159
|GET |X
5260
|GETBIT |X
5361
|GETRANGE |X

src/main/java/org/springframework/data/redis/connection/ReactiveGeoCommands.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ public GeoRadiusCommand withFlag(Flag flag) {
672672
Assert.notNull(flag, "Flag must not be null!");
673673

674674
GeoRadiusCommandArgs args = cloneArgs();
675-
args.flags.add(flag);
675+
args.getFlags().add(flag);
676676

677677
return new GeoRadiusCommand(getKey(), point, distance, args, store, storeDist);
678678
}
@@ -735,7 +735,7 @@ public GeoRadiusCommand sort(Direction direction) {
735735
Assert.notNull(direction, "Direction must not be null!");
736736

737737
GeoRadiusCommandArgs args = cloneArgs();
738-
args.sortDirection = direction;
738+
args.sort(direction);
739739

740740
return new GeoRadiusCommand(getKey(), point, distance, args, store, storeDist);
741741
}
@@ -1007,7 +1007,7 @@ public GeoRadiusByMemberCommand withFlag(Flag flag) {
10071007
Assert.notNull(flag, "Flag must not be null!");
10081008

10091009
GeoRadiusCommandArgs args = cloneArgs();
1010-
args.flags.add(flag);
1010+
args.getFlags().add(flag);
10111011

10121012
return new GeoRadiusByMemberCommand(getKey(), member, distance, args, store, storeDist);
10131013
}
@@ -1069,7 +1069,7 @@ public GeoRadiusByMemberCommand sort(Direction direction) {
10691069
Assert.notNull(direction, "Direction must not be null!");
10701070

10711071
GeoRadiusCommandArgs args = cloneArgs();
1072-
args.sortDirection = direction;
1072+
args.sort(direction);
10731073

10741074
return new GeoRadiusByMemberCommand(getKey(), member, distance, args, store, storeDist);
10751075
}

src/main/java/org/springframework/data/redis/connection/RedisGeoCommands.java

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ Long geoSearchStore(byte[] destKey, byte[] key, GeoReference<byte[]> reference,
255255
*
256256
* @since 2.6
257257
*/
258-
interface GeoShape {
258+
interface GeoShape extends Shape {
259259

260260
/**
261261
* Create a shape used as predicate for geo queries from a {@link Distance radius} around the query center point.
@@ -350,17 +350,44 @@ public Metric getMetric() {
350350
}
351351
}
352352

353+
interface GeoCommandArgs {
354+
355+
@Nullable
356+
public Direction getSortDirection();
357+
358+
@Nullable
359+
Long getLimit();
360+
361+
Set<? extends GeoCommandFlag> getFlags();
362+
363+
default boolean hasLimit() {
364+
return getLimit() != null;
365+
}
366+
367+
default boolean hasSortDirection() {
368+
return getSortDirection() != null;
369+
}
370+
371+
default boolean hasFlags() {
372+
return !getFlags().isEmpty();
373+
}
374+
375+
public interface GeoCommandFlag {}
376+
}
377+
353378
/**
354379
* Additional arguments (like count/sort/...) to be used with {@link RedisGeoCommands}.
355380
*
356381
* @author Mark Paluch
357382
* @since 2.6
358383
*/
359-
class GeoSearchCommandArgs implements Cloneable {
384+
class GeoSearchCommandArgs implements GeoCommandArgs, Cloneable {
385+
386+
protected final Set<Flag> flags = new LinkedHashSet<>(2, 1);
360387

361-
Set<Flag> flags = new LinkedHashSet<>(2, 1);
362-
@Nullable Long limit;
363-
@Nullable Direction sortDirection;
388+
@Nullable protected Long limit;
389+
390+
@Nullable protected Direction sortDirection;
364391

365392
private GeoSearchCommandArgs() {}
366393

@@ -433,10 +460,7 @@ public GeoSearchCommandArgs sortDescending() {
433460
* @return never {@literal null}.
434461
*/
435462
public GeoSearchCommandArgs limit(long count) {
436-
437-
Assert.isTrue(count > 0, "Count has to positive value.");
438-
limit = count;
439-
return this;
463+
return limit(count, false);
440464
}
441465

442466
/**
@@ -450,7 +474,9 @@ public GeoSearchCommandArgs limit(long count, boolean any) {
450474

451475
Assert.isTrue(count > 0, "Count has to positive value.");
452476
limit = count;
453-
flags.add(Flag.ANY);
477+
if (any) {
478+
flags.add(Flag.ANY);
479+
}
454480
return this;
455481
}
456482

@@ -477,30 +503,18 @@ public Direction getSortDirection() {
477503
return sortDirection;
478504
}
479505

480-
public boolean hasFlags() {
481-
return !flags.isEmpty();
482-
}
483-
484-
public boolean hasSortDirection() {
485-
return sortDirection != null;
486-
}
487-
488-
public boolean hasLimit() {
489-
return limit != null;
490-
}
491-
492506
public boolean hasAnyLimit() {
493507
return hasLimit() && flags.contains(Flag.ANY);
494508
}
495509

496510
@Override
497511
protected GeoSearchCommandArgs clone() {
498512

499-
GeoSearchCommandArgs tmp = new GeoSearchCommandArgs();
500-
tmp.flags = this.flags != null ? new LinkedHashSet<>(this.flags) : new LinkedHashSet<>(2);
501-
tmp.limit = this.limit;
502-
tmp.sortDirection = this.sortDirection;
503-
return tmp;
513+
GeoSearchCommandArgs that = new GeoSearchCommandArgs();
514+
that.flags.addAll(this.flags);
515+
that.limit = this.limit;
516+
that.sortDirection = this.sortDirection;
517+
return that;
504518
}
505519
}
506520

@@ -510,11 +524,13 @@ protected GeoSearchCommandArgs clone() {
510524
* @author Mark Paluch
511525
* @since 2.6
512526
*/
513-
class GeoSearchStoreCommandArgs implements Cloneable {
527+
class GeoSearchStoreCommandArgs implements GeoCommandArgs, Cloneable {
528+
529+
private final Set<Flag> flags = new LinkedHashSet<>(2, 1);
514530

515-
Set<Flag> flags = new LinkedHashSet<>(2, 1);
516-
@Nullable Long limit;
517-
@Nullable Direction sortDirection;
531+
@Nullable private Long limit;
532+
533+
@Nullable private Direction sortDirection;
518534

519535
private GeoSearchStoreCommandArgs() {}
520536

@@ -576,10 +592,7 @@ public GeoSearchStoreCommandArgs sortDescending() {
576592
* @return never {@literal null}.
577593
*/
578594
public GeoSearchStoreCommandArgs limit(long count) {
579-
580-
Assert.isTrue(count > 0, "Count has to positive value.");
581-
limit = count;
582-
return this;
595+
return limit(count, false);
583596
}
584597

585598
/**
@@ -592,8 +605,10 @@ public GeoSearchStoreCommandArgs limit(long count) {
592605
public GeoSearchStoreCommandArgs limit(long count, boolean any) {
593606

594607
Assert.isTrue(count > 0, "Count has to positive value.");
595-
limit = count;
596-
flags.add(Flag.ANY);
608+
this.limit = count;
609+
if (any) {
610+
flags.add(Flag.ANY);
611+
}
597612
return this;
598613
}
599614

@@ -624,26 +639,18 @@ public boolean isStoreDistance() {
624639
return flags.contains(Flag.STOREDIST);
625640
}
626641

627-
public boolean hasSortDirection() {
628-
return sortDirection != null;
629-
}
630-
631-
public boolean hasLimit() {
632-
return limit != null;
633-
}
634-
635642
public boolean hasAnyLimit() {
636643
return hasLimit() && flags.contains(Flag.ANY);
637644
}
638645

639646
@Override
640647
protected GeoSearchStoreCommandArgs clone() {
641648

642-
GeoSearchStoreCommandArgs tmp = new GeoSearchStoreCommandArgs();
643-
tmp.flags = this.flags != null ? new LinkedHashSet<>(this.flags) : new LinkedHashSet<>(2);
644-
tmp.limit = this.limit;
645-
tmp.sortDirection = this.sortDirection;
646-
return tmp;
649+
GeoSearchStoreCommandArgs that = new GeoSearchStoreCommandArgs();
650+
that.flags.addAll(this.flags);
651+
that.limit = this.limit;
652+
that.sortDirection = this.sortDirection;
653+
return that;
647654
}
648655
}
649656

@@ -729,18 +736,18 @@ public GeoRadiusCommandArgs limit(long count) {
729736
return this;
730737
}
731738

732-
public enum Flag {
739+
public enum Flag implements GeoCommandFlag {
733740
WITHCOORD, WITHDIST, ANY, STOREDIST
734741
}
735742

736743
@Override
737744
protected GeoRadiusCommandArgs clone() {
738745

739-
GeoRadiusCommandArgs tmp = new GeoRadiusCommandArgs();
740-
tmp.flags = this.flags != null ? new LinkedHashSet<>(this.flags) : new LinkedHashSet<>(2);
741-
tmp.limit = this.limit;
742-
tmp.sortDirection = this.sortDirection;
743-
return tmp;
746+
GeoRadiusCommandArgs that = new GeoRadiusCommandArgs();
747+
that.flags.addAll(this.flags);
748+
that.limit = this.limit;
749+
that.sortDirection = this.sortDirection;
750+
return that;
744751
}
745752
}
746753

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import org.springframework.core.convert.converter.Converter;
3232
import org.springframework.dao.DataAccessException;
33+
import org.springframework.data.domain.Sort;
3334
import org.springframework.data.geo.Distance;
3435
import org.springframework.data.geo.GeoResult;
3536
import org.springframework.data.geo.GeoResults;
@@ -868,29 +869,27 @@ public static GeoArgs.Unit toGeoArgsUnit(Metric metric) {
868869
* @since 1.8
869870
*/
870871
public static GeoArgs toGeoArgs(GeoRadiusCommandArgs args) {
871-
return toGeoArgs((GeoSearchCommandArgs) args);
872+
return toGeoArgs((GeoCommandArgs) args);
872873
}
873874

874875
/**
875-
* Convert {@link GeoRadiusCommandArgs} into {@link GeoArgs}.
876+
* Convert {@link GeoCommandArgs} into {@link GeoArgs}.
876877
*
877878
* @param args
878879
* @return
879880
* @since 2.6
880881
*/
881-
public static GeoArgs toGeoArgs(GeoSearchCommandArgs args) {
882+
public static GeoArgs toGeoArgs(GeoCommandArgs args) {
882883

883884
GeoArgs geoArgs = new GeoArgs();
884885

885886
if (args.hasFlags()) {
886-
for (GeoRadiusCommandArgs.Flag flag : args.getFlags()) {
887-
switch (flag) {
888-
case WITHCOORD:
889-
geoArgs.withCoordinates();
890-
break;
891-
case WITHDIST:
892-
geoArgs.withDistance();
893-
break;
887+
for (GeoCommandArgs.GeoCommandFlag flag : args.getFlags()) {
888+
if(flag.equals(GeoRadiusCommandArgs.Flag.WITHCOORD)) {
889+
geoArgs.withCoordinates();
890+
}
891+
else if(flag.equals(GeoRadiusCommandArgs.Flag.WITHDIST)) {
892+
geoArgs.withDistance();
894893
}
895894
}
896895
}
@@ -907,36 +906,9 @@ public static GeoArgs toGeoArgs(GeoSearchCommandArgs args) {
907906
}
908907

909908
if (args.hasLimit()) {
910-
geoArgs.withCount(args.getLimit(), args.hasAnyLimit());
909+
geoArgs.withCount(args.getLimit(), args.getFlags().contains(GeoRadiusCommandArgs.Flag.ANY));
911910
}
912-
return geoArgs;
913-
}
914911

915-
/**
916-
* Convert {@link GeoRadiusCommandArgs} into {@link GeoArgs}.
917-
*
918-
* @param args
919-
* @return
920-
* @since 2.6
921-
*/
922-
static GeoArgs toGeoArgs(GeoSearchStoreCommandArgs args) {
923-
924-
GeoArgs geoArgs = new GeoArgs();
925-
926-
if (args.hasSortDirection()) {
927-
switch (args.getSortDirection()) {
928-
case ASC:
929-
geoArgs.asc();
930-
break;
931-
case DESC:
932-
geoArgs.desc();
933-
break;
934-
}
935-
}
936-
937-
if (args.hasLimit()) {
938-
geoArgs.withCount(args.getLimit(), args.hasLimit());
939-
}
940912
return geoArgs;
941913
}
942914

0 commit comments

Comments
 (0)