1313 */
1414package com .google .cloud .bigtable .admin .v2 .models ;
1515
16+ import java .util .ArrayList ;
17+ import java .util .List ;
1618import java .util .concurrent .TimeUnit ;
1719import javax .annotation .Nonnull ;
1820import org .threeten .bp .Duration ;
21+ import com .google .api .core .BetaApi ;
22+ import com .google .api .core .InternalApi ;
1923import com .google .bigtable .admin .v2 .GcRule ;
24+ import com .google .bigtable .admin .v2 .GcRule .Intersection ;
25+ import com .google .bigtable .admin .v2 .GcRule .Union ;
26+ import com .google .common .base .MoreObjects ;
2027
28+ /**
29+ * Wraps {@link GcRule} protocol buffer object and exposes a simpler Fluent DSL model
30+ */
31+ @ BetaApi
2132public final class GCRules {
33+ /** Factory method to create GCRules - entry point into the DSL. */
2234 public static final GCRules GCRULES = new GCRules ();
2335
2436 private GCRules () {}
2537
38+ /**
39+ * Creates a new instance of the IntersectionRule
40+ *
41+ * @return IntersectionRule
42+ */
2643 public IntersectionRule intersection () {
2744 return new IntersectionRule ();
2845 }
2946
47+ /**
48+ * Creates a new instance of the IntersectionRule
49+ *
50+ * @return UnionRule
51+ */
3052 public UnionRule union () {
3153 return new UnionRule ();
3254 }
3355
56+ /**
57+ * Creates a new instance of the VersionRule
58+ *
59+ * @param maxVersion - maximum number of cell versions to keep
60+ * @return VersionRule
61+ */
3462 public VersionRule maxVersions (int maxVersion ) {
3563 return new VersionRule (maxVersion );
3664 }
3765
66+ /**
67+ * Creates a new instance of the DurationRule
68+ *
69+ * @param maxAge - maximum age of the cell to keep
70+ * @param timeUnit - timeunit for the age
71+ * @return DurationRule
72+ */
3873 public DurationRule maxAge (long maxAge , TimeUnit timeUnit ) {
3974 Duration duration = Duration .ZERO ;
4075 TimeUnit .SECONDS .convert (maxAge , timeUnit );
4176 return maxAge (duration );
4277 }
4378
79+ /**
80+ * Creates a new instance of the DurationRule
81+ *
82+ * @param duration - age expressed as duration
83+ * @return DurationRule
84+ */
4485 public DurationRule maxAge (Duration duration ) {
4586 return new DurationRule (duration );
4687 }
4788
89+ /**
90+ * Creates an empty default rule
91+ *
92+ * @return DefaultRule
93+ */
4894 public DefaultRule defaulRule () {
4995 return new DefaultRule ();
5096 }
5197
52- public static final class IntersectionRule implements GCRule {
98+ /**
99+ * Fluent wrapper for {@link Intersection} rule.
100+ * Allows far adding an hierarchy of rules with intersection as the root
101+ */
102+ public static final class IntersectionRule extends BaseRule {
53103 private GcRule .Intersection .Builder builder ;
54-
104+ private List <GCRule > rulesList = new ArrayList <>();
105+
55106 private IntersectionRule () {
56107 this .builder = GcRule .Intersection .newBuilder ();
57108 }
58109
110+ /**
111+ * Adds a new GCRule
112+ *
113+ * @param rule
114+ * @return IntersectionRule
115+ */
59116 public IntersectionRule rule (@ Nonnull GCRule rule ) {
117+ rulesList .add (rule );
60118 builder .addRules (rule .toProto ());
61119 return this ;
62120 }
63121
122+ /**
123+ * Gets the list of child rules
124+ *
125+ * @return List<GCRule>
126+ */
127+ public List <GCRule > getRulesList () {
128+ return rulesList ;
129+ }
130+
131+ @ Override
132+ public String toString () {
133+ return MoreObjects .toStringHelper (this )
134+ .add ("rulesList" , rulesList )
135+ .toString ();
136+ }
137+
138+ @ InternalApi
64139 @ Override
65140 public GcRule toProto () {
66141 switch (builder .getRulesCount ()) {
@@ -74,18 +149,47 @@ public GcRule toProto() {
74149 }
75150 }
76151
77- public static final class UnionRule implements GCRule {
152+ /**
153+ * Fluent wrapper for {@link Union} rule.
154+ * Allows far adding an hierarchy of rules with union as the root
155+ */
156+ public static final class UnionRule extends BaseRule {
78157 private GcRule .Union .Builder builder ;
79-
158+ private List <GCRule > rulesList = new ArrayList <>();
159+
80160 private UnionRule () {
81161 this .builder = GcRule .Union .newBuilder ();
82162 }
83163
164+ /**
165+ * Adds a new GCRule
166+ *
167+ * @param rule
168+ * @return UnionRule
169+ */
84170 public UnionRule rule (@ Nonnull GCRule rule ) {
171+ rulesList .add (rule );
85172 builder .addRules (rule .toProto ());
86173 return this ;
87174 }
88175
176+ /**
177+ * Gets the list of child rules
178+ *
179+ * @return List<GCRule>
180+ */
181+ public List <GCRule > getRulesList () {
182+ return rulesList ;
183+ }
184+
185+ @ Override
186+ public String toString () {
187+ return MoreObjects .toStringHelper (this )
188+ .add ("rulesList" , rulesList )
189+ .toString ();
190+ }
191+
192+ @ InternalApi
89193 @ Override
90194 public GcRule toProto () {
91195 switch (builder .getRulesCount ()) {
@@ -99,21 +203,42 @@ public GcRule toProto() {
99203 }
100204 }
101205
102- public static final class VersionRule implements GCRule {
206+ /**
207+ * Wrapper for building max versions rule
208+ */
209+ public static final class VersionRule extends BaseRule {
103210 private GcRule .Builder builder ;
104211
105212 private VersionRule (int maxVersion ) {
106213 this .builder = GcRule .newBuilder ();
107214 builder .setMaxNumVersions (maxVersion );
108215 }
109216
217+ /**
218+ * Gets the configured maximum versions
219+ */
220+ public int getMaxVersions () {
221+ return toProto ().getMaxNumVersions ();
222+ }
223+
224+ @ Override
225+ public String toString () {
226+ return MoreObjects .toStringHelper (this )
227+ .add ("maxNumVersions" , getMaxVersions ())
228+ .toString ();
229+ }
230+
231+ @ InternalApi
110232 @ Override
111233 public GcRule toProto () {
112234 return builder .build ();
113235 }
114236 }
115237
116- public static final class DurationRule implements GCRule {
238+ /**
239+ * Wrapper for building max duration rule
240+ */
241+ public static final class DurationRule extends BaseRule {
117242 private com .google .protobuf .Duration .Builder builder ;
118243
119244 private DurationRule (Duration duration ) {
@@ -122,23 +247,102 @@ private DurationRule(Duration duration) {
122247 .setSeconds (duration .getSeconds ())
123248 .setNanos (duration .getNano ());
124249 }
250+
251+ /**
252+ * Gets the configured maximum age
253+ *
254+ * @return Duration
255+ */
256+ public Duration getMaxAge () {
257+ long seconds = toProto ().getMaxAge ().getSeconds ();
258+ int nanos = toProto ().getMaxAge ().getNanos ();
259+ return Duration .ofSeconds (seconds , nanos );
260+ }
261+
262+ @ Override
263+ public String toString () {
264+ return MoreObjects .toStringHelper (this )
265+ .add ("maxAge" , getMaxAge ())
266+ .toString ();
267+ }
125268
269+ @ InternalApi
126270 @ Override
127271 public GcRule toProto () {
128272 return GcRule .newBuilder ().setMaxAge (builder .build ()).build ();
129273 }
130274 }
131275
132- public static final class DefaultRule implements GCRule {
276+ /**
277+ * Wrapper for building a empty rule
278+ *
279+ */
280+ public static final class DefaultRule extends BaseRule {
133281 private DefaultRule () {}
134282
283+ @ InternalApi
135284 @ Override
136285 public GcRule toProto () {
137286 return GcRule .getDefaultInstance ();
138287 }
288+
289+ @ Override
290+ public String toString () {
291+ return MoreObjects .toStringHelper (this )
292+ .toString ();
293+ }
139294 }
140295
141- interface GCRule {
296+ /**
297+ * Helpers to support casting rules the Type, when the Type is known
298+ */
299+ public static abstract class BaseRule implements GCRule {
300+
301+ /**
302+ * Casts the rule to DurationRule.
303+ * On failure throws a ClassCastException
304+ */
305+ public DurationRule getDurationOrThow () {
306+ return (DurationRule ) this ;
307+ }
308+
309+ /**
310+ * Casts the rule to VersionRule.
311+ * On failure throws a ClassCastException
312+ */
313+ public VersionRule getVersionOrThow () {
314+ return (VersionRule ) this ;
315+ }
316+
317+ /**
318+ * Casts the rule to UnionRule.
319+ * On failure throws a ClassCastException
320+ */
321+ public UnionRule getUnionOrThow () {
322+ return (UnionRule ) this ;
323+ }
324+
325+ /**
326+ * Casts the rule to IntersectionRule.
327+ * On failure throws a ClassCastException
328+ */
329+ public IntersectionRule getIntersectionOrThow () {
330+ return (IntersectionRule ) this ;
331+ }
332+ }
333+
334+ /**
335+ *
336+ * interface for fluent GcRule wrappers
337+ *
338+ */
339+ public interface GCRule {
340+ DurationRule getDurationOrThow ();
341+ VersionRule getVersionOrThow ();
342+ UnionRule getUnionOrThow ();
343+ IntersectionRule getIntersectionOrThow ();
344+
345+ @ InternalApi
142346 GcRule toProto ();
143347 }
144348}
0 commit comments