Skip to content

Commit a2f2d90

Browse files
Yanammp911de
authored andcommitted
Add support for BitFieldSubCommands that generate multiple bit operations using non-chaining methods.
Original pull request: #2060. Closes #2055
1 parent 26d1cf6 commit a2f2d90

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

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

+71
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.redis.connection;
1717

1818
import java.util.ArrayList;
19+
import java.util.Arrays;
1920
import java.util.Collections;
2021
import java.util.Iterator;
2122
import java.util.List;
@@ -29,6 +30,7 @@
2930
*
3031
* @author Christoph Strobl
3132
* @author Qiang Lee
33+
* @author Yanam
3234
* @since 2.1
3335
*/
3436
public class BitFieldSubCommands implements Iterable<BitFieldSubCommand> {
@@ -56,6 +58,15 @@ public static BitFieldSubCommands create() {
5658
return new BitFieldSubCommands(Collections.emptyList());
5759
}
5860

61+
/**
62+
* Creates a new {@link BitFieldSubCommands} with Multiple BitFieldSubCommand.
63+
*
64+
* @return
65+
*/
66+
public static BitFieldSubCommands create(BitFieldSubCommand... subCommands) {
67+
return new BitFieldSubCommands(Arrays.asList(subCommands));
68+
}
69+
5970
/**
6071
* Obtain a new {@link BitFieldGetBuilder} for creating and adding a {@link BitFieldGet} sub command.
6172
*
@@ -533,6 +544,21 @@ public static class BitFieldSet extends AbstractBitFieldSubCommand {
533544

534545
private long value;
535546

547+
/**
548+
* Creates a new {@link BitFieldSet}.
549+
* @param type must not be {@literal null}.
550+
* @param offset must not be {@literal null}.
551+
* @param value must not be {@literal null}.
552+
* @return
553+
*/
554+
public static BitFieldSet create(BitFieldType type,Offset offset,long value){
555+
BitFieldSet instance = new BitFieldSet();
556+
instance.type = type;
557+
instance.offset = offset;
558+
instance.value = value;
559+
return instance;
560+
}
561+
536562
/*
537563
* (non-Javadoc)
538564
* @see org.springframework.data.redis.connection.StringRedisConnection.BitFieldSubCommand#getCommand()
@@ -561,6 +587,19 @@ public long getValue() {
561587
*/
562588
public static class BitFieldGet extends AbstractBitFieldSubCommand {
563589

590+
/**
591+
* Creates a new {@link BitFieldGet}.
592+
* @param type must not be {@literal null}.
593+
* @param offset must not be {@literal null}.
594+
* @return
595+
*/
596+
public static BitFieldGet create(BitFieldType type,Offset offset){
597+
BitFieldGet instance = new BitFieldGet();
598+
instance.type = type;
599+
instance.offset = offset;
600+
return instance;
601+
}
602+
564603
/*
565604
* (non-Javadoc)
566605
* @see org.springframework.data.redis.connection.StringRedisConnection.BitFieldSubCommand#getCommand()
@@ -583,6 +622,38 @@ public static class BitFieldIncrBy extends AbstractBitFieldSubCommand {
583622
private long value;
584623
private @Nullable Overflow overflow;
585624

625+
/**
626+
* Creates a new {@link BitFieldIncrBy}.
627+
* @param type must not be {@literal null}.
628+
* @param offset must not be {@literal null}.
629+
* @param value must not be {@literal null}.
630+
* @return
631+
*/
632+
public static BitFieldIncrBy create(BitFieldType type,Offset offset,long value){
633+
BitFieldIncrBy instance = new BitFieldIncrBy();
634+
instance.type = type;
635+
instance.offset = offset;
636+
instance.value = value;
637+
return instance;
638+
}
639+
640+
/**
641+
* Creates a new {@link BitFieldIncrBy}.
642+
* @param type must not be {@literal null}.
643+
* @param offset must not be {@literal null}.
644+
* @param value must not be {@literal null}.
645+
* @param overflow Can be {@literal null} to use redis defaults.
646+
* @return
647+
*/
648+
public static BitFieldIncrBy create(BitFieldType type,Offset offset,long value,Overflow overflow){
649+
BitFieldIncrBy instance = new BitFieldIncrBy();
650+
instance.type = type;
651+
instance.offset = offset;
652+
instance.value = value;
653+
instance.overflow = overflow;
654+
return instance;
655+
}
656+
586657
/*
587658
* (non-Javadoc)
588659
* @see org.springframework.data.redis.connection.StringRedisConnection.BitFieldSubCommand#getCommand()

src/test/java/org/springframework/data/redis/connection/BitFieldSubCommandsUnitTests.java

+34
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222

2323
import org.springframework.data.redis.connection.BitFieldSubCommands.BitFieldType;
2424

25+
import java.util.ArrayList;
26+
import java.util.List;
27+
2528
/**
2629
* Unit tests for {@link BitFieldSubCommands}.
2730
*
2831
* @author Mark Paluch
32+
* @author Yanam
2933
*/
3034
class BitFieldSubCommandsUnitTests {
3135

@@ -46,4 +50,34 @@ void shouldCreateUnsignedBitFieldType() {
4650
assertThat(type.isSigned()).isFalse();
4751
assertThat(type.getBits()).isEqualTo(10);
4852
}
53+
54+
@Test //ISSUES #2055
55+
void shouldCreateBitCommandsWithChainingMethod(){
56+
57+
BitFieldType type = BitFieldType.unsigned(1);
58+
BitFieldSubCommands bitFieldSubCommands = BitFieldSubCommands.create()
59+
.get(type).valueAt(BitFieldSubCommands.Offset.offset(1))
60+
.get(type).valueAt(BitFieldSubCommands.Offset.offset(2))
61+
.set(type).valueAt(BitFieldSubCommands.Offset.offset(3)).to(1)
62+
.set(type).valueAt(BitFieldSubCommands.Offset.offset(4)).to(1)
63+
.incr(type).valueAt(BitFieldSubCommands.Offset.offset(5)).by(1);
64+
65+
assertThat(bitFieldSubCommands.getSubCommands().size()).isEqualTo(5);
66+
}
67+
68+
@Test //ISSUES #2055
69+
void shouldCreateBitCommandsWithNonChainingMethod(){
70+
71+
BitFieldType type = BitFieldType.unsigned(1);
72+
BitFieldSubCommands.Offset offset = BitFieldSubCommands.Offset.offset(1);
73+
74+
BitFieldSubCommands.BitFieldSubCommand subGetCommand = BitFieldSubCommands.BitFieldGet.create(type,offset);
75+
BitFieldSubCommands.BitFieldSubCommand subSetCommand = BitFieldSubCommands.BitFieldSet.create(type,offset,1);
76+
BitFieldSubCommands.BitFieldSubCommand subIncrByCommand = BitFieldSubCommands.BitFieldIncrBy.create(type,offset,1);
77+
BitFieldSubCommands.BitFieldSubCommand subIncrByCommand2 = BitFieldSubCommands.BitFieldIncrBy.create(type,offset,1,BitFieldSubCommands.BitFieldIncrBy.Overflow.FAIL);
78+
79+
BitFieldSubCommands bitFieldSubCommands = BitFieldSubCommands.create(subGetCommand,subSetCommand,subIncrByCommand,subIncrByCommand2);
80+
81+
assertThat(bitFieldSubCommands.getSubCommands().size()).isEqualTo(4);
82+
}
4983
}

0 commit comments

Comments
 (0)