Add support for BitFieldSubCommands that generate multiple bit operations using non-chaining methods.

Original pull request: #2060.
Closes #2055
This commit is contained in:
yanam
2021-05-07 21:51:44 +08:00
committed by Mark Paluch
parent 26d1cf63e3
commit a2f2d9051a
2 changed files with 105 additions and 0 deletions

View File

@@ -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 @@ import org.springframework.util.Assert;
*
* @author Christoph Strobl
* @author Qiang Lee
* @author Yanam
* @since 2.1
*/
public class BitFieldSubCommands implements Iterable<BitFieldSubCommand> {
@@ -56,6 +58,15 @@ public class BitFieldSubCommands implements Iterable<BitFieldSubCommand> {
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 class BitFieldSubCommands implements Iterable<BitFieldSubCommand> {
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 class BitFieldSubCommands implements Iterable<BitFieldSubCommand> {
*/
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 class BitFieldSubCommands implements Iterable<BitFieldSubCommand> {
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()

View File

@@ -22,10 +22,14 @@ import static org.assertj.core.api.Assertions.*;
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 @@ class BitFieldSubCommandsUnitTests {
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);
}
}