Skip to content
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

Add methods in CommandArguments and RawableFactory #3834

Merged
merged 2 commits into from
May 6, 2024
Merged
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
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
Loading