From 9d945ff886fabf32bc4615a2bffccf1c9ac0afe6 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Mon, 6 May 2024 11:47:58 +0600 Subject: [PATCH] Add methods in CommandArguments and RawableFactory (#3834) * Add direct methods in CommandArguments * Add methods for boolean and long in RawableFactory --- .../redis/clients/jedis/CommandArguments.java | 42 ++++++++++++++++--- .../clients/jedis/args/RawableFactory.java | 18 ++++++++ 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/main/java/redis/clients/jedis/CommandArguments.java b/src/main/java/redis/clients/jedis/CommandArguments.java index b9190245ce..c630ae76de 100644 --- a/src/main/java/redis/clients/jedis/CommandArguments.java +++ b/src/main/java/redis/clients/jedis/CommandArguments.java @@ -1,6 +1,7 @@ package redis.clients.jedis; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; @@ -29,6 +30,35 @@ public ProtocolCommand getCommand() { return (ProtocolCommand) args.get(0); } + public CommandArguments add(Rawable arg) { + args.add(arg); + return this; + } + + public CommandArguments add(byte[] arg) { + return add(RawableFactory.from(arg)); + } + + public CommandArguments add(boolean arg) { + return add(RawableFactory.from(arg)); + } + + public CommandArguments add(int arg) { + return add(RawableFactory.from(arg)); + } + + public CommandArguments add(long arg) { + return add(RawableFactory.from(arg)); + } + + public CommandArguments add(double arg) { + return add(RawableFactory.from(arg)); + } + + public CommandArguments add(String arg) { + return add(RawableFactory.from(arg)); + } + public CommandArguments add(Object arg) { if (arg == null) { throw new IllegalArgumentException("null is not a valid argument."); @@ -36,12 +66,14 @@ public CommandArguments add(Object arg) { args.add((Rawable) arg); } else if (arg instanceof byte[]) { args.add(RawableFactory.from((byte[]) arg)); + } else if (arg instanceof Boolean) { + args.add(RawableFactory.from((Boolean) arg)); } else if (arg instanceof Integer) { args.add(RawableFactory.from((Integer) arg)); + } else if (arg instanceof Long) { + args.add(RawableFactory.from((Long) arg)); } else if (arg instanceof Double) { args.add(RawableFactory.from((Double) arg)); - } else if (arg instanceof Boolean) { - args.add(RawableFactory.from((Boolean) arg ? 1 : 0)); } else if (arg instanceof float[]) { args.add(RawableFactory.from(RediSearchUtil.toByteArray((float[]) arg))); } else if (arg instanceof String) { @@ -87,14 +119,12 @@ public CommandArguments key(Object key) { } public final CommandArguments keys(Object... keys) { - for (Object key : keys) { - key(key); - } + Arrays.stream(keys).forEach(this::key); return this; } public final CommandArguments keys(Collection keys) { - keys.forEach(key -> key(key)); + keys.forEach(this::key); return this; } diff --git a/src/main/java/redis/clients/jedis/args/RawableFactory.java b/src/main/java/redis/clients/jedis/args/RawableFactory.java index 8bce7f0e78..813ddd021b 100644 --- a/src/main/java/redis/clients/jedis/args/RawableFactory.java +++ b/src/main/java/redis/clients/jedis/args/RawableFactory.java @@ -10,6 +10,15 @@ */ public final class RawableFactory { + /** + * Get a {@link Rawable} from a {@code boolean}. + * @param b boolean value + * @return raw + */ + public static Rawable from(boolean b) { + return from(toByteArray(b)); + } + /** * Get a {@link Rawable} from an {@code int}. * @param i integer value @@ -19,6 +28,15 @@ public static Rawable from(int i) { return from(toByteArray(i)); } + /** + * Get a {@link Rawable} from a {@code long}. + * @param l long value + * @return raw + */ + public static Rawable from(long l) { + return from(toByteArray(l)); + } + /** * Get a {@link Rawable} from a {@code double}. * @param d numeric value