Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,7 @@
*
* @author Christoph Strobl
* @author Qiang Lee
* @author Yanam
* @since 2.1
*/
public class BitFieldSubCommands implements Iterable<BitFieldSubCommand> {
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
}
}