blob: a1221251d98d5250afb5f7ba87aee844315dccd3 [file] [log] [blame]
Madan Jampanie17d3282016-02-03 15:30:57 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampanie17d3282016-02-03 15:30:57 -08003 *
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.store.service.DistributedPrimitive;
20import org.onosproject.store.service.Serializer;
21
22/**
23 * Abstract builder for distributed primitives.
24 *
25 * @param <T> distributed primitive type
26 */
Madan Jampani538be742016-02-10 14:55:38 -080027public abstract class DistributedPrimitiveBuilder<B extends DistributedPrimitiveBuilder<B, T>,
28 T extends DistributedPrimitive> {
Madan Jampanie17d3282016-02-03 15:30:57 -080029
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 Jampani538be742016-02-10 14:55:38 -080036 private boolean readOnly = false;
37 private boolean relaxedReadConsistency = false;
Madan Jampanie17d3282016-02-03 15:30:57 -080038
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 Jampani538be742016-02-10 14:55:38 -080049 public B withName(String name) {
Madan Jampanie17d3282016-02-03 15:30:57 -080050 this.name = name;
Madan Jampani538be742016-02-10 14:55:38 -080051 return (B) this;
Madan Jampanie17d3282016-02-03 15:30:57 -080052 }
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 Jampani538be742016-02-10 14:55:38 -080060 public B withSerializer(Serializer serializer) {
Madan Jampanie17d3282016-02-03 15:30:57 -080061 this.serializer = serializer;
Madan Jampani538be742016-02-10 14:55:38 -080062 return (B) this;
Madan Jampanie17d3282016-02-03 15:30:57 -080063 }
64
65 /**
66 * Sets the application id that owns this primitive.
67 *
68 * @param applicationId application identifier
69 * @return this builder
70 */
Madan Jampani538be742016-02-10 14:55:38 -080071 public B withApplicationId(ApplicationId applicationId) {
Madan Jampanie17d3282016-02-03 15:30:57 -080072 this.applicationId = applicationId;
Madan Jampani538be742016-02-10 14:55:38 -080073 return (B) this;
Madan Jampanie17d3282016-02-03 15:30:57 -080074 }
75
76 /**
Madan Jampanie17d3282016-02-03 15:30:57 -080077 * 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 Jampani538be742016-02-10 14:55:38 -080082 public B withMeteringDisabled() {
Madan Jampanie17d3282016-02-03 15:30:57 -080083 this.meteringDisabled = true;
Madan Jampani538be742016-02-10 14:55:38 -080084 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 Jampanie17d3282016-02-03 15:30:57 -0800103 }
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 Jampani538be742016-02-10 14:55:38 -0800124 * 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 Jampanie17d3282016-02-03 15:30:57 -0800142 * 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}