From 200fe89d08fbc259f7f86c64318292ae4fef59e0 Mon Sep 17 00:00:00 2001 From: yanam Date: Fri, 7 May 2021 21:51:44 +0800 Subject: [PATCH 1/2] Add support for BitFieldSubCommands that generate multiple bit operations using non-chaining methods --- .../redis/connection/BitFieldSubCommands.java | 35 ++++++++++++++++-- .../BitFieldSubCommandsUnitTests.java | 36 +++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/connection/BitFieldSubCommands.java b/src/main/java/org/springframework/data/redis/connection/BitFieldSubCommands.java index 0e60b34cba..648ceeb275 100644 --- a/src/main/java/org/springframework/data/redis/connection/BitFieldSubCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/BitFieldSubCommands.java @@ -29,17 +29,18 @@ * * @author Christoph Strobl * @author Qiang Lee + * @author Yanam * @since 2.1 */ public class BitFieldSubCommands implements Iterable { private final List subCommands; - private BitFieldSubCommands(List subCommands) { + public BitFieldSubCommands(List subCommands) { this.subCommands = new ArrayList<>(subCommands); } - private BitFieldSubCommands(List subCommands, BitFieldSubCommand subCommand) { + public BitFieldSubCommands(List subCommands, BitFieldSubCommand subCommand) { this(subCommands); @@ -533,6 +534,14 @@ public static class BitFieldSet extends AbstractBitFieldSubCommand { private long value; + public BitFieldSet(BitFieldType type,Offset offset,long value){ + this.type = type; + this.offset = offset; + this.value = value; + } + + public BitFieldSet(){} + /* * (non-Javadoc) * @see org.springframework.data.redis.connection.StringRedisConnection.BitFieldSubCommand#getCommand() @@ -561,6 +570,13 @@ public long getValue() { */ public static class BitFieldGet extends AbstractBitFieldSubCommand { + public BitFieldGet(BitFieldType type,Offset offset){ + this.type = type; + this.offset = offset; + } + + public BitFieldGet(){} + /* * (non-Javadoc) * @see org.springframework.data.redis.connection.StringRedisConnection.BitFieldSubCommand#getCommand() @@ -583,6 +599,21 @@ public static class BitFieldIncrBy extends AbstractBitFieldSubCommand { private long value; private @Nullable Overflow overflow; + public BitFieldIncrBy(BitFieldType type,Offset offset,long value){ + this.type = type; + this.offset = offset; + this.value = value; + } + + public BitFieldIncrBy(BitFieldType type,Offset offset,long value,Overflow overflow){ + this.type = type; + this.offset = offset; + this.value = value; + this.overflow = overflow; + } + + public BitFieldIncrBy(){} + /* * (non-Javadoc) * @see org.springframework.data.redis.connection.StringRedisConnection.BitFieldSubCommand#getCommand() diff --git a/src/test/java/org/springframework/data/redis/connection/BitFieldSubCommandsUnitTests.java b/src/test/java/org/springframework/data/redis/connection/BitFieldSubCommandsUnitTests.java index 00e319473e..f28c5036c7 100644 --- a/src/test/java/org/springframework/data/redis/connection/BitFieldSubCommandsUnitTests.java +++ b/src/test/java/org/springframework/data/redis/connection/BitFieldSubCommandsUnitTests.java @@ -22,10 +22,14 @@ import org.springframework.data.redis.connection.BitFieldSubCommands.BitFieldType; +import java.util.ArrayList; +import java.util.List; + /** * Unit tests for {@link BitFieldSubCommands}. * * @author Mark Paluch + * @author Yanam */ class BitFieldSubCommandsUnitTests { @@ -46,4 +50,36 @@ void shouldCreateUnsignedBitFieldType() { assertThat(type.isSigned()).isFalse(); assertThat(type.getBits()).isEqualTo(10); } + + @Test //ISSUES #2055 + void shouldCreateBitCommandsWithChainingMethod(){ + + BitFieldType type = BitFieldType.unsigned(1); + BitFieldSubCommands bitFieldSubCommands = BitFieldSubCommands.create() + .get(type).valueAt(BitFieldSubCommands.Offset.offset(1)) + .get(type).valueAt(BitFieldSubCommands.Offset.offset(2)) + .set(type).valueAt(BitFieldSubCommands.Offset.offset(3)).to(1) + .set(type).valueAt(BitFieldSubCommands.Offset.offset(4)).to(1) + .incr(type).valueAt(BitFieldSubCommands.Offset.offset(5)).by(1); + + assertThat(bitFieldSubCommands.getSubCommands().size()).isEqualTo(5); + } + + @Test //ISSUES #2055 + void shouldCreateBitCommandsWithNonChainingMethod(){ + + List subCommandList = new ArrayList<>(); + + BitFieldType type = BitFieldType.unsigned(1); + + subCommandList.add(new BitFieldSubCommands.BitFieldGet(type, BitFieldSubCommands.Offset.offset(1))); + subCommandList.add(new BitFieldSubCommands.BitFieldSet(type, BitFieldSubCommands.Offset.offset(2),2)); + subCommandList.add(new BitFieldSubCommands.BitFieldIncrBy(type, BitFieldSubCommands.Offset.offset(3),3)); + subCommandList.add(new BitFieldSubCommands.BitFieldIncrBy(type, BitFieldSubCommands.Offset.offset(4),4, + BitFieldSubCommands.BitFieldIncrBy.Overflow.FAIL)); + + BitFieldSubCommands bitFieldSubCommands = new BitFieldSubCommands(subCommandList); + + assertThat(bitFieldSubCommands.getSubCommands().size()).isEqualTo(4); + } } From 84ce5382b39b8f31bba89d33f8b932b4f47cadbc Mon Sep 17 00:00:00 2001 From: yanam Date: Tue, 18 May 2021 22:39:39 +0800 Subject: [PATCH 2/2] Improve the implementation to make the API controllable --- .../redis/connection/BitFieldSubCommands.java | 88 ++++++++++++++----- .../BitFieldSubCommandsUnitTests.java | 14 ++- 2 files changed, 70 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/connection/BitFieldSubCommands.java b/src/main/java/org/springframework/data/redis/connection/BitFieldSubCommands.java index 648ceeb275..821db40106 100644 --- a/src/main/java/org/springframework/data/redis/connection/BitFieldSubCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/BitFieldSubCommands.java @@ -16,6 +16,7 @@ package org.springframework.data.redis.connection; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -36,11 +37,11 @@ public class BitFieldSubCommands implements Iterable { private final List subCommands; - public BitFieldSubCommands(List subCommands) { + private BitFieldSubCommands(List subCommands) { this.subCommands = new ArrayList<>(subCommands); } - public BitFieldSubCommands(List subCommands, BitFieldSubCommand subCommand) { + private BitFieldSubCommands(List subCommands, BitFieldSubCommand subCommand) { this(subCommands); @@ -57,6 +58,15 @@ public static BitFieldSubCommands create() { return new BitFieldSubCommands(Collections.emptyList()); } + /** + * Creates a new {@link BitFieldSubCommands} with Multiple BitFieldSubCommand. + * + * @return + */ + public static BitFieldSubCommands create(BitFieldSubCommand... subCommands) { + return new BitFieldSubCommands(Arrays.asList(subCommands)); + } + /** * Obtain a new {@link BitFieldGetBuilder} for creating and adding a {@link BitFieldGet} sub command. * @@ -534,14 +544,21 @@ public static class BitFieldSet extends AbstractBitFieldSubCommand { private long value; - public BitFieldSet(BitFieldType type,Offset offset,long value){ - this.type = type; - this.offset = offset; - this.value = value; + /** + * Creates a new {@link BitFieldSet}. + * @param type must not be {@literal null}. + * @param offset must not be {@literal null}. + * @param value must not be {@literal null}. + * @return + */ + public static BitFieldSet create(BitFieldType type,Offset offset,long value){ + BitFieldSet instance = new BitFieldSet(); + instance.type = type; + instance.offset = offset; + instance.value = value; + return instance; } - public BitFieldSet(){} - /* * (non-Javadoc) * @see org.springframework.data.redis.connection.StringRedisConnection.BitFieldSubCommand#getCommand() @@ -570,13 +587,19 @@ public long getValue() { */ public static class BitFieldGet extends AbstractBitFieldSubCommand { - public BitFieldGet(BitFieldType type,Offset offset){ - this.type = type; - this.offset = offset; + /** + * Creates a new {@link BitFieldGet}. + * @param type must not be {@literal null}. + * @param offset must not be {@literal null}. + * @return + */ + public static BitFieldGet create(BitFieldType type,Offset offset){ + BitFieldGet instance = new BitFieldGet(); + instance.type = type; + instance.offset = offset; + return instance; } - public BitFieldGet(){} - /* * (non-Javadoc) * @see org.springframework.data.redis.connection.StringRedisConnection.BitFieldSubCommand#getCommand() @@ -599,21 +622,38 @@ public static class BitFieldIncrBy extends AbstractBitFieldSubCommand { private long value; private @Nullable Overflow overflow; - public BitFieldIncrBy(BitFieldType type,Offset offset,long value){ - this.type = type; - this.offset = offset; - this.value = value; + /** + * Creates a new {@link BitFieldIncrBy}. + * @param type must not be {@literal null}. + * @param offset must not be {@literal null}. + * @param value must not be {@literal null}. + * @return + */ + public static BitFieldIncrBy create(BitFieldType type,Offset offset,long value){ + BitFieldIncrBy instance = new BitFieldIncrBy(); + instance.type = type; + instance.offset = offset; + instance.value = value; + return instance; } - public BitFieldIncrBy(BitFieldType type,Offset offset,long value,Overflow overflow){ - this.type = type; - this.offset = offset; - this.value = value; - this.overflow = overflow; + /** + * Creates a new {@link BitFieldIncrBy}. + * @param type must not be {@literal null}. + * @param offset must not be {@literal null}. + * @param value must not be {@literal null}. + * @param overflow Can be {@literal null} to use redis defaults. + * @return + */ + public static BitFieldIncrBy create(BitFieldType type,Offset offset,long value,Overflow overflow){ + BitFieldIncrBy instance = new BitFieldIncrBy(); + instance.type = type; + instance.offset = offset; + instance.value = value; + instance.overflow = overflow; + return instance; } - public BitFieldIncrBy(){} - /* * (non-Javadoc) * @see org.springframework.data.redis.connection.StringRedisConnection.BitFieldSubCommand#getCommand() diff --git a/src/test/java/org/springframework/data/redis/connection/BitFieldSubCommandsUnitTests.java b/src/test/java/org/springframework/data/redis/connection/BitFieldSubCommandsUnitTests.java index f28c5036c7..21a14f7f87 100644 --- a/src/test/java/org/springframework/data/redis/connection/BitFieldSubCommandsUnitTests.java +++ b/src/test/java/org/springframework/data/redis/connection/BitFieldSubCommandsUnitTests.java @@ -68,17 +68,15 @@ void shouldCreateBitCommandsWithChainingMethod(){ @Test //ISSUES #2055 void shouldCreateBitCommandsWithNonChainingMethod(){ - List subCommandList = new ArrayList<>(); - BitFieldType type = BitFieldType.unsigned(1); + BitFieldSubCommands.Offset offset = BitFieldSubCommands.Offset.offset(1); - subCommandList.add(new BitFieldSubCommands.BitFieldGet(type, BitFieldSubCommands.Offset.offset(1))); - subCommandList.add(new BitFieldSubCommands.BitFieldSet(type, BitFieldSubCommands.Offset.offset(2),2)); - subCommandList.add(new BitFieldSubCommands.BitFieldIncrBy(type, BitFieldSubCommands.Offset.offset(3),3)); - subCommandList.add(new BitFieldSubCommands.BitFieldIncrBy(type, BitFieldSubCommands.Offset.offset(4),4, - BitFieldSubCommands.BitFieldIncrBy.Overflow.FAIL)); + BitFieldSubCommands.BitFieldSubCommand subGetCommand = BitFieldSubCommands.BitFieldGet.create(type,offset); + BitFieldSubCommands.BitFieldSubCommand subSetCommand = BitFieldSubCommands.BitFieldSet.create(type,offset,1); + BitFieldSubCommands.BitFieldSubCommand subIncrByCommand = BitFieldSubCommands.BitFieldIncrBy.create(type,offset,1); + BitFieldSubCommands.BitFieldSubCommand subIncrByCommand2 = BitFieldSubCommands.BitFieldIncrBy.create(type,offset,1,BitFieldSubCommands.BitFieldIncrBy.Overflow.FAIL); - BitFieldSubCommands bitFieldSubCommands = new BitFieldSubCommands(subCommandList); + BitFieldSubCommands bitFieldSubCommands = BitFieldSubCommands.create(subGetCommand,subSetCommand,subIncrByCommand,subIncrByCommand2); assertThat(bitFieldSubCommands.getSubCommands().size()).isEqualTo(4); }