Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 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 org.onosproject.store.primitives; |
| 17 | |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 18 | import java.util.concurrent.Executor; |
| 19 | import java.util.function.Supplier; |
| 20 | |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.store.service.DistributedPrimitive; |
| 23 | import org.onosproject.store.service.Serializer; |
| 24 | |
| 25 | /** |
| 26 | * Abstract builder for distributed primitives. |
| 27 | * |
| 28 | * @param <T> distributed primitive type |
| 29 | */ |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 30 | public abstract class DistributedPrimitiveBuilder<B extends DistributedPrimitiveBuilder<B, T>, |
| 31 | T extends DistributedPrimitive> { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 32 | |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 33 | private final DistributedPrimitive.Type type; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 34 | private String name; |
| 35 | private ApplicationId applicationId; |
| 36 | private Serializer serializer; |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 37 | private Supplier<Executor> executorSupplier; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 38 | private boolean partitionsDisabled = false; |
| 39 | private boolean meteringDisabled = false; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 40 | private boolean readOnly = false; |
| 41 | private boolean relaxedReadConsistency = false; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 42 | |
| 43 | public DistributedPrimitiveBuilder(DistributedPrimitive.Type type) { |
| 44 | this.type = type; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Sets the primitive name. |
| 49 | * |
| 50 | * @param name primitive name |
| 51 | * @return this builder |
| 52 | */ |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 53 | public B withName(String name) { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 54 | this.name = name; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 55 | return (B) this; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Sets the serializer to use for transcoding info held in the primitive. |
| 60 | * |
| 61 | * @param serializer serializer |
| 62 | * @return this builder |
| 63 | */ |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 64 | public B withSerializer(Serializer serializer) { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 65 | this.serializer = serializer; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 66 | return (B) this; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 70 | * Sets the executor to use for asynchronous callbacks. |
| 71 | * <p> |
| 72 | * For partitioned primitives, the provided executor will be shared across all partitions. |
| 73 | * |
| 74 | * @param executor the executor to use for asynchronous callbacks |
| 75 | * @return this builder |
| 76 | */ |
| 77 | public B withExecutor(Executor executor) { |
| 78 | return withExecutorSupplier(() -> executor); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Sets the supplier to be used to create executors. |
| 83 | * <p> |
| 84 | * When a factory is set, the supplier will be used to create a separate executor for each partition. |
| 85 | * |
| 86 | * @param executorSupplier the executor supplier |
| 87 | * @return this builder |
| 88 | */ |
| 89 | public B withExecutorSupplier(Supplier<Executor> executorSupplier) { |
| 90 | this.executorSupplier = executorSupplier; |
| 91 | return (B) this; |
| 92 | } |
| 93 | |
| 94 | /** |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 95 | * Sets the application id that owns this primitive. |
| 96 | * |
| 97 | * @param applicationId application identifier |
| 98 | * @return this builder |
| 99 | */ |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 100 | public B withApplicationId(ApplicationId applicationId) { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 101 | this.applicationId = applicationId; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 102 | return (B) this; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 106 | * Disables recording usage stats for this primitive. |
| 107 | * @deprecated usage of this method is discouraged for most common scenarios. |
| 108 | * @return this builder |
| 109 | */ |
| 110 | @Deprecated |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 111 | public B withMeteringDisabled() { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 112 | this.meteringDisabled = true; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 113 | return (B) this; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Disables state changing operations on the returned distributed primitive. |
| 118 | * @return this builder |
| 119 | */ |
| 120 | public B withUpdatesDisabled() { |
| 121 | this.readOnly = true; |
| 122 | return (B) this; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Turns on relaxed consistency for read operations. |
| 127 | * @return this builder |
| 128 | */ |
| 129 | public B withRelaxedReadConsistency() { |
| 130 | this.relaxedReadConsistency = true; |
| 131 | return (B) this; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns if metering is enabled. |
| 136 | * |
| 137 | * @return {@code true} if yes; {@code false} otherwise |
| 138 | */ |
| 139 | public final boolean meteringEnabled() { |
| 140 | return !meteringDisabled; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns if partitions are disabled. |
| 145 | * |
| 146 | * @return {@code true} if yes; {@code false} otherwise |
| 147 | */ |
| 148 | public final boolean partitionsDisabled() { |
| 149 | return partitionsDisabled; |
| 150 | } |
| 151 | |
| 152 | /** |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 153 | * Returns if updates are disabled. |
| 154 | * |
| 155 | * @return {@code true} if yes; {@code false} otherwise |
| 156 | */ |
| 157 | public final boolean readOnly() { |
| 158 | return readOnly; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns if consistency is relaxed for read operations. |
| 163 | * |
| 164 | * @return {@code true} if yes; {@code false} otherwise |
| 165 | */ |
| 166 | public final boolean relaxedReadConsistency() { |
| 167 | return relaxedReadConsistency; |
| 168 | } |
| 169 | |
| 170 | /** |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 171 | * Returns the serializer. |
| 172 | * |
| 173 | * @return serializer |
| 174 | */ |
| 175 | public final Serializer serializer() { |
| 176 | return serializer; |
| 177 | } |
| 178 | |
| 179 | /** |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 180 | * Returns the executor supplier. |
| 181 | * |
| 182 | * @return executor supplier |
| 183 | */ |
| 184 | public final Supplier<Executor> executorSupplier() { |
| 185 | return executorSupplier; |
| 186 | } |
| 187 | |
| 188 | /** |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 189 | * Returns the application identifier. |
| 190 | * |
| 191 | * @return application id |
| 192 | */ |
| 193 | public final ApplicationId applicationId() { |
| 194 | return applicationId; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Returns the name of the primitive. |
| 199 | * |
| 200 | * @return primitive name |
| 201 | */ |
| 202 | public final String name() { |
| 203 | return name; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Returns the primitive type. |
| 208 | * |
| 209 | * @return primitive type |
| 210 | */ |
| 211 | public final DistributedPrimitive.Type type() { |
| 212 | return type; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Constructs an instance of the distributed primitive. |
| 217 | * @return distributed primitive |
| 218 | */ |
| 219 | public abstract T build(); |
| 220 | } |