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..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; @@ -29,6 +30,7 @@ * * @author Christoph Strobl * @author Qiang Lee + * @author Yanam * @since 2.1 */ public class BitFieldSubCommands implements Iterable { @@ -56,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. * @@ -533,6 +544,21 @@ public static class BitFieldSet extends AbstractBitFieldSubCommand { private long 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; + } + /* * (non-Javadoc) * @see org.springframework.data.redis.connection.StringRedisConnection.BitFieldSubCommand#getCommand() @@ -561,6 +587,19 @@ public long getValue() { */ public static class BitFieldGet extends AbstractBitFieldSubCommand { + /** + * 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; + } + /* * (non-Javadoc) * @see org.springframework.data.redis.connection.StringRedisConnection.BitFieldSubCommand#getCommand() @@ -583,6 +622,38 @@ public static class BitFieldIncrBy extends AbstractBitFieldSubCommand { private long value; private @Nullable 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}. + * @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; + } + + /** + * 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; + } + /* * (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..21a14f7f87 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,34 @@ 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(){ + + BitFieldType type = BitFieldType.unsigned(1); + BitFieldSubCommands.Offset offset = BitFieldSubCommands.Offset.offset(1); + + 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 = BitFieldSubCommands.create(subGetCommand,subSetCommand,subIncrByCommand,subIncrByCommand2); + + assertThat(bitFieldSubCommands.getSubCommands().size()).isEqualTo(4); + } }