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 | |
| 18 | import org.onosproject.core.ApplicationId; |
| 19 | import org.onosproject.store.service.DistributedPrimitive; |
| 20 | import org.onosproject.store.service.Serializer; |
| 21 | |
| 22 | /** |
| 23 | * Abstract builder for distributed primitives. |
| 24 | * |
| 25 | * @param <T> distributed primitive type |
| 26 | */ |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 27 | public abstract class DistributedPrimitiveBuilder<B extends DistributedPrimitiveBuilder<B, T>, |
| 28 | T extends DistributedPrimitive> { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 29 | |
| 30 | private DistributedPrimitive.Type type; |
| 31 | private String name; |
| 32 | private ApplicationId applicationId; |
| 33 | private Serializer serializer; |
| 34 | private boolean partitionsDisabled = false; |
| 35 | private boolean meteringDisabled = false; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 36 | private boolean readOnly = false; |
| 37 | private boolean relaxedReadConsistency = false; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 38 | |
| 39 | public DistributedPrimitiveBuilder(DistributedPrimitive.Type type) { |
| 40 | this.type = type; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Sets the primitive name. |
| 45 | * |
| 46 | * @param name primitive name |
| 47 | * @return this builder |
| 48 | */ |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 49 | public B withName(String name) { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 50 | this.name = name; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 51 | return (B) this; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Sets the serializer to use for transcoding info held in the primitive. |
| 56 | * |
| 57 | * @param serializer serializer |
| 58 | * @return this builder |
| 59 | */ |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 60 | public B withSerializer(Serializer serializer) { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 61 | this.serializer = serializer; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 62 | return (B) this; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Sets the application id that owns this primitive. |
| 67 | * |
| 68 | * @param applicationId application identifier |
| 69 | * @return this builder |
| 70 | */ |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 71 | public B withApplicationId(ApplicationId applicationId) { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 72 | this.applicationId = applicationId; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 73 | return (B) this; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 77 | * Disables recording usage stats for this primitive. |
| 78 | * @deprecated usage of this method is discouraged for most common scenarios. |
| 79 | * @return this builder |
| 80 | */ |
| 81 | @Deprecated |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 82 | public B withMeteringDisabled() { |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 83 | this.meteringDisabled = true; |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 84 | return (B) this; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Disables state changing operations on the returned distributed primitive. |
| 89 | * @return this builder |
| 90 | */ |
| 91 | public B withUpdatesDisabled() { |
| 92 | this.readOnly = true; |
| 93 | return (B) this; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Turns on relaxed consistency for read operations. |
| 98 | * @return this builder |
| 99 | */ |
| 100 | public B withRelaxedReadConsistency() { |
| 101 | this.relaxedReadConsistency = true; |
| 102 | return (B) this; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns if metering is enabled. |
| 107 | * |
| 108 | * @return {@code true} if yes; {@code false} otherwise |
| 109 | */ |
| 110 | public final boolean meteringEnabled() { |
| 111 | return !meteringDisabled; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns if partitions are disabled. |
| 116 | * |
| 117 | * @return {@code true} if yes; {@code false} otherwise |
| 118 | */ |
| 119 | public final boolean partitionsDisabled() { |
| 120 | return partitionsDisabled; |
| 121 | } |
| 122 | |
| 123 | /** |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 124 | * Returns if updates are disabled. |
| 125 | * |
| 126 | * @return {@code true} if yes; {@code false} otherwise |
| 127 | */ |
| 128 | public final boolean readOnly() { |
| 129 | return readOnly; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns if consistency is relaxed for read operations. |
| 134 | * |
| 135 | * @return {@code true} if yes; {@code false} otherwise |
| 136 | */ |
| 137 | public final boolean relaxedReadConsistency() { |
| 138 | return relaxedReadConsistency; |
| 139 | } |
| 140 | |
| 141 | /** |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 142 | * Returns the serializer. |
| 143 | * |
| 144 | * @return serializer |
| 145 | */ |
| 146 | public final Serializer serializer() { |
| 147 | return serializer; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns the application identifier. |
| 152 | * |
| 153 | * @return application id |
| 154 | */ |
| 155 | public final ApplicationId applicationId() { |
| 156 | return applicationId; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns the name of the primitive. |
| 161 | * |
| 162 | * @return primitive name |
| 163 | */ |
| 164 | public final String name() { |
| 165 | return name; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns the primitive type. |
| 170 | * |
| 171 | * @return primitive type |
| 172 | */ |
| 173 | public final DistributedPrimitive.Type type() { |
| 174 | return type; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Constructs an instance of the distributed primitive. |
| 179 | * @return distributed primitive |
| 180 | */ |
| 181 | public abstract T build(); |
| 182 | } |