blob: 224358e036ee0f10f18edbc619f70a074e366784 [file] [log] [blame]
Jordan Halterman2c045992018-03-20 21:33:00 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.store.primitives;
17
18import org.onosproject.core.ApplicationId;
19import org.onosproject.core.Version;
20import org.onosproject.store.service.DistributedPrimitive;
21import org.onosproject.store.service.Serializer;
22import org.onosproject.store.service.RevisionType;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * Abstract builder for distributed primitives.
28 *
29 * @param <O> distributed primitive options type
30 */
31public abstract class DistributedPrimitiveOptions<O extends DistributedPrimitiveOptions<O>> {
32
33 private final DistributedPrimitive.Type type;
34 private String name;
35 private ApplicationId applicationId;
36 private Serializer serializer;
37 private boolean partitionsDisabled = false;
38 private boolean meteringDisabled = false;
39 private boolean readOnly = false;
40 private boolean relaxedReadConsistency = false;
Jordan Halterman45008172018-03-19 16:40:31 -070041 private Version version;
42 private RevisionType revisionType;
Jordan Halterman2c045992018-03-20 21:33:00 -070043
44 public DistributedPrimitiveOptions(DistributedPrimitive.Type type) {
45 this.type = type;
46 }
47
48 /**
49 * Sets the primitive name.
50 *
51 * @param name primitive name
52 * @return this builder
53 */
54 public O withName(String name) {
55 this.name = name;
56 return (O) this;
57 }
58
59 /**
60 * Sets the serializer to use for transcoding info held in the primitive.
61 *
62 * @param serializer serializer
63 * @return this builder
64 */
65 public O withSerializer(Serializer serializer) {
66 this.serializer = serializer;
67 return (O) this;
68 }
69
70 /**
71 * Sets the application id that owns this primitive.
72 *
73 * @param applicationId application identifier
74 * @return this builder
75 */
76 public O withApplicationId(ApplicationId applicationId) {
77 this.applicationId = applicationId;
78 return (O) this;
79 }
80
81 /**
82 * Sets the primitive version.
83 *
84 * @param version the primitive version
85 * @return this builder
86 */
87 public O withVersion(Version version) {
Jordan Halterman45008172018-03-19 16:40:31 -070088 this.version = version;
Jordan Halterman2c045992018-03-20 21:33:00 -070089 return (O) this;
90 }
91
92 /**
93 * Sets the primitive revision type.
94 *
95 * @param revisionType the revision type
96 * @return this builder
97 */
98 public O withRevisionType(RevisionType revisionType) {
99 this.revisionType = checkNotNull(revisionType);
100 return (O) this;
101 }
102
103 /**
Jordan Halterman2c045992018-03-20 21:33:00 -0700104 * Disables state changing operations on the returned distributed primitive.
105 * @return this builder
106 */
107 public O withUpdatesDisabled() {
108 this.readOnly = true;
109 return (O) this;
110 }
111
112 /**
113 * Turns on relaxed consistency for read operations.
114 * @return this builder
115 */
116 public O withRelaxedReadConsistency() {
117 this.relaxedReadConsistency = true;
118 return (O) this;
119 }
120
121 /**
122 * Returns if metering is enabled.
123 *
124 * @return {@code true} if yes; {@code false} otherwise
125 */
126 public final boolean meteringEnabled() {
127 return !meteringDisabled;
128 }
129
130 /**
131 * Returns if partitions are disabled.
132 *
133 * @return {@code true} if yes; {@code false} otherwise
134 */
135 public final boolean partitionsDisabled() {
136 return partitionsDisabled;
137 }
138
139 /**
140 * Returns if updates are disabled.
141 *
142 * @return {@code true} if yes; {@code false} otherwise
143 */
144 public final boolean readOnly() {
145 return readOnly;
146 }
147
148 /**
149 * Returns if consistency is relaxed for read operations.
150 *
151 * @return {@code true} if yes; {@code false} otherwise
152 */
153 public final boolean relaxedReadConsistency() {
154 return relaxedReadConsistency;
155 }
156
157 /**
158 * Returns the serializer.
159 *
160 * @return serializer
161 */
162 public final Serializer serializer() {
163 return serializer;
164 }
165
166 /**
167 * Returns the application identifier.
168 *
169 * @return application id
170 */
171 public final ApplicationId applicationId() {
172 return applicationId;
173 }
174
175 /**
176 * Returns the name of the primitive.
177 *
178 * @return primitive name
179 */
180 public final String name() {
181 return name;
182 }
183
184 /**
185 * Returns the primitive type.
186 *
187 * @return primitive type
188 */
189 public final DistributedPrimitive.Type type() {
190 return type;
191 }
192
193 /**
Jordan Halterman45008172018-03-19 16:40:31 -0700194 * Returns the primitive version.
Jordan Halterman2c045992018-03-20 21:33:00 -0700195 *
Jordan Halterman45008172018-03-19 16:40:31 -0700196 * @return the primitive version
Jordan Halterman2c045992018-03-20 21:33:00 -0700197 */
Jordan Halterman45008172018-03-19 16:40:31 -0700198 public final Version version() {
199 return version;
Jordan Halterman2c045992018-03-20 21:33:00 -0700200 }
201
202 /**
203 * Returns the primitive revision type.
204 *
205 * @return the primitive revision type
206 */
207 public RevisionType revisionType() {
208 return revisionType;
209 }
210}