Skip to content

Commit

Permalink
Add methods in CommandArguments and RawableFactory (#3834)
Browse files Browse the repository at this point in the history
* Add direct methods in CommandArguments

* Add methods for boolean and long in RawableFactory
  • Loading branch information
sazzad16 committed Jul 9, 2024
1 parent 927c923 commit a42d0cd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/main/java/redis/clients/jedis/CommandArguments.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis.clients.jedis;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

Expand Down Expand Up @@ -29,19 +30,50 @@ 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.");
} else if (arg instanceof Rawable) {
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) {
Expand Down Expand Up @@ -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;
}

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/redis/clients/jedis/args/RawableFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a42d0cd

Please sign in to comment.