|
| 1 | +/* |
| 2 | + * Copyright 2014-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.redis.core; |
| 17 | + |
| 18 | +import java.util.StringJoiner; |
| 19 | + |
| 20 | +import org.springframework.lang.Nullable; |
| 21 | +import org.springframework.util.StringUtils; |
| 22 | + |
| 23 | +/** |
| 24 | + * Options to be used for with {@literal SCAN} commands. |
| 25 | + * |
| 26 | + * @author Mark Paluch |
| 27 | + * @since 2.6 |
| 28 | + */ |
| 29 | +public class KeyScanOptions extends ScanOptions { |
| 30 | + |
| 31 | + /** |
| 32 | + * Constant to apply default {@link KeyScanOptions} without setting a limit or matching a pattern. |
| 33 | + */ |
| 34 | + public static KeyScanOptions NONE = new KeyScanOptions(null, null, null, null); |
| 35 | + |
| 36 | + private final @Nullable String type; |
| 37 | + |
| 38 | + private KeyScanOptions(@Nullable Long count, @Nullable String pattern, @Nullable byte[] bytePattern, |
| 39 | + @Nullable String type) { |
| 40 | + |
| 41 | + super(count, pattern, bytePattern); |
| 42 | + this.type = type; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Static factory method that returns a new {@link KeyScanOptionsBuilder}. |
| 47 | + * |
| 48 | + * @return |
| 49 | + */ |
| 50 | + public static KeyScanOptionsBuilder scanOptions() { |
| 51 | + return new KeyScanOptionsBuilder(); |
| 52 | + } |
| 53 | + |
| 54 | + @Nullable |
| 55 | + public String getType() { |
| 56 | + return type; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String toOptionString() { |
| 61 | + |
| 62 | + if (this.equals(KeyScanOptions.NONE)) { |
| 63 | + return ""; |
| 64 | + } |
| 65 | + |
| 66 | + StringJoiner joiner = new StringJoiner(", ").add(super.toOptionString()); |
| 67 | + |
| 68 | + if (StringUtils.hasText(type)) { |
| 69 | + joiner.add("'type' '" + type + "'"); |
| 70 | + } |
| 71 | + |
| 72 | + return joiner.toString(); |
| 73 | + } |
| 74 | + |
| 75 | + public static class KeyScanOptionsBuilder extends ScanOptionsBuilder { |
| 76 | + |
| 77 | + private @Nullable String type; |
| 78 | + |
| 79 | + private KeyScanOptionsBuilder() {} |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns the current {@link KeyScanOptionsBuilder} configured with the given {@code count}. |
| 83 | + * |
| 84 | + * @param count |
| 85 | + * @return |
| 86 | + */ |
| 87 | + @Override |
| 88 | + public KeyScanOptionsBuilder count(long count) { |
| 89 | + super.count(count); |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns the current {@link KeyScanOptionsBuilder} configured with the given {@code pattern}. |
| 95 | + * |
| 96 | + * @param pattern |
| 97 | + * @return |
| 98 | + */ |
| 99 | + @Override |
| 100 | + public KeyScanOptionsBuilder match(String pattern) { |
| 101 | + super.match(pattern); |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Returns the current {@link KeyScanOptionsBuilder} configured with the given {@code pattern}. |
| 107 | + * |
| 108 | + * @param pattern |
| 109 | + * @return |
| 110 | + */ |
| 111 | + @Override |
| 112 | + public KeyScanOptionsBuilder match(byte[] pattern) { |
| 113 | + super.match(pattern); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Returns the current {@link KeyScanOptionsBuilder} configured with the given {@code type}. |
| 119 | + * |
| 120 | + * @param type |
| 121 | + * @return |
| 122 | + */ |
| 123 | + public KeyScanOptionsBuilder type(String type) { |
| 124 | + this.type = type; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Builds a new {@link KeyScanOptions} objects. |
| 130 | + * |
| 131 | + * @return a new {@link KeyScanOptions} objects. |
| 132 | + */ |
| 133 | + @Override |
| 134 | + public KeyScanOptions build() { |
| 135 | + return new KeyScanOptions(count, pattern, bytePattern, type); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments