All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.swisspush.redisques.util.RedisUtils Maven / Gradle / Ivy

There is a newer version: 4.1.10
Show newest version
package org.swisspush.redisques.util;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.util.*;
import java.util.stream.Stream;

/**
 * Useful utilities for Redis
 *
 */
public final class RedisUtils {

    private RedisUtils() {}

    /**
     * from https://github.com/vert-x3/vertx-redis-client/blob/3.9/src/main/java/io/vertx/redis/impl/RedisClientImpl.java#L94
     *
     * @param parameters
     * @return
     */
    public static List toPayload(Object... parameters) {
        List result = new ArrayList<>(parameters.length);

        for (Object param : parameters) {
            // unwrap
            if (param instanceof JsonArray) {
                param = ((JsonArray) param).getList();
            }
            // unwrap
            if (param instanceof JsonObject) {
                param = ((JsonObject) param).getMap();
            }

            if (param instanceof Collection) {
                ((Collection) param).stream().filter(Objects::nonNull).forEach(o -> result.add(o.toString()));
            } else if (param instanceof Map) {
                for (Map.Entry pair : ((Map) param).entrySet()) {
                    result.add(pair.getKey().toString());
                    result.add(pair.getValue().toString());
                }
            } else if (param instanceof Stream) {
                ((Stream) param).forEach(e -> {
                    if (e instanceof Object[]) {
                        Collections.addAll(result, (String[]) e);
                    } else {
                        result.add(e.toString());
                    }
                });
            } else if (param != null) {
                result.add(param.toString());
            }
        }
        return result;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy