From f69146c2a6d0affe4fd5d3a855c6cf046e32d18e Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 17 Jun 2021 14:40:10 +0200 Subject: [PATCH] Polishing. Add since tags, equals/hashCode/toString methods. Reformat code. Original pull request: #2060. Closes #2055 --- .../redis/connection/BitFieldSubCommands.java | 285 +++++++++++++++++- .../BitFieldSubCommandsUnitTests.java | 66 ++-- 2 files changed, 316 insertions(+), 35 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 821db4010..63e47783b 100644 --- a/src/main/java/org/springframework/data/redis/connection/BitFieldSubCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/BitFieldSubCommands.java @@ -24,6 +24,7 @@ import java.util.List; import org.springframework.data.redis.connection.BitFieldSubCommands.BitFieldSubCommand; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.ObjectUtils; /** * The actual {@code BITFIELD} command representation holding several {@link BitFieldSubCommand}s to execute. @@ -62,8 +63,12 @@ public class BitFieldSubCommands implements Iterable { * Creates a new {@link BitFieldSubCommands} with Multiple BitFieldSubCommand. * * @return + * @since 2.5.2 */ public static BitFieldSubCommands create(BitFieldSubCommand... subCommands) { + + Assert.notNull(subCommands, "Subcommands must not be null"); + return new BitFieldSubCommands(Arrays.asList(subCommands)); } @@ -145,6 +150,44 @@ public class BitFieldSubCommands implements Iterable { return subCommands.iterator(); } + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof BitFieldSubCommands)) { + return false; + } + BitFieldSubCommands that = (BitFieldSubCommands) o; + return ObjectUtils.nullSafeEquals(subCommands, that.subCommands); + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + return ObjectUtils.nullSafeHashCode(subCommands); + } + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + final StringBuffer sb = new StringBuffer(); + sb.append(getClass().getSimpleName()); + sb.append(" [subCommands=").append(subCommands); + sb.append(']'); + return sb.toString(); + } + /** * @author Christoph Strobl */ @@ -203,7 +246,7 @@ public class BitFieldSubCommands implements Iterable { /** * @author Christoph Strobl */ - public class BitFieldGetBuilder { + public static class BitFieldGetBuilder { private BitFieldSubCommands ref; @@ -407,6 +450,37 @@ public class BitFieldSubCommands implements Iterable { public String toString() { return asString(); } + + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object o) { + + if (this == o) { + return true; + } + if (!(o instanceof Offset)) { + return false; + } + Offset that = (Offset) o; + if (offset != that.offset) { + return false; + } + return zeroBased == that.zeroBased; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + int result = (int) (offset ^ (offset >>> 32)); + result = 31 * result + (zeroBased ? 1 : 0); + return result; + } } /** @@ -497,6 +571,37 @@ public class BitFieldSubCommands implements Iterable { return (isSigned() ? "i" : "u") + getBits(); } + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object o) { + + if (this == o) { + return true; + } + if (!(o instanceof BitFieldType)) { + return false; + } + BitFieldType that = (BitFieldType) o; + if (signed != that.signed) { + return false; + } + return bits == that.bits; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + int result = (signed ? 1 : 0); + result = 31 * result + bits; + return result; + } + /* * (non-Javadoc) * @see java.lang.Object#toString() @@ -505,6 +610,7 @@ public class BitFieldSubCommands implements Iterable { public String toString() { return asString(); } + } /** @@ -532,6 +638,55 @@ public class BitFieldSubCommands implements Iterable { public Offset getOffset() { return offset; } + + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object o) { + + if (this == o) { + return true; + } + if (!(o instanceof AbstractBitFieldSubCommand)) { + return false; + } + AbstractBitFieldSubCommand that = (AbstractBitFieldSubCommand) o; + if (!ObjectUtils.nullSafeEquals(getClass(), that.getClass())) { + return false; + } + if (!ObjectUtils.nullSafeEquals(type, that.type)) { + return false; + } + return ObjectUtils.nullSafeEquals(offset, that.offset); + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + + int result = ObjectUtils.nullSafeHashCode(type); + result = 31 * result + ObjectUtils.nullSafeHashCode(offset); + return result; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append(getClass().getSimpleName()); + sb.append(" [type=").append(type); + sb.append(", offset=").append(offset); + sb.append(']'); + return sb.toString(); + } } /** @@ -546,16 +701,23 @@ public class BitFieldSubCommands implements Iterable { /** * 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 + * @since 2.5.2 */ public static BitFieldSet create(BitFieldType type,Offset offset,long value){ + + Assert.notNull(type, "BitFieldType must not be null"); + Assert.notNull(offset, "Offset must not be null"); + BitFieldSet instance = new BitFieldSet(); instance.type = type; instance.offset = offset; instance.value = value; + return instance; } @@ -577,6 +739,53 @@ public class BitFieldSubCommands implements Iterable { return value; } + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof BitFieldSet)) { + return false; + } + if (!super.equals(o)) { + return false; + } + BitFieldSet that = (BitFieldSet) o; + if (value != that.value) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (int) (value ^ (value >>> 32)); + return result; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append(getClass().getSimpleName()); + sb.append(" [type=").append(type); + sb.append(", offset=").append(offset); + sb.append(", value=").append(value); + sb.append(']'); + return sb.toString(); + } } /** @@ -589,14 +798,21 @@ public class BitFieldSubCommands implements Iterable { /** * Creates a new {@link BitFieldGet}. + * * @param type must not be {@literal null}. * @param offset must not be {@literal null}. + * @since 2.5.2 * @return */ public static BitFieldGet create(BitFieldType type,Offset offset){ + + Assert.notNull(type, "BitFieldType must not be null"); + Assert.notNull(offset, "Offset must not be null"); + BitFieldGet instance = new BitFieldGet(); instance.type = type; instance.offset = offset; + return instance; } @@ -624,33 +840,38 @@ public class BitFieldSubCommands implements Iterable { /** * 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 + * @since 2.5.2 */ 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; + return create(type, offset, value, null); } /** * 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. + * @param overflow can be {@literal null} to use redis defaults. + * @since 2.5.2 * @return */ - public static BitFieldIncrBy create(BitFieldType type,Offset offset,long value,Overflow overflow){ + public static BitFieldIncrBy create(BitFieldType type, Offset offset, long value, @Nullable Overflow overflow) { + + Assert.notNull(type, "BitFieldType must not be null"); + Assert.notNull(offset, "Offset must not be null"); + BitFieldIncrBy instance = new BitFieldIncrBy(); instance.type = type; instance.offset = offset; instance.value = value; instance.overflow = overflow; + return instance; } @@ -688,5 +909,53 @@ public class BitFieldSubCommands implements Iterable { public enum Overflow { SAT, FAIL, WRAP } + + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object o) { + + if (this == o) { + return true; + } + if (!(o instanceof BitFieldIncrBy)) { + return false; + } + BitFieldIncrBy that = (BitFieldIncrBy) o; + if (value != that.value) { + return false; + } + return overflow == that.overflow; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + + int result = (int) (value ^ (value >>> 32)); + result = 31 * result + ObjectUtils.nullSafeHashCode(overflow); + return result; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append(getClass().getSimpleName()); + sb.append(" [type=").append(type); + sb.append(", offset=").append(offset); + sb.append(", value=").append(value); + sb.append(", overflow=").append(overflow); + sb.append(']'); + return sb.toString(); + } } } 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 21a14f7f8..e6ed3a081 100644 --- a/src/test/java/org/springframework/data/redis/connection/BitFieldSubCommandsUnitTests.java +++ b/src/test/java/org/springframework/data/redis/connection/BitFieldSubCommandsUnitTests.java @@ -15,15 +15,10 @@ */ package org.springframework.data.redis.connection; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.*; +import static org.springframework.data.redis.connection.BitFieldSubCommands.*; - -import org.springframework.data.redis.connection.BitFieldSubCommands.BitFieldType; - -import java.util.ArrayList; -import java.util.List; +import org.junit.jupiter.api.Test; /** * Unit tests for {@link BitFieldSubCommands}. @@ -51,33 +46,50 @@ class BitFieldSubCommandsUnitTests { assertThat(type.getBits()).isEqualTo(10); } - @Test //ISSUES #2055 - void shouldCreateBitCommandsWithChainingMethod(){ + @Test // GH-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); + BitFieldType type = BitFieldType.unsigned(1); + BitFieldSubCommands bitFieldSubCommands = BitFieldSubCommands.create().get(type).valueAt(Offset.offset(1)).get(type) + .valueAt(Offset.offset(2)).set(type).valueAt(Offset.offset(3)).to(1).set(type).valueAt(Offset.offset(4)).to(1) + .incr(type).valueAt(Offset.offset(5)).by(1); - assertThat(bitFieldSubCommands.getSubCommands().size()).isEqualTo(5); + assertThat(bitFieldSubCommands.getSubCommands()).hasSize(5); } - @Test //ISSUES #2055 - void shouldCreateBitCommandsWithNonChainingMethod(){ + @Test // GH-2055 + void shouldCreateEqualObjects() { - BitFieldType type = BitFieldType.unsigned(1); - BitFieldSubCommands.Offset offset = BitFieldSubCommands.Offset.offset(1); + BitFieldType type = BitFieldType.unsigned(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 createdWithBuilder = BitFieldSubCommands.create() // + .get(type).valueAt(Offset.offset(2)) // + .set(type).valueAt(Offset.offset(3)).to(1) // + .incr(type).valueAt(Offset.offset(5)).by(1); - BitFieldSubCommands bitFieldSubCommands = BitFieldSubCommands.create(subGetCommand,subSetCommand,subIncrByCommand,subIncrByCommand2); + BitFieldSubCommand subGetCommand = BitFieldGet.create(type, Offset.offset(2)); + BitFieldSubCommand subSetCommand = BitFieldSet.create(type, Offset.offset(3), 1); + BitFieldSubCommand subIncrByCommand = BitFieldIncrBy.create(type, Offset.offset(5), 1); - assertThat(bitFieldSubCommands.getSubCommands().size()).isEqualTo(4); + BitFieldSubCommands createdWithCreate = BitFieldSubCommands.create(subGetCommand, subSetCommand, subIncrByCommand); + + assertThat(createdWithBuilder).isEqualTo(createdWithCreate).hasSameHashCodeAs(createdWithCreate); + } + + @Test // GH-2055 + void shouldCreateBitCommandsWithNonChainingMethod() { + + BitFieldType type = BitFieldType.unsigned(1); + Offset offset = Offset.offset(1); + + BitFieldSubCommand subGetCommand = BitFieldGet.create(type, offset); + BitFieldSubCommand subSetCommand = BitFieldSet.create(type, offset, 1); + BitFieldSubCommand subIncrByCommand = BitFieldIncrBy.create(type, offset, 1); + BitFieldSubCommand subIncrByCommand2 = BitFieldIncrBy.create(type, offset, 1, BitFieldIncrBy.Overflow.FAIL); + + BitFieldSubCommands bitFieldSubCommands = BitFieldSubCommands.create(subGetCommand, subSetCommand, subIncrByCommand, + subIncrByCommand2); + + assertThat(bitFieldSubCommands.getSubCommands()).hasSize(4); } }