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

com.lambdaworks.redis.MigrateArgs Maven / Gradle / Ivy

Go to download

Advanced and thread-safe Java Redis client for synchronous, asynchronous, and reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs and much more.

The newest version!
/*
 * Copyright 2011-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambdaworks.redis;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.lambdaworks.redis.internal.LettuceAssert;
import com.lambdaworks.redis.protocol.CommandArgs;
import com.lambdaworks.redis.protocol.CommandKeyword;
import com.lambdaworks.redis.protocol.CommandType;

/**
 * Argument list builder for the new redis MIGRATE command. Static import
 * the methods from {@link Builder} and chain the method calls: {@code ex(10).nx()}.
 *
 * @author Mark Paluch
 */
public class MigrateArgs implements CompositeArgument {

    private boolean copy = false;
    private boolean replace = false;
    List keys = new ArrayList<>();

    public static class Builder {

        /**
         * Utility constructor.
         */
        private Builder() {

        }

        public static  MigrateArgs copy() {
            return new MigrateArgs().copy();
        }

        public static  MigrateArgs replace() {
            return new MigrateArgs().replace();
        }

        public static  MigrateArgs key(K key) {
            return new MigrateArgs().key(key);
        }

        @SafeVarargs
        public static  MigrateArgs keys(K... keys) {
            return new MigrateArgs().keys(keys);
        }

        public static  MigrateArgs keys(Iterable keys) {
            return new MigrateArgs().keys(keys);
        }
    }

    public MigrateArgs copy() {
        this.copy = true;
        return this;
    }

    public MigrateArgs replace() {
        this.replace = true;
        return this;
    }

    public MigrateArgs key(K key) {
        LettuceAssert.notNull(key, "Key must not be null");
        this.keys.add(key);
        return this;
    }

    @SafeVarargs
    public final MigrateArgs keys(K... keys) {
        LettuceAssert.notEmpty(keys, "Keys must not be empty");
        for (K key : keys) {
            this.keys.add(key);
        }
        return this;
    }

    public MigrateArgs keys(Iterable keys) {
        LettuceAssert.notNull(keys, "Keys must not be null");
        Iterator iterator = keys.iterator();
        while (iterator.hasNext()) {
            this.keys.add(iterator.next());
        }
        return this;
    }

    @SuppressWarnings("unchecked")
    public  void build(CommandArgs args) {

        if (copy) {
            args.add(CommandKeyword.COPY);
        }

        if (replace) {
            args.add(CommandKeyword.REPLACE);
        }

        if (keys.size() > 1) {
            args.add(CommandType.KEYS);
            args.addKeys((Iterable) keys);
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy