1616package com .google .cloud .bigtable .data .v2 .wrappers ;
1717
1818import com .google .api .core .InternalApi ;
19+ import com .google .api .core .InternalExtensionOnly ;
1920import com .google .bigtable .v2 .ColumnRange ;
2021import com .google .bigtable .v2 .RowFilter ;
21- import com .google .bigtable .v2 .RowFilter .Condition ;
2222import com .google .bigtable .v2 .ValueRange ;
2323import com .google .cloud .bigtable .data .v2 .internal .RegexUtil ;
2424import com .google .cloud .bigtable .data .v2 .wrappers .Range .AbstractByteStringRange ;
2525import com .google .cloud .bigtable .data .v2 .wrappers .Range .AbstractTimestampRange ;
2626import com .google .common .base .Preconditions ;
2727import com .google .protobuf .ByteString ;
2828import javax .annotation .Nonnull ;
29- import javax .annotation .Nullable ;
3029
3130/**
3231 * A Fluent DSL to create a hierarchy of filters for the CheckAndMutateRow RPCs and ReadRows Query.
@@ -151,6 +150,15 @@ public LimitFilter limit() {
151150 }
152151
153152 // Miscellaneous filters without a clear target.
153+ /**
154+ * Wraps protobuf representation of a filter.
155+ *
156+ * <p>For advanced use only.
157+ */
158+ public Filter fromProto (RowFilter rowFilter ) {
159+ return new SimpleFilter (rowFilter );
160+ }
161+
154162 /** Matches all cells, regardless of input. Functionally equivalent to having no filter. */
155163 public Filter pass () {
156164 return PASS ;
@@ -207,9 +215,12 @@ public ChainFilter filter(@Nonnull Filter filter) {
207215 @ InternalApi
208216 @ Override
209217 public RowFilter toProto () {
210- if (builder .getFiltersCount () == 1 ) {
218+ switch (builder .getFiltersCount ()) {
219+ case 0 :
220+ return PASS .toProto ();
221+ case 1 :
211222 return builder .getFilters (0 );
212- } else {
223+ default :
213224 return RowFilter .newBuilder ().setChain (builder .build ()).build ();
214225 }
215226 }
@@ -245,9 +256,12 @@ public InterleaveFilter filter(@Nonnull Filter filter) {
245256 @ InternalApi
246257 @ Override
247258 public RowFilter toProto () {
248- if (builder .getFiltersCount () == 1 ) {
259+ switch (builder .getFiltersCount ()) {
260+ case 0 :
261+ return PASS .toProto ();
262+ case 1 :
249263 return builder .getFilters (0 );
250- } else {
264+ default :
251265 return RowFilter .newBuilder ().setInterleave (builder .build ()).build ();
252266 }
253267 }
@@ -270,7 +284,7 @@ public static final class ConditionFilter implements Filter {
270284
271285 private ConditionFilter (@ Nonnull Filter predicate ) {
272286 Preconditions .checkNotNull (predicate );
273- builder = Condition .newBuilder ().setPredicateFilter (predicate .toProto ());
287+ builder = RowFilter . Condition .newBuilder ().setPredicateFilter (predicate .toProto ());
274288 }
275289
276290 /** Sets (replaces) the filter to apply when the predicate is true. */
@@ -321,7 +335,7 @@ private KeyFilter() {}
321335 */
322336 public Filter regex (@ Nonnull String regex ) {
323337 Preconditions .checkNotNull (regex );
324- return regex (wrapString (regex ));
338+ return regex (ByteString . copyFromUtf8 (regex ));
325339 }
326340
327341 /**
@@ -392,7 +406,8 @@ private QualifierFilter() {}
392406 * be present in a binary qualifier.
393407 */
394408 public Filter regex (@ Nonnull String regex ) {
395- return regex (wrapString (regex ));
409+ Preconditions .checkNotNull (regex );
410+ return regex (ByteString .copyFromUtf8 (regex ));
396411 }
397412
398413 /**
@@ -410,6 +425,7 @@ public Filter regex(@Nonnull ByteString regex) {
410425
411426 /** Matches only cells from columns whose qualifiers equal the value. */
412427 public Filter exactMatch (@ Nonnull ByteString value ) {
428+ Preconditions .checkNotNull (value );
413429 return regex (RegexUtil .literalRegex (value ));
414430 }
415431
@@ -431,7 +447,6 @@ public static final class QualifierRangeFilter
431447 private final String family ;
432448
433449 private QualifierRangeFilter (String family ) {
434- super ();
435450 this .family = family ;
436451 }
437452
@@ -487,15 +502,10 @@ public TimestampRangeFilter range() {
487502 }
488503 }
489504
490- /**
491- * Matches only cells with microsecond timestamps within the given range. Start is inclusive and
492- * end is exclusive.
493- */
505+ /** Matches only cells with microsecond timestamps within the given range. */
494506 public static final class TimestampRangeFilter
495507 extends AbstractTimestampRange <TimestampRangeFilter > implements Filter {
496- private TimestampRangeFilter () {
497- super ();
498- }
508+ private TimestampRangeFilter () {}
499509
500510 @ InternalApi
501511 @ Override
@@ -547,11 +557,13 @@ private ValueFilter() {}
547557 * in a binary value.
548558 */
549559 public Filter regex (@ Nonnull String regex ) {
550- return regex (wrapString (regex ));
560+ Preconditions .checkNotNull (regex );
561+ return regex (ByteString .copyFromUtf8 (regex ));
551562 }
552563
553564 /** Matches only cells with values that match the given value. */
554565 public Filter exactMatch (@ Nonnull ByteString value ) {
566+ Preconditions .checkNotNull (value );
555567 return regex (RegexUtil .literalRegex (value ));
556568 }
557569
@@ -586,9 +598,7 @@ public Filter strip() {
586598 /** Matches only cells with values that fall within the given value range. */
587599 public static final class ValueRangeFilter extends AbstractByteStringRange <ValueRangeFilter >
588600 implements Filter {
589- private ValueRangeFilter () {
590- super ();
591- }
601+ private ValueRangeFilter () {}
592602
593603 @ InternalApi
594604 @ Override
@@ -687,15 +697,9 @@ public SimpleFilter clone() {
687697 }
688698 }
689699
700+ @ InternalExtensionOnly
690701 public interface Filter extends Cloneable {
691702 @ InternalApi
692703 RowFilter toProto ();
693704 }
694-
695- private static ByteString wrapString (@ Nullable String value ) {
696- if (value == null ) {
697- return null ;
698- }
699- return ByteString .copyFromUtf8 (value );
700- }
701705}
0 commit comments