/*
 * Copyright 2011-2020 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
 *
 *      https://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 io.lettuce.core;

import static io.lettuce.core.protocol.CommandKeyword.COUNT;
import static io.lettuce.core.protocol.CommandKeyword.MATCH;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.internal.LettuceAssert;
import io.lettuce.core.protocol.CommandArgs;

/**
 * Argument list builder for the Redis scan commands ({@literal SCAN, HSCAN, SSCAN, ZSCAN}). Static import the methods from
 *
 * {@link Builder} and chain the method calls: {@code matches("weight_*").limit(0, 2)}.
 * <p>
 * {@link ScanArgs} is a mutable object and instances should be used only once to avoid shared mutable state.
 *
 * @author Mark Paluch
 * @since 3.0
 */
public class ScanArgs implements CompositeArgument {

    private Long count;
    private String match;

    /**
     * Builder entry points for {@link ScanArgs}.
     */
    public static class Builder {

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

        /**
         * Creates new {@link ScanArgs} with {@literal LIMIT} set.
         *
         * @param count number of elements to scan
         * @return new {@link ScanArgs} with {@literal LIMIT} set.
         * @see ScanArgs#limit(long)
         */
        public static ScanArgs limit(long count) {
            return new ScanArgs().limit(count);
        }

        /**
         * Creates new {@link ScanArgs} with {@literal MATCH} set.
         *
         * @param matches the filter.
         * @return new {@link ScanArgs} with {@literal MATCH} set.
         * @see ScanArgs#match(String)
         */
        public static ScanArgs matches(String matches) {
            return new ScanArgs().match(matches);
        }
    }

    /**
     * Set the match filter.
     *
     * @param match the filter, must not be {@literal null}.
     * @return {@literal this} {@link ScanArgs}.
     */
    public ScanArgs match(String match) {

        LettuceAssert.notNull(match, "Match must not be null");

        this.match = match;
        return this;
    }

    /**
     * Limit the scan by count
     *
     * @param count number of elements to scan
     * @return {@literal this} {@link ScanArgs}.
     */
    public ScanArgs limit(long count) {

        this.count = count;
        return this;
    }

    public <K, V> void build(CommandArgs<K, V> args) {

        if (match != null) {
            args.add(MATCH).add(match.getBytes(StandardCharsets.UTF_8));
        }

        if (count != null) {
            args.add(COUNT).add(count);
        }
    }
}

@@ -792,16 +792,49 @@
 Buffer;%0A
+import java.nio.charset.Charset;%0A
 import j
@@ -867,16 +867,16 @@
 arsets;%0A
-
 %0Aimport 
@@ -1486,16 +1486,45 @@
 g match;
+%0A    private Charset charset;
 %0A%0A    /*
@@ -2768,32 +2768,548 @@
 rn this;%0A    %7D%0A%0A
+    /**%0A     * Set the match filter.%0A     *%0A     * @param match the filter, must not be %7B@literal null%7D.%0A     * @param charset the charset for match, must not be %7B@literal null%7D.%0A     * @return %7B@literal this%7D %7B@link ScanArgs%7D.%0A     */%0A    public ScanArgs match(String match, Charset charset) %7B%0A%0A        LettuceAssert.notNull(match, %22Match must not be null%22);%0A        LettuceAssert.notNull(charset, %22Charset must not be null%22);%0A        this.match = match;%0A        this.charset = charset;%0A        return this;%0A    %7D%0A%0A
     /**%0A     * L
@@ -3616,32 +3616,68 @@
 atch != null) %7B%0A
+            if(charset==null) %7B%0A    
             args
@@ -3733,16 +3733,113 @@
 TF_8));%0A
+            %7D else %7B%0A                args.add(MATCH).add(match.getBytes(charset));%0A            %7D%0A
         

Add charset option to ScanArgs.match(…) #1285