From 6fca7e9a3f1995baf7d31058ff4e867afb9d20c7 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Thu, 23 Jun 2022 10:36:46 +0300 Subject: [PATCH] refactoring --- .../RedissonTransactionalBucket.java | 63 ++++--------------- 1 file changed, 11 insertions(+), 52 deletions(-) diff --git a/redisson/src/main/java/org/redisson/transaction/RedissonTransactionalBucket.java b/redisson/src/main/java/org/redisson/transaction/RedissonTransactionalBucket.java index ec84eb5f7..2726efbcb 100644 --- a/redisson/src/main/java/org/redisson/transaction/RedissonTransactionalBucket.java +++ b/redisson/src/main/java/org/redisson/transaction/RedissonTransactionalBucket.java @@ -29,6 +29,7 @@ import org.redisson.transaction.operation.UnlinkOperation; import org.redisson.transaction.operation.bucket.*; import java.util.List; +import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.concurrent.TimeUnit; @@ -113,11 +114,7 @@ public class RedissonTransactionalBucket extends RedissonBucket { public RFuture isExistsAsync() { checkState(); if (state != null) { - if (state == NULL) { - return new CompletableFutureWrapper<>((Boolean) null); - } else { - return new CompletableFutureWrapper<>(true); - } + return new CompletableFutureWrapper<>(state != NULL); } return super.isExistsAsync(); @@ -210,11 +207,7 @@ public class RedissonTransactionalBucket extends RedissonBucket { operations.add(new BucketCompareAndSetOperation(getRawName(), getLockName(), getCodec(), expect, update, transactionId, currentThreadId)); if ((state == NULL && expect == null) || isEquals(state, expect)) { - if (update == null) { - state = NULL; - } else { - state = update; - } + state = Optional.ofNullable((Object) update).orElse(NULL); return CompletableFuture.completedFuture(true); } return CompletableFuture.completedFuture(false); @@ -224,11 +217,7 @@ public class RedissonTransactionalBucket extends RedissonBucket { operations.add(new BucketCompareAndSetOperation(getRawName(), getLockName(), getCodec(), expect, update, transactionId, currentThreadId)); if ((res == null && expect == null) || isEquals(res, expect)) { - if (update == null) { - state = NULL; - } else { - state = update; - } + state = Optional.ofNullable((Object) update).orElse(NULL); return true; } return false; @@ -251,27 +240,14 @@ public class RedissonTransactionalBucket extends RedissonBucket { checkState(); return executeLocked(() -> { if (state != null) { - Object prevValue; - if (state == NULL) { - prevValue = null; - } else { - prevValue = state; - } + Object prevValue = Optional.of(state).filter(s -> s != NULL).orElse(null); operations.add(operation); - if (newValue == null) { - state = NULL; - } else { - state = newValue; - } + state = Optional.ofNullable((Object) newValue).orElse(NULL); return CompletableFuture.completedFuture((V) prevValue); } return getAsync().thenApply(res -> { - if (newValue == null) { - state = NULL; - } else { - state = newValue; - } + state = Optional.ofNullable((Object) newValue).orElse(NULL); operations.add(operation); return res; }); @@ -285,12 +261,7 @@ public class RedissonTransactionalBucket extends RedissonBucket { long currentThreadId = Thread.currentThread().getId(); return executeLocked(() -> { if (state != null) { - Object prevValue; - if (state == NULL) { - prevValue = null; - } else { - prevValue = state; - } + Object prevValue = Optional.of(state).filter(s -> s != NULL).orElse(null); operations.add(new BucketGetAndDeleteOperation(getRawName(), getLockName(), getCodec(), transactionId, currentThreadId)); state = NULL; return CompletableFuture.completedFuture((V) prevValue); @@ -314,11 +285,7 @@ public class RedissonTransactionalBucket extends RedissonBucket { checkState(); return executeLocked(() -> { operations.add(operation); - if (newValue == null) { - state = NULL; - } else { - state = newValue; - } + state = Optional.ofNullable((Object) newValue).orElse(NULL); return CompletableFuture.completedFuture(null); }); } @@ -347,11 +314,7 @@ public class RedissonTransactionalBucket extends RedissonBucket { if (state != null) { operations.add(operation); if (state == NULL) { - if (newValue == null) { - state = NULL; - } else { - state = newValue; - } + state = Optional.ofNullable((Object) newValue).orElse(NULL); return CompletableFuture.completedFuture(true); } else { return CompletableFuture.completedFuture(false); @@ -361,11 +324,7 @@ public class RedissonTransactionalBucket extends RedissonBucket { return getAsync().thenApply(res -> { operations.add(operation); if (res == null) { - if (newValue == null) { - state = NULL; - } else { - state = newValue; - } + state = Optional.ofNullable((Object) newValue).orElse(NULL); return true; } return false;