|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 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 | + * http://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 com.datastax.oss.driver.api.core.config; |
| 17 | + |
| 18 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +/** |
| 24 | + * A builder that allows providing typed configuration tied to {@link DriverOption}. |
| 25 | + * |
| 26 | + * <p>Rarely would one ever deal with this type directly. This interface is useful for defining |
| 27 | + * default methods so implementors only have to implement a small set of methods. |
| 28 | + */ |
| 29 | +public interface ProgrammaticBuilder<SelfT extends ProgrammaticBuilder> { |
| 30 | + |
| 31 | + @NonNull |
| 32 | + default SelfT withBoolean(@NonNull DriverOption option, boolean value) { |
| 33 | + return with(option, value); |
| 34 | + } |
| 35 | + |
| 36 | + @NonNull |
| 37 | + default SelfT withBooleanList(@NonNull DriverOption option, @NonNull List<Boolean> value) { |
| 38 | + return with(option, value); |
| 39 | + } |
| 40 | + |
| 41 | + @NonNull |
| 42 | + default SelfT withInt(@NonNull DriverOption option, int value) { |
| 43 | + return with(option, value); |
| 44 | + } |
| 45 | + |
| 46 | + @NonNull |
| 47 | + default SelfT withIntList(@NonNull DriverOption option, @NonNull List<Integer> value) { |
| 48 | + return with(option, value); |
| 49 | + } |
| 50 | + |
| 51 | + @NonNull |
| 52 | + default SelfT withLong(@NonNull DriverOption option, long value) { |
| 53 | + return with(option, value); |
| 54 | + } |
| 55 | + |
| 56 | + @NonNull |
| 57 | + default SelfT withLongList(@NonNull DriverOption option, @NonNull List<Long> value) { |
| 58 | + return with(option, value); |
| 59 | + } |
| 60 | + |
| 61 | + @NonNull |
| 62 | + default SelfT withDouble(@NonNull DriverOption option, double value) { |
| 63 | + return with(option, value); |
| 64 | + } |
| 65 | + |
| 66 | + @NonNull |
| 67 | + default SelfT withDoubleList(@NonNull DriverOption option, @NonNull List<Double> value) { |
| 68 | + return with(option, value); |
| 69 | + } |
| 70 | + |
| 71 | + @NonNull |
| 72 | + default SelfT withString(@NonNull DriverOption option, @NonNull String value) { |
| 73 | + return with(option, value); |
| 74 | + } |
| 75 | + |
| 76 | + @NonNull |
| 77 | + default SelfT withStringList(@NonNull DriverOption option, @NonNull List<String> value) { |
| 78 | + return with(option, value); |
| 79 | + } |
| 80 | + |
| 81 | + @SuppressWarnings("unchecked") |
| 82 | + @NonNull |
| 83 | + default SelfT withStringMap(@NonNull DriverOption option, @NonNull Map<String, String> value) { |
| 84 | + SelfT v = (SelfT) this; |
| 85 | + for (String key : value.keySet()) { |
| 86 | + v = (SelfT) v.with(option.getPath() + "." + key, value.get(key)); |
| 87 | + } |
| 88 | + return v; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Specifies a size in bytes. This is separate from {@link #withLong(DriverOption, long)}, in case |
| 93 | + * implementations want to allow users to provide sizes in a more human-readable way, for example |
| 94 | + * "256 MB". |
| 95 | + */ |
| 96 | + @NonNull |
| 97 | + default SelfT withBytes(@NonNull DriverOption option, @NonNull String value) { |
| 98 | + return with(option, value); |
| 99 | + } |
| 100 | + |
| 101 | + @NonNull |
| 102 | + default SelfT withBytesList(@NonNull DriverOption option, @NonNull List<Long> value) { |
| 103 | + return with(option, value); |
| 104 | + } |
| 105 | + |
| 106 | + @NonNull |
| 107 | + default SelfT withDuration(@NonNull DriverOption option, @NonNull Duration value) { |
| 108 | + return with(option, value); |
| 109 | + } |
| 110 | + |
| 111 | + @NonNull |
| 112 | + default SelfT withDurationList(@NonNull DriverOption option, @NonNull List<Duration> value) { |
| 113 | + return with(option, value); |
| 114 | + } |
| 115 | + |
| 116 | + @NonNull |
| 117 | + default SelfT withClass(@NonNull DriverOption option, @NonNull Class<?> value) { |
| 118 | + return with(option, value.getName()); |
| 119 | + } |
| 120 | + |
| 121 | + /** Unsets an option. */ |
| 122 | + @NonNull |
| 123 | + default SelfT without(@NonNull DriverOption option) { |
| 124 | + return with(option, null); |
| 125 | + } |
| 126 | + |
| 127 | + @NonNull |
| 128 | + default SelfT with(@NonNull DriverOption option, @NonNull Object value) { |
| 129 | + return with(option.getPath(), value); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Provides a simple path to value mapping, all default methods invoke this method directly. It is |
| 134 | + * not recommended that it is used directly other than by these defaults. |
| 135 | + */ |
| 136 | + @NonNull |
| 137 | + SelfT with(@NonNull String path, @NonNull Object value); |
| 138 | +} |
0 commit comments