aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--api/src/main/java/io/opencensus/internal/Utils.java86
-rw-r--r--api/src/main/java/io/opencensus/stats/Aggregation.java5
-rw-r--r--api/src/main/java/io/opencensus/stats/AggregationData.java10
-rw-r--r--api/src/main/java/io/opencensus/stats/BucketBoundaries.java8
-rw-r--r--api/src/main/java/io/opencensus/stats/Measure.java7
-rw-r--r--api/src/main/java/io/opencensus/stats/NoopStats.java18
-rw-r--r--api/src/main/java/io/opencensus/stats/View.java13
-rw-r--r--api/src/main/java/io/opencensus/stats/ViewData.java17
-rw-r--r--api/src/main/java/io/opencensus/tags/NoopTags.java22
-rw-r--r--api/src/main/java/io/opencensus/tags/TagKey.java5
-rw-r--r--api/src/main/java/io/opencensus/tags/TagValue.java4
-rw-r--r--api/src/main/java/io/opencensus/trace/Annotation.java5
-rw-r--r--api/src/main/java/io/opencensus/trace/AttributeValue.java9
-rw-r--r--api/src/main/java/io/opencensus/trace/MessageEvent.java5
-rw-r--r--api/src/main/java/io/opencensus/trace/NetworkEvent.java5
-rw-r--r--api/src/main/java/io/opencensus/trace/Span.java8
-rw-r--r--api/src/main/java/io/opencensus/trace/SpanBuilder.java5
-rw-r--r--api/src/main/java/io/opencensus/trace/SpanId.java15
-rw-r--r--api/src/main/java/io/opencensus/trace/Status.java5
-rw-r--r--api/src/main/java/io/opencensus/trace/TraceId.java15
-rw-r--r--api/src/main/java/io/opencensus/trace/TraceOptions.java15
-rw-r--r--api/src/main/java/io/opencensus/trace/Tracer.java5
-rw-r--r--api/src/main/java/io/opencensus/trace/config/TraceParams.java12
-rw-r--r--api/src/main/java/io/opencensus/trace/export/RunningSpanStore.java12
-rw-r--r--api/src/main/java/io/opencensus/trace/export/SampledSpanStore.java29
-rw-r--r--api/src/main/java/io/opencensus/trace/export/SpanData.java10
-rw-r--r--api/src/main/java/io/opencensus/trace/internal/BaseMessageEventUtils.java7
-rw-r--r--api/src/main/java/io/opencensus/trace/propagation/BinaryFormat.java7
-rw-r--r--api/src/main/java/io/opencensus/trace/propagation/TextFormat.java13
-rw-r--r--api/src/main/java/io/opencensus/trace/samplers/ProbabilitySampler.java5
-rw-r--r--api/src/test/java/io/opencensus/internal/UtilsTest.java88
-rw-r--r--findbugs-exclude.xml6
32 files changed, 314 insertions, 162 deletions
diff --git a/api/src/main/java/io/opencensus/internal/Utils.java b/api/src/main/java/io/opencensus/internal/Utils.java
new file mode 100644
index 00000000..85bf3785
--- /dev/null
+++ b/api/src/main/java/io/opencensus/internal/Utils.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.opencensus.internal;
+
+/*>>>
+import org.checkerframework.checker.nullness.qual.NonNull;
+*/
+
+/** General internal utility methods. */
+public final class Utils {
+
+ private Utils() {}
+
+ /**
+ * Throws an {@link IllegalArgumentException} if the argument is false. This method is similar to
+ * {@code Preconditions.checkArgument(boolean, Object)} from Guava.
+ *
+ * @param isValid whether the argument check passed.
+ * @param message the message to use for the exception.
+ */
+ public static void checkArgument(boolean isValid, String message) {
+ if (!isValid) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Throws an {@link IllegalStateException} if the argument is false. This method is similar to
+ * {@code Preconditions.checkState(boolean, Object)} from Guava.
+ *
+ * @param isValid whether the state check passed.
+ * @param message the message to use for the exception.
+ */
+ public static void checkState(boolean isValid, String message) {
+ if (!isValid) {
+ throw new IllegalStateException(message);
+ }
+ }
+
+ /**
+ * Validates an index in an array or other container. This method throws an {@link
+ * IllegalArgumentException} if the size is negative and throws an {@link
+ * IndexOutOfBoundsException} if the index is negative or greater than or equal to the size. This
+ * method is similar to {@code Preconditions.checkElementIndex(int, int)} from Guava.
+ *
+ * @param index the index to validate.
+ * @param size the size of the array or container.
+ */
+ public static void checkIndex(int index, int size) {
+ if (size < 0) {
+ throw new IllegalArgumentException("Negative size: " + size);
+ }
+ if (index < 0 || index >= size) {
+ throw new IndexOutOfBoundsException("Index out of bounds: size=" + size + ", index=" + index);
+ }
+ }
+
+ /**
+ * Throws a {@link NullPointerException} if the argument is null. This method is similar to {@code
+ * Preconditions.checkNotNull(Object, Object)} from Guava.
+ *
+ * @param arg the argument to check for null.
+ * @param message the message to use for the exception.
+ * @return the argument, if it passes the null check.
+ */
+ public static <T /*>>> extends @NonNull Object*/> T checkNotNull(T arg, String message) {
+ if (arg == null) {
+ throw new NullPointerException(message);
+ }
+ return arg;
+ }
+}
diff --git a/api/src/main/java/io/opencensus/stats/Aggregation.java b/api/src/main/java/io/opencensus/stats/Aggregation.java
index 55ce9d96..e8579c33 100644
--- a/api/src/main/java/io/opencensus/stats/Aggregation.java
+++ b/api/src/main/java/io/opencensus/stats/Aggregation.java
@@ -16,10 +16,9 @@
package io.opencensus.stats;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.auto.value.AutoValue;
import io.opencensus.common.Function;
+import io.opencensus.internal.Utils;
import javax.annotation.concurrent.Immutable;
/**
@@ -221,7 +220,7 @@ public abstract class Aggregation {
* @since 0.8
*/
public static Distribution create(BucketBoundaries bucketBoundaries) {
- checkNotNull(bucketBoundaries, "bucketBoundaries should not be null.");
+ Utils.checkNotNull(bucketBoundaries, "bucketBoundaries should not be null.");
return new AutoValue_Aggregation_Distribution(bucketBoundaries);
}
diff --git a/api/src/main/java/io/opencensus/stats/AggregationData.java b/api/src/main/java/io/opencensus/stats/AggregationData.java
index 637621e9..716e97d8 100644
--- a/api/src/main/java/io/opencensus/stats/AggregationData.java
+++ b/api/src/main/java/io/opencensus/stats/AggregationData.java
@@ -16,11 +16,9 @@
package io.opencensus.stats;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.auto.value.AutoValue;
import io.opencensus.common.Function;
+import io.opencensus.internal.Utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -329,13 +327,13 @@ public abstract class AggregationData {
double sumOfSquaredDeviations,
List<Long> bucketCounts) {
if (min != Double.POSITIVE_INFINITY || max != Double.NEGATIVE_INFINITY) {
- checkArgument(min <= max, "max should be greater or equal to min.");
+ Utils.checkArgument(min <= max, "max should be greater or equal to min.");
}
- checkNotNull(bucketCounts, "bucket counts should not be null.");
+ Utils.checkNotNull(bucketCounts, "bucket counts should not be null.");
List<Long> bucketCountsCopy = Collections.unmodifiableList(new ArrayList<Long>(bucketCounts));
for (Long bucket : bucketCountsCopy) {
- checkNotNull(bucket, "bucket should not be null.");
+ Utils.checkNotNull(bucket, "bucket should not be null.");
}
return new AutoValue_AggregationData_DistributionData(
diff --git a/api/src/main/java/io/opencensus/stats/BucketBoundaries.java b/api/src/main/java/io/opencensus/stats/BucketBoundaries.java
index 0dcb1710..20588d59 100644
--- a/api/src/main/java/io/opencensus/stats/BucketBoundaries.java
+++ b/api/src/main/java/io/opencensus/stats/BucketBoundaries.java
@@ -16,10 +16,8 @@
package io.opencensus.stats;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.auto.value.AutoValue;
+import io.opencensus.internal.Utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -42,14 +40,14 @@ public abstract class BucketBoundaries {
* @since 0.8
*/
public static final BucketBoundaries create(List<Double> bucketBoundaries) {
- checkNotNull(bucketBoundaries, "bucketBoundaries list should not be null.");
+ Utils.checkNotNull(bucketBoundaries, "bucketBoundaries list should not be null.");
List<Double> bucketBoundariesCopy = new ArrayList<Double>(bucketBoundaries); // Deep copy.
// Check if sorted.
if (bucketBoundariesCopy.size() > 1) {
double lower = bucketBoundariesCopy.get(0);
for (int i = 1; i < bucketBoundariesCopy.size(); i++) {
double next = bucketBoundariesCopy.get(i);
- checkArgument(lower < next, "Bucket boundaries not sorted.");
+ Utils.checkArgument(lower < next, "Bucket boundaries not sorted.");
lower = next;
}
}
diff --git a/api/src/main/java/io/opencensus/stats/Measure.java b/api/src/main/java/io/opencensus/stats/Measure.java
index 7b9b833e..ddd25763 100644
--- a/api/src/main/java/io/opencensus/stats/Measure.java
+++ b/api/src/main/java/io/opencensus/stats/Measure.java
@@ -16,12 +16,11 @@
package io.opencensus.stats;
-import static com.google.common.base.Preconditions.checkArgument;
-
import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import io.opencensus.common.Function;
import io.opencensus.internal.StringUtils;
+import io.opencensus.internal.Utils;
import javax.annotation.concurrent.Immutable;
/**
@@ -104,7 +103,7 @@ public abstract class Measure {
* @since 0.8
*/
public static MeasureDouble create(String name, String description, String unit) {
- checkArgument(
+ Utils.checkArgument(
StringUtils.isPrintableString(name) && name.length() <= NAME_MAX_LENGTH,
"Name should be a ASCII string with a length no greater than "
+ NAME_MAX_LENGTH
@@ -151,7 +150,7 @@ public abstract class Measure {
* @since 0.8
*/
public static MeasureLong create(String name, String description, String unit) {
- checkArgument(
+ Utils.checkArgument(
StringUtils.isPrintableString(name) && name.length() <= NAME_MAX_LENGTH,
"Name should be a ASCII string with a length no greater than "
+ NAME_MAX_LENGTH
diff --git a/api/src/main/java/io/opencensus/stats/NoopStats.java b/api/src/main/java/io/opencensus/stats/NoopStats.java
index 45fb6a17..02f7c521 100644
--- a/api/src/main/java/io/opencensus/stats/NoopStats.java
+++ b/api/src/main/java/io/opencensus/stats/NoopStats.java
@@ -16,13 +16,9 @@
package io.opencensus.stats;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Preconditions.checkState;
-
-import com.google.common.base.Preconditions;
import io.opencensus.common.Functions;
import io.opencensus.common.Timestamp;
+import io.opencensus.internal.Utils;
import io.opencensus.stats.Measure.MeasureDouble;
import io.opencensus.stats.Measure.MeasureLong;
import io.opencensus.tags.TagContext;
@@ -106,8 +102,8 @@ final class NoopStats {
@Override
@Deprecated
public void setState(StatsCollectionState state) {
- Preconditions.checkNotNull(state, "state");
- checkState(!isRead, "State was already read, cannot set state.");
+ Utils.checkNotNull(state, "state");
+ Utils.checkState(!isRead, "State was already read, cannot set state.");
}
}
@@ -140,7 +136,7 @@ final class NoopStats {
@Override
public void record(TagContext tags) {
- checkNotNull(tags, "tags");
+ Utils.checkNotNull(tags, "tags");
}
}
@@ -157,11 +153,11 @@ final class NoopStats {
@Override
public void registerView(View newView) {
- checkNotNull(newView, "newView");
+ Utils.checkNotNull(newView, "newView");
synchronized (registeredViews) {
exportedViews = null;
View existing = registeredViews.get(newView.getName());
- checkArgument(
+ Utils.checkArgument(
existing == null || newView.equals(existing),
"A different view with the same name already exists.");
if (existing == null) {
@@ -174,7 +170,7 @@ final class NoopStats {
@Nullable
@SuppressWarnings("deprecation")
public ViewData getView(View.Name name) {
- checkNotNull(name, "name");
+ Utils.checkNotNull(name, "name");
synchronized (registeredViews) {
View view = registeredViews.get(name);
if (view == null) {
diff --git a/api/src/main/java/io/opencensus/stats/View.java b/api/src/main/java/io/opencensus/stats/View.java
index e3d4540d..42179d58 100644
--- a/api/src/main/java/io/opencensus/stats/View.java
+++ b/api/src/main/java/io/opencensus/stats/View.java
@@ -16,13 +16,12 @@
package io.opencensus.stats;
-import static com.google.common.base.Preconditions.checkArgument;
-
import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import io.opencensus.common.Duration;
import io.opencensus.common.Function;
import io.opencensus.internal.StringUtils;
+import io.opencensus.internal.Utils;
import io.opencensus.tags.TagKey;
import java.util.ArrayList;
import java.util.Collections;
@@ -125,7 +124,8 @@ public abstract class View {
Aggregation aggregation,
List<TagKey> columns,
AggregationWindow window) {
- checkArgument(new HashSet<TagKey>(columns).size() == columns.size(), "Columns have duplicate.");
+ Utils.checkArgument(
+ new HashSet<TagKey>(columns).size() == columns.size(), "Columns have duplicate.");
List<TagKey> tagKeys = new ArrayList<TagKey>(columns);
Collections.sort(tagKeys, TAG_KEY_COMPARATOR);
@@ -151,7 +151,8 @@ public abstract class View {
Measure measure,
Aggregation aggregation,
List<TagKey> columns) {
- checkArgument(new HashSet<TagKey>(columns).size() == columns.size(), "Columns have duplicate.");
+ Utils.checkArgument(
+ new HashSet<TagKey>(columns).size() == columns.size(), "Columns have duplicate.");
return create(
name, description, measure, aggregation, columns, AggregationWindow.Cumulative.create());
}
@@ -187,7 +188,7 @@ public abstract class View {
* @since 0.8
*/
public static Name create(String name) {
- checkArgument(
+ Utils.checkArgument(
StringUtils.isPrintableString(name) && name.length() <= NAME_MAX_LENGTH,
"Name should be a ASCII string with a length no greater than 255 characters.");
return new AutoValue_View_Name(name);
@@ -289,7 +290,7 @@ public abstract class View {
* @since 0.8
*/
public static Interval create(Duration duration) {
- checkArgument(duration.compareTo(ZERO) > 0, "Duration must be positive");
+ Utils.checkArgument(duration.compareTo(ZERO) > 0, "Duration must be positive");
return new AutoValue_View_AggregationWindow_Interval(duration);
}
diff --git a/api/src/main/java/io/opencensus/stats/ViewData.java b/api/src/main/java/io/opencensus/stats/ViewData.java
index 788bea76..c511edab 100644
--- a/api/src/main/java/io/opencensus/stats/ViewData.java
+++ b/api/src/main/java/io/opencensus/stats/ViewData.java
@@ -16,13 +16,12 @@
package io.opencensus.stats;
-import static com.google.common.base.Preconditions.checkArgument;
-
import com.google.auto.value.AutoValue;
import io.opencensus.common.Duration;
import io.opencensus.common.Function;
import io.opencensus.common.Functions;
import io.opencensus.common.Timestamp;
+import io.opencensus.internal.Utils;
import io.opencensus.stats.Aggregation.Count;
import io.opencensus.stats.Aggregation.Distribution;
import io.opencensus.stats.Aggregation.Sum;
@@ -208,7 +207,7 @@ public abstract class ViewData {
new Function<View.AggregationWindow.Cumulative, Void>() {
@Override
public Void apply(View.AggregationWindow.Cumulative arg) {
- checkArgument(
+ Utils.checkArgument(
windowData instanceof AggregationWindowData.CumulativeData,
createErrorMessageForWindow(arg, windowData));
return null;
@@ -217,7 +216,7 @@ public abstract class ViewData {
new Function<View.AggregationWindow.Interval, Void>() {
@Override
public Void apply(View.AggregationWindow.Interval arg) {
- checkArgument(
+ Utils.checkArgument(
windowData instanceof AggregationWindowData.IntervalData,
createErrorMessageForWindow(arg, windowData));
return null;
@@ -245,7 +244,7 @@ public abstract class ViewData {
new Function<MeasureDouble, Void>() {
@Override
public Void apply(MeasureDouble arg) {
- checkArgument(
+ Utils.checkArgument(
aggregationData instanceof SumDataDouble,
createErrorMessageForAggregation(aggregation, aggregationData));
return null;
@@ -254,7 +253,7 @@ public abstract class ViewData {
new Function<MeasureLong, Void>() {
@Override
public Void apply(MeasureLong arg) {
- checkArgument(
+ Utils.checkArgument(
aggregationData instanceof SumDataLong,
createErrorMessageForAggregation(aggregation, aggregationData));
return null;
@@ -267,7 +266,7 @@ public abstract class ViewData {
new Function<Count, Void>() {
@Override
public Void apply(Count arg) {
- checkArgument(
+ Utils.checkArgument(
aggregationData instanceof CountData,
createErrorMessageForAggregation(aggregation, aggregationData));
return null;
@@ -276,7 +275,7 @@ public abstract class ViewData {
new Function<Aggregation.Mean, Void>() {
@Override
public Void apply(Aggregation.Mean arg) {
- checkArgument(
+ Utils.checkArgument(
aggregationData instanceof AggregationData.MeanData,
createErrorMessageForAggregation(aggregation, aggregationData));
return null;
@@ -285,7 +284,7 @@ public abstract class ViewData {
new Function<Distribution, Void>() {
@Override
public Void apply(Distribution arg) {
- checkArgument(
+ Utils.checkArgument(
aggregationData instanceof DistributionData,
createErrorMessageForAggregation(aggregation, aggregationData));
return null;
diff --git a/api/src/main/java/io/opencensus/tags/NoopTags.java b/api/src/main/java/io/opencensus/tags/NoopTags.java
index 973a1b40..fb52b164 100644
--- a/api/src/main/java/io/opencensus/tags/NoopTags.java
+++ b/api/src/main/java/io/opencensus/tags/NoopTags.java
@@ -16,11 +16,9 @@
package io.opencensus.tags;
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import com.google.common.base.Preconditions;
import io.opencensus.common.Scope;
import io.opencensus.internal.NoopScope;
+import io.opencensus.internal.Utils;
import io.opencensus.tags.propagation.TagContextBinarySerializer;
import io.opencensus.tags.propagation.TagPropagationComponent;
import java.util.Collections;
@@ -105,8 +103,8 @@ final class NoopTags {
@Override
@Deprecated
public void setState(TaggingState state) {
- Preconditions.checkNotNull(state, "state");
- Preconditions.checkState(!isRead, "State was already read, cannot set state.");
+ Utils.checkNotNull(state, "state");
+ Utils.checkState(!isRead, "State was already read, cannot set state.");
}
}
@@ -131,7 +129,7 @@ final class NoopTags {
@Override
public TagContextBuilder toBuilder(TagContext tags) {
- checkNotNull(tags, "tags");
+ Utils.checkNotNull(tags, "tags");
return getNoopTagContextBuilder();
}
@@ -142,7 +140,7 @@ final class NoopTags {
@Override
public Scope withTagContext(TagContext tags) {
- checkNotNull(tags, "tags");
+ Utils.checkNotNull(tags, "tags");
return NoopScope.getInstance();
}
}
@@ -153,14 +151,14 @@ final class NoopTags {
@Override
public TagContextBuilder put(TagKey key, TagValue value) {
- checkNotNull(key, "key");
- checkNotNull(value, "value");
+ Utils.checkNotNull(key, "key");
+ Utils.checkNotNull(value, "value");
return this;
}
@Override
public TagContextBuilder remove(TagKey key) {
- checkNotNull(key, "key");
+ Utils.checkNotNull(key, "key");
return this;
}
@@ -203,13 +201,13 @@ final class NoopTags {
@Override
public byte[] toByteArray(TagContext tags) {
- checkNotNull(tags, "tags");
+ Utils.checkNotNull(tags, "tags");
return EMPTY_BYTE_ARRAY;
}
@Override
public TagContext fromByteArray(byte[] bytes) {
- checkNotNull(bytes, "bytes");
+ Utils.checkNotNull(bytes, "bytes");
return getNoopTagContext();
}
}
diff --git a/api/src/main/java/io/opencensus/tags/TagKey.java b/api/src/main/java/io/opencensus/tags/TagKey.java
index ba0b91ed..615c2d46 100644
--- a/api/src/main/java/io/opencensus/tags/TagKey.java
+++ b/api/src/main/java/io/opencensus/tags/TagKey.java
@@ -16,10 +16,9 @@
package io.opencensus.tags;
-import static com.google.common.base.Preconditions.checkArgument;
-
import com.google.auto.value.AutoValue;
import io.opencensus.internal.StringUtils;
+import io.opencensus.internal.Utils;
import javax.annotation.concurrent.Immutable;
/**
@@ -61,7 +60,7 @@ public abstract class TagKey {
* @since 0.8
*/
public static TagKey create(String name) {
- checkArgument(isValid(name));
+ Utils.checkArgument(isValid(name), "Invalid TagKey name: " + name);
return new AutoValue_TagKey(name);
}
diff --git a/api/src/main/java/io/opencensus/tags/TagValue.java b/api/src/main/java/io/opencensus/tags/TagValue.java
index 95e13002..5c1019ff 100644
--- a/api/src/main/java/io/opencensus/tags/TagValue.java
+++ b/api/src/main/java/io/opencensus/tags/TagValue.java
@@ -17,8 +17,8 @@
package io.opencensus.tags;
import com.google.auto.value.AutoValue;
-import com.google.common.base.Preconditions;
import io.opencensus.internal.StringUtils;
+import io.opencensus.internal.Utils;
import javax.annotation.concurrent.Immutable;
/**
@@ -55,7 +55,7 @@ public abstract class TagValue {
* @since 0.8
*/
public static TagValue create(String value) {
- Preconditions.checkArgument(isValid(value));
+ Utils.checkArgument(isValid(value), "Invalid TagValue: " + value);
return new AutoValue_TagValue(value);
}
diff --git a/api/src/main/java/io/opencensus/trace/Annotation.java b/api/src/main/java/io/opencensus/trace/Annotation.java
index 5eb3a505..97f2fdd2 100644
--- a/api/src/main/java/io/opencensus/trace/Annotation.java
+++ b/api/src/main/java/io/opencensus/trace/Annotation.java
@@ -16,9 +16,8 @@
package io.opencensus.trace;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.auto.value.AutoValue;
+import io.opencensus.internal.Utils;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -61,7 +60,7 @@ public abstract class Annotation {
return new AutoValue_Annotation(
description,
Collections.unmodifiableMap(
- new HashMap<String, AttributeValue>(checkNotNull(attributes, "attributes"))));
+ new HashMap<String, AttributeValue>(Utils.checkNotNull(attributes, "attributes"))));
}
/**
diff --git a/api/src/main/java/io/opencensus/trace/AttributeValue.java b/api/src/main/java/io/opencensus/trace/AttributeValue.java
index b4759fd2..a8d4e1e3 100644
--- a/api/src/main/java/io/opencensus/trace/AttributeValue.java
+++ b/api/src/main/java/io/opencensus/trace/AttributeValue.java
@@ -16,10 +16,9 @@
package io.opencensus.trace;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.auto.value.AutoValue;
import io.opencensus.common.Function;
+import io.opencensus.internal.Utils;
import javax.annotation.concurrent.Immutable;
/**
@@ -94,7 +93,7 @@ public abstract class AttributeValue {
static AttributeValue create(String stringValue) {
return new AutoValue_AttributeValue_AttributeValueString(
- checkNotNull(stringValue, "stringValue"));
+ Utils.checkNotNull(stringValue, "stringValue"));
}
@Override
@@ -117,7 +116,7 @@ public abstract class AttributeValue {
static AttributeValue create(Boolean stringValue) {
return new AutoValue_AttributeValue_AttributeValueBoolean(
- checkNotNull(stringValue, "stringValue"));
+ Utils.checkNotNull(stringValue, "stringValue"));
}
@Override
@@ -140,7 +139,7 @@ public abstract class AttributeValue {
static AttributeValue create(Long stringValue) {
return new AutoValue_AttributeValue_AttributeValueLong(
- checkNotNull(stringValue, "stringValue"));
+ Utils.checkNotNull(stringValue, "stringValue"));
}
@Override
diff --git a/api/src/main/java/io/opencensus/trace/MessageEvent.java b/api/src/main/java/io/opencensus/trace/MessageEvent.java
index 3be65f52..4b693aaa 100644
--- a/api/src/main/java/io/opencensus/trace/MessageEvent.java
+++ b/api/src/main/java/io/opencensus/trace/MessageEvent.java
@@ -16,9 +16,8 @@
package io.opencensus.trace;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.auto.value.AutoValue;
+import io.opencensus.internal.Utils;
import javax.annotation.concurrent.Immutable;
/**
@@ -66,7 +65,7 @@ public abstract class MessageEvent extends BaseMessageEvent {
*/
public static Builder builder(Type type, long messageId) {
return new AutoValue_MessageEvent.Builder()
- .setType(checkNotNull(type, "type"))
+ .setType(Utils.checkNotNull(type, "type"))
.setMessageId(messageId)
// We need to set a value for the message size because the autovalue requires all
// primitives to be initialized.
diff --git a/api/src/main/java/io/opencensus/trace/NetworkEvent.java b/api/src/main/java/io/opencensus/trace/NetworkEvent.java
index f12597f9..fafedf8e 100644
--- a/api/src/main/java/io/opencensus/trace/NetworkEvent.java
+++ b/api/src/main/java/io/opencensus/trace/NetworkEvent.java
@@ -16,10 +16,9 @@
package io.opencensus.trace;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.auto.value.AutoValue;
import io.opencensus.common.Timestamp;
+import io.opencensus.internal.Utils;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
@@ -67,7 +66,7 @@ public abstract class NetworkEvent extends io.opencensus.trace.BaseMessageEvent
*/
public static Builder builder(Type type, long messageId) {
return new AutoValue_NetworkEvent.Builder()
- .setType(checkNotNull(type, "type"))
+ .setType(Utils.checkNotNull(type, "type"))
.setMessageId(messageId)
// We need to set a value for the message size because the autovalue requires all
// primitives to be initialized.
diff --git a/api/src/main/java/io/opencensus/trace/Span.java b/api/src/main/java/io/opencensus/trace/Span.java
index 523b9517..4a8c6f04 100644
--- a/api/src/main/java/io/opencensus/trace/Span.java
+++ b/api/src/main/java/io/opencensus/trace/Span.java
@@ -16,9 +16,7 @@
package io.opencensus.trace;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-
+import io.opencensus.internal.Utils;
import io.opencensus.trace.internal.BaseMessageEventUtils;
import java.util.Collections;
import java.util.EnumSet;
@@ -76,12 +74,12 @@ public abstract class Span {
* @since 0.5
*/
protected Span(SpanContext context, @Nullable EnumSet<Options> options) {
- this.context = checkNotNull(context, "context");
+ this.context = Utils.checkNotNull(context, "context");
this.options =
options == null
? DEFAULT_OPTIONS
: Collections.<Options>unmodifiableSet(EnumSet.copyOf(options));
- checkArgument(
+ Utils.checkArgument(
!context.getTraceOptions().isSampled() || (this.options.contains(Options.RECORD_EVENTS)),
"Span is sampled, but does not have RECORD_EVENTS set.");
}
diff --git a/api/src/main/java/io/opencensus/trace/SpanBuilder.java b/api/src/main/java/io/opencensus/trace/SpanBuilder.java
index ee68d109..f10cb7c0 100644
--- a/api/src/main/java/io/opencensus/trace/SpanBuilder.java
+++ b/api/src/main/java/io/opencensus/trace/SpanBuilder.java
@@ -16,10 +16,9 @@
package io.opencensus.trace;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.errorprone.annotations.MustBeClosed;
import io.opencensus.common.Scope;
+import io.opencensus.internal.Utils;
import java.util.List;
import java.util.concurrent.Callable;
import javax.annotation.Nullable;
@@ -324,7 +323,7 @@ public abstract class SpanBuilder {
}
private NoopSpanBuilder(String name) {
- checkNotNull(name, "name");
+ Utils.checkNotNull(name, "name");
}
}
}
diff --git a/api/src/main/java/io/opencensus/trace/SpanId.java b/api/src/main/java/io/opencensus/trace/SpanId.java
index eee5f497..859950c7 100644
--- a/api/src/main/java/io/opencensus/trace/SpanId.java
+++ b/api/src/main/java/io/opencensus/trace/SpanId.java
@@ -16,11 +16,9 @@
package io.opencensus.trace;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.common.base.MoreObjects;
import com.google.common.io.BaseEncoding;
+import io.opencensus.internal.Utils;
import java.util.Arrays;
import java.util.Random;
import javax.annotation.Nullable;
@@ -71,8 +69,10 @@ public final class SpanId implements Comparable<SpanId> {
* @since 0.5
*/
public static SpanId fromBytes(byte[] buffer) {
- checkNotNull(buffer, "buffer");
- checkArgument(buffer.length == SIZE, "Invalid size: expected %s, got %s", SIZE, buffer.length);
+ Utils.checkNotNull(buffer, "buffer");
+ Utils.checkArgument(
+ buffer.length == SIZE,
+ String.format("Invalid size: expected %s, got %s", SIZE, buffer.length));
byte[] bytesCopied = Arrays.copyOf(buffer, SIZE);
return new SpanId(bytesCopied);
}
@@ -107,8 +107,9 @@ public final class SpanId implements Comparable<SpanId> {
* @since 0.11
*/
public static SpanId fromLowerBase16(CharSequence src) {
- checkArgument(
- src.length() == 2 * SIZE, "Invalid size: expected %s, got %s", 2 * SIZE, src.length());
+ Utils.checkArgument(
+ src.length() == 2 * SIZE,
+ String.format("Invalid size: expected %s, got %s", 2 * SIZE, src.length()));
byte[] bytes = BaseEncoding.base16().lowerCase().decode(src);
return new SpanId(bytes);
}
diff --git a/api/src/main/java/io/opencensus/trace/Status.java b/api/src/main/java/io/opencensus/trace/Status.java
index b8c6b7a1..0a0e45ce 100644
--- a/api/src/main/java/io/opencensus/trace/Status.java
+++ b/api/src/main/java/io/opencensus/trace/Status.java
@@ -16,11 +16,10 @@
package io.opencensus.trace;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
+import io.opencensus.internal.Utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -386,7 +385,7 @@ public final class Status {
@Nullable private final String description;
private Status(CanonicalCode canonicalCode, @Nullable String description) {
- this.canonicalCode = checkNotNull(canonicalCode, "canonicalCode");
+ this.canonicalCode = Utils.checkNotNull(canonicalCode, "canonicalCode");
this.description = description;
}
diff --git a/api/src/main/java/io/opencensus/trace/TraceId.java b/api/src/main/java/io/opencensus/trace/TraceId.java
index 6378bf58..5683d750 100644
--- a/api/src/main/java/io/opencensus/trace/TraceId.java
+++ b/api/src/main/java/io/opencensus/trace/TraceId.java
@@ -16,12 +16,10 @@
package io.opencensus.trace;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.common.base.MoreObjects;
import com.google.common.io.BaseEncoding;
import io.opencensus.common.Internal;
+import io.opencensus.internal.Utils;
import java.util.Arrays;
import java.util.Random;
import javax.annotation.Nullable;
@@ -72,8 +70,10 @@ public final class TraceId implements Comparable<TraceId> {
* @since 0.5
*/
public static TraceId fromBytes(byte[] buffer) {
- checkNotNull(buffer, "buffer");
- checkArgument(buffer.length == SIZE, "Invalid size: expected %s, got %s", SIZE, buffer.length);
+ Utils.checkNotNull(buffer, "buffer");
+ Utils.checkArgument(
+ buffer.length == SIZE,
+ String.format("Invalid size: expected %s, got %s", SIZE, buffer.length));
byte[] bytesCopied = Arrays.copyOf(buffer, SIZE);
return new TraceId(bytesCopied);
}
@@ -108,8 +108,9 @@ public final class TraceId implements Comparable<TraceId> {
* @since 0.11
*/
public static TraceId fromLowerBase16(CharSequence src) {
- checkArgument(
- src.length() == 2 * SIZE, "Invalid size: expected %s, got %s", 2 * SIZE, src.length());
+ Utils.checkArgument(
+ src.length() == 2 * SIZE,
+ String.format("Invalid size: expected %s, got %s", 2 * SIZE, src.length()));
byte[] bytes = BaseEncoding.base16().lowerCase().decode(src);
return new TraceId(bytes);
}
diff --git a/api/src/main/java/io/opencensus/trace/TraceOptions.java b/api/src/main/java/io/opencensus/trace/TraceOptions.java
index bd6fe6bd..ee09376f 100644
--- a/api/src/main/java/io/opencensus/trace/TraceOptions.java
+++ b/api/src/main/java/io/opencensus/trace/TraceOptions.java
@@ -16,13 +16,10 @@
package io.opencensus.trace;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkElementIndex;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
+import io.opencensus.internal.Utils;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
@@ -78,8 +75,10 @@ public final class TraceOptions {
* @since 0.5
*/
public static TraceOptions fromBytes(byte[] buffer) {
- checkNotNull(buffer, "buffer");
- checkArgument(buffer.length == SIZE, "Invalid size: expected %s, got %s", SIZE, buffer.length);
+ Utils.checkNotNull(buffer, "buffer");
+ Utils.checkArgument(
+ buffer.length == SIZE,
+ String.format("Invalid size: expected %s, got %s", SIZE, buffer.length));
return new TraceOptions(buffer[0]);
}
@@ -97,7 +96,7 @@ public final class TraceOptions {
* @since 0.5
*/
public static TraceOptions fromBytes(byte[] src, int srcOffset) {
- checkElementIndex(srcOffset, src.length);
+ Utils.checkIndex(srcOffset, src.length);
return new TraceOptions(src[srcOffset]);
}
@@ -131,7 +130,7 @@ public final class TraceOptions {
* @since 0.5
*/
public void copyBytesTo(byte[] dest, int destOffset) {
- checkElementIndex(destOffset, dest.length);
+ Utils.checkIndex(destOffset, dest.length);
dest[destOffset] = options;
}
diff --git a/api/src/main/java/io/opencensus/trace/Tracer.java b/api/src/main/java/io/opencensus/trace/Tracer.java
index f4ac8c65..b83fbed6 100644
--- a/api/src/main/java/io/opencensus/trace/Tracer.java
+++ b/api/src/main/java/io/opencensus/trace/Tracer.java
@@ -16,10 +16,9 @@
package io.opencensus.trace;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.errorprone.annotations.MustBeClosed;
import io.opencensus.common.Scope;
+import io.opencensus.internal.Utils;
import io.opencensus.trace.SpanBuilder.NoopSpanBuilder;
import java.util.concurrent.Callable;
import javax.annotation.Nullable;
@@ -152,7 +151,7 @@ public abstract class Tracer {
*/
@MustBeClosed
public final Scope withSpan(Span span) {
- return CurrentSpanUtils.withSpan(checkNotNull(span, "span"), /* endSpan= */ false);
+ return CurrentSpanUtils.withSpan(Utils.checkNotNull(span, "span"), /* endSpan= */ false);
}
/**
diff --git a/api/src/main/java/io/opencensus/trace/config/TraceParams.java b/api/src/main/java/io/opencensus/trace/config/TraceParams.java
index 7594e696..d10642a8 100644
--- a/api/src/main/java/io/opencensus/trace/config/TraceParams.java
+++ b/api/src/main/java/io/opencensus/trace/config/TraceParams.java
@@ -16,9 +16,8 @@
package io.opencensus.trace.config;
-import static com.google.common.base.Preconditions.checkArgument;
-
import com.google.auto.value.AutoValue;
+import io.opencensus.internal.Utils;
import io.opencensus.trace.Annotation;
import io.opencensus.trace.Link;
import io.opencensus.trace.MessageEvent;
@@ -209,10 +208,11 @@ public abstract class TraceParams {
*/
public TraceParams build() {
TraceParams traceParams = autoBuild();
- checkArgument(traceParams.getMaxNumberOfAttributes() > 0, "maxNumberOfAttributes");
- checkArgument(traceParams.getMaxNumberOfAnnotations() > 0, "maxNumberOfAnnotations");
- checkArgument(traceParams.getMaxNumberOfMessageEvents() > 0, "maxNumberOfMessageEvents");
- checkArgument(traceParams.getMaxNumberOfLinks() > 0, "maxNumberOfLinks");
+ Utils.checkArgument(traceParams.getMaxNumberOfAttributes() > 0, "maxNumberOfAttributes");
+ Utils.checkArgument(traceParams.getMaxNumberOfAnnotations() > 0, "maxNumberOfAnnotations");
+ Utils.checkArgument(
+ traceParams.getMaxNumberOfMessageEvents() > 0, "maxNumberOfMessageEvents");
+ Utils.checkArgument(traceParams.getMaxNumberOfLinks() > 0, "maxNumberOfLinks");
return traceParams;
}
}
diff --git a/api/src/main/java/io/opencensus/trace/export/RunningSpanStore.java b/api/src/main/java/io/opencensus/trace/export/RunningSpanStore.java
index aa05e4fe..fac3c855 100644
--- a/api/src/main/java/io/opencensus/trace/export/RunningSpanStore.java
+++ b/api/src/main/java/io/opencensus/trace/export/RunningSpanStore.java
@@ -16,10 +16,8 @@
package io.opencensus.trace.export;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.auto.value.AutoValue;
+import io.opencensus.internal.Utils;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -92,7 +90,7 @@ public abstract class RunningSpanStore {
return new AutoValue_RunningSpanStore_Summary(
Collections.unmodifiableMap(
new HashMap<String, PerSpanNameSummary>(
- checkNotNull(perSpanNameSummary, "perSpanNameSummary"))));
+ Utils.checkNotNull(perSpanNameSummary, "perSpanNameSummary"))));
}
/**
@@ -124,7 +122,7 @@ public abstract class RunningSpanStore {
* @since 0.5
*/
public static PerSpanNameSummary create(int numRunningSpans) {
- checkArgument(numRunningSpans >= 0, "Negative numRunningSpans.");
+ Utils.checkArgument(numRunningSpans >= 0, "Negative numRunningSpans.");
return new AutoValue_RunningSpanStore_PerSpanNameSummary(numRunningSpans);
}
@@ -163,7 +161,7 @@ public abstract class RunningSpanStore {
* @since 0.5
*/
public static Filter create(String spanName, int maxSpansToReturn) {
- checkArgument(maxSpansToReturn >= 0, "Negative maxSpansToReturn.");
+ Utils.checkArgument(maxSpansToReturn >= 0, "Negative maxSpansToReturn.");
return new AutoValue_RunningSpanStore_Filter(spanName, maxSpansToReturn);
}
@@ -196,7 +194,7 @@ public abstract class RunningSpanStore {
@Override
public Collection<SpanData> getRunningSpans(Filter filter) {
- checkNotNull(filter, "filter");
+ Utils.checkNotNull(filter, "filter");
return Collections.<SpanData>emptyList();
}
}
diff --git a/api/src/main/java/io/opencensus/trace/export/SampledSpanStore.java b/api/src/main/java/io/opencensus/trace/export/SampledSpanStore.java
index 5e7f4dd6..3ee80ce2 100644
--- a/api/src/main/java/io/opencensus/trace/export/SampledSpanStore.java
+++ b/api/src/main/java/io/opencensus/trace/export/SampledSpanStore.java
@@ -16,11 +16,9 @@
package io.opencensus.trace.export;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
+import io.opencensus.internal.Utils;
import io.opencensus.trace.Span;
import io.opencensus.trace.Status;
import io.opencensus.trace.Status.CanonicalCode;
@@ -157,7 +155,7 @@ public abstract class SampledSpanStore {
return new AutoValue_SampledSpanStore_Summary(
Collections.unmodifiableMap(
new HashMap<String, PerSpanNameSummary>(
- checkNotNull(perSpanNameSummary, "perSpanNameSummary"))));
+ Utils.checkNotNull(perSpanNameSummary, "perSpanNameSummary"))));
}
/**
@@ -196,10 +194,11 @@ public abstract class SampledSpanStore {
return new AutoValue_SampledSpanStore_PerSpanNameSummary(
Collections.unmodifiableMap(
new HashMap<LatencyBucketBoundaries, Integer>(
- checkNotNull(numbersOfLatencySampledSpans, "numbersOfLatencySampledSpans"))),
+ Utils.checkNotNull(
+ numbersOfLatencySampledSpans, "numbersOfLatencySampledSpans"))),
Collections.unmodifiableMap(
new HashMap<CanonicalCode, Integer>(
- checkNotNull(numbersOfErrorSampledSpans, "numbersOfErrorSampledSpans"))));
+ Utils.checkNotNull(numbersOfErrorSampledSpans, "numbersOfErrorSampledSpans"))));
}
/**
@@ -361,9 +360,9 @@ public abstract class SampledSpanStore {
*/
public static LatencyFilter create(
String spanName, long latencyLowerNs, long latencyUpperNs, int maxSpansToReturn) {
- checkArgument(maxSpansToReturn >= 0, "Negative maxSpansToReturn.");
- checkArgument(latencyLowerNs >= 0, "Negative latencyLowerNs");
- checkArgument(latencyUpperNs >= 0, "Negative latencyUpperNs");
+ Utils.checkArgument(maxSpansToReturn >= 0, "Negative maxSpansToReturn.");
+ Utils.checkArgument(latencyLowerNs >= 0, "Negative latencyLowerNs");
+ Utils.checkArgument(latencyUpperNs >= 0, "Negative latencyUpperNs");
return new AutoValue_SampledSpanStore_LatencyFilter(
spanName, latencyLowerNs, latencyUpperNs, maxSpansToReturn);
}
@@ -432,9 +431,9 @@ public abstract class SampledSpanStore {
public static ErrorFilter create(
String spanName, @Nullable CanonicalCode canonicalCode, int maxSpansToReturn) {
if (canonicalCode != null) {
- checkArgument(canonicalCode != CanonicalCode.OK, "Invalid canonical code.");
+ Utils.checkArgument(canonicalCode != CanonicalCode.OK, "Invalid canonical code.");
}
- checkArgument(maxSpansToReturn >= 0, "Negative maxSpansToReturn.");
+ Utils.checkArgument(maxSpansToReturn >= 0, "Negative maxSpansToReturn.");
return new AutoValue_SampledSpanStore_ErrorFilter(spanName, canonicalCode, maxSpansToReturn);
}
@@ -489,19 +488,19 @@ public abstract class SampledSpanStore {
@Override
public Collection<SpanData> getLatencySampledSpans(LatencyFilter filter) {
- checkNotNull(filter, "latencyFilter");
+ Utils.checkNotNull(filter, "latencyFilter");
return Collections.<SpanData>emptyList();
}
@Override
public Collection<SpanData> getErrorSampledSpans(ErrorFilter filter) {
- checkNotNull(filter, "errorFilter");
+ Utils.checkNotNull(filter, "errorFilter");
return Collections.<SpanData>emptyList();
}
@Override
public void registerSpanNamesForCollection(Collection<String> spanNames) {
- checkNotNull(spanNames, "spanNames");
+ Utils.checkNotNull(spanNames, "spanNames");
synchronized (registeredSpanNames) {
registeredSpanNames.addAll(spanNames);
}
@@ -509,7 +508,7 @@ public abstract class SampledSpanStore {
@Override
public void unregisterSpanNamesForCollection(Collection<String> spanNames) {
- checkNotNull(spanNames);
+ Utils.checkNotNull(spanNames, "spanNames");
synchronized (registeredSpanNames) {
registeredSpanNames.removeAll(spanNames);
}
diff --git a/api/src/main/java/io/opencensus/trace/export/SpanData.java b/api/src/main/java/io/opencensus/trace/export/SpanData.java
index 34a0920d..7b1daf42 100644
--- a/api/src/main/java/io/opencensus/trace/export/SpanData.java
+++ b/api/src/main/java/io/opencensus/trace/export/SpanData.java
@@ -16,10 +16,9 @@
package io.opencensus.trace.export;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.auto.value.AutoValue;
import io.opencensus.common.Timestamp;
+import io.opencensus.internal.Utils;
import io.opencensus.trace.Annotation;
import io.opencensus.trace.AttributeValue;
import io.opencensus.trace.Link;
@@ -319,7 +318,7 @@ public abstract class SpanData {
public static <T> TimedEvents<T> create(List<TimedEvent<T>> events, int droppedEventsCount) {
return new AutoValue_SpanData_TimedEvents<T>(
Collections.unmodifiableList(
- new ArrayList<TimedEvent<T>>(checkNotNull(events, "events"))),
+ new ArrayList<TimedEvent<T>>(Utils.checkNotNull(events, "events"))),
droppedEventsCount);
}
@@ -364,7 +363,8 @@ public abstract class SpanData {
// for others on account of determinism.
return new AutoValue_SpanData_Attributes(
Collections.unmodifiableMap(
- new HashMap<String, AttributeValue>(checkNotNull(attributeMap, "attributeMap"))),
+ new HashMap<String, AttributeValue>(
+ Utils.checkNotNull(attributeMap, "attributeMap"))),
droppedAttributesCount);
}
@@ -405,7 +405,7 @@ public abstract class SpanData {
*/
public static Links create(List<Link> links, int droppedLinksCount) {
return new AutoValue_SpanData_Links(
- Collections.unmodifiableList(new ArrayList<Link>(checkNotNull(links, "links"))),
+ Collections.unmodifiableList(new ArrayList<Link>(Utils.checkNotNull(links, "links"))),
droppedLinksCount);
}
diff --git a/api/src/main/java/io/opencensus/trace/internal/BaseMessageEventUtils.java b/api/src/main/java/io/opencensus/trace/internal/BaseMessageEventUtils.java
index 812ec30a..9d22a1c6 100644
--- a/api/src/main/java/io/opencensus/trace/internal/BaseMessageEventUtils.java
+++ b/api/src/main/java/io/opencensus/trace/internal/BaseMessageEventUtils.java
@@ -16,9 +16,8 @@
package io.opencensus.trace.internal;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import io.opencensus.common.Internal;
+import io.opencensus.internal.Utils;
/**
* Helper class to convert/cast between for {@link io.opencensus.trace.MessageEvent} and {@link
@@ -39,7 +38,7 @@ public final class BaseMessageEventUtils {
*/
public static io.opencensus.trace.MessageEvent asMessageEvent(
io.opencensus.trace.BaseMessageEvent event) {
- checkNotNull(event);
+ Utils.checkNotNull(event, "event");
if (event instanceof io.opencensus.trace.MessageEvent) {
return (io.opencensus.trace.MessageEvent) event;
}
@@ -63,7 +62,7 @@ public final class BaseMessageEventUtils {
*/
public static io.opencensus.trace.NetworkEvent asNetworkEvent(
io.opencensus.trace.BaseMessageEvent event) {
- checkNotNull(event);
+ Utils.checkNotNull(event, "event");
if (event instanceof io.opencensus.trace.NetworkEvent) {
return (io.opencensus.trace.NetworkEvent) event;
}
diff --git a/api/src/main/java/io/opencensus/trace/propagation/BinaryFormat.java b/api/src/main/java/io/opencensus/trace/propagation/BinaryFormat.java
index e07afd12..da7ea4cc 100644
--- a/api/src/main/java/io/opencensus/trace/propagation/BinaryFormat.java
+++ b/api/src/main/java/io/opencensus/trace/propagation/BinaryFormat.java
@@ -16,8 +16,7 @@
package io.opencensus.trace.propagation;
-import static com.google.common.base.Preconditions.checkNotNull;
-
+import io.opencensus.internal.Utils;
import io.opencensus.trace.SpanContext;
import java.text.ParseException;
@@ -138,13 +137,13 @@ public abstract class BinaryFormat {
private static final class NoopBinaryFormat extends BinaryFormat {
@Override
public byte[] toByteArray(SpanContext spanContext) {
- checkNotNull(spanContext, "spanContext");
+ Utils.checkNotNull(spanContext, "spanContext");
return new byte[0];
}
@Override
public SpanContext fromByteArray(byte[] bytes) {
- checkNotNull(bytes, "bytes");
+ Utils.checkNotNull(bytes, "bytes");
return SpanContext.INVALID;
}
diff --git a/api/src/main/java/io/opencensus/trace/propagation/TextFormat.java b/api/src/main/java/io/opencensus/trace/propagation/TextFormat.java
index 66d018ce..d52e71f1 100644
--- a/api/src/main/java/io/opencensus/trace/propagation/TextFormat.java
+++ b/api/src/main/java/io/opencensus/trace/propagation/TextFormat.java
@@ -16,9 +16,8 @@
package io.opencensus.trace.propagation;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import io.opencensus.common.ExperimentalApi;
+import io.opencensus.internal.Utils;
import io.opencensus.trace.SpanContext;
import java.util.Collections;
import java.util.List;
@@ -190,15 +189,15 @@ public abstract class TextFormat {
@Override
public <C /*>>> extends @NonNull Object*/> void inject(
SpanContext spanContext, C carrier, Setter<C> setter) {
- checkNotNull(spanContext, "spanContext");
- checkNotNull(carrier, "carrier");
- checkNotNull(setter, "setter");
+ Utils.checkNotNull(spanContext, "spanContext");
+ Utils.checkNotNull(carrier, "carrier");
+ Utils.checkNotNull(setter, "setter");
}
@Override
public <C /*>>> extends @NonNull Object*/> SpanContext extract(C carrier, Getter<C> getter) {
- checkNotNull(carrier, "carrier");
- checkNotNull(getter, "getter");
+ Utils.checkNotNull(carrier, "carrier");
+ Utils.checkNotNull(getter, "getter");
return SpanContext.INVALID;
}
}
diff --git a/api/src/main/java/io/opencensus/trace/samplers/ProbabilitySampler.java b/api/src/main/java/io/opencensus/trace/samplers/ProbabilitySampler.java
index 21d4d6ca..b9c18e00 100644
--- a/api/src/main/java/io/opencensus/trace/samplers/ProbabilitySampler.java
+++ b/api/src/main/java/io/opencensus/trace/samplers/ProbabilitySampler.java
@@ -16,9 +16,8 @@
package io.opencensus.trace.samplers;
-import static com.google.common.base.Preconditions.checkArgument;
-
import com.google.auto.value.AutoValue;
+import io.opencensus.internal.Utils;
import io.opencensus.trace.Sampler;
import io.opencensus.trace.Span;
import io.opencensus.trace.SpanContext;
@@ -54,7 +53,7 @@ abstract class ProbabilitySampler extends Sampler {
* @throws IllegalArgumentException if {@code probability} is out of range
*/
static ProbabilitySampler create(double probability) {
- checkArgument(
+ Utils.checkArgument(
probability >= 0.0 && probability <= 1.0, "probability must be in range [0.0, 1.0]");
long idUpperBound;
// Special case the limits, to avoid any possible issues with lack of precision across
diff --git a/api/src/test/java/io/opencensus/internal/UtilsTest.java b/api/src/test/java/io/opencensus/internal/UtilsTest.java
new file mode 100644
index 00000000..2416eeb8
--- /dev/null
+++ b/api/src/test/java/io/opencensus/internal/UtilsTest.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.opencensus.internal;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link Utils}. */
+@RunWith(JUnit4.class)
+public final class UtilsTest {
+ private static final String TEST_MESSAGE = "test message";
+
+ @Rule public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void checkArgument() {
+ Utils.checkArgument(true, TEST_MESSAGE);
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage(TEST_MESSAGE);
+ Utils.checkArgument(false, TEST_MESSAGE);
+ }
+
+ @Test
+ public void checkState() {
+ Utils.checkNotNull(true, TEST_MESSAGE);
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage(TEST_MESSAGE);
+ Utils.checkState(false, TEST_MESSAGE);
+ }
+
+ @Test
+ public void checkNotNull() {
+ Utils.checkNotNull(new Object(), TEST_MESSAGE);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(TEST_MESSAGE);
+ Utils.checkNotNull(null, TEST_MESSAGE);
+ }
+
+ @Test
+ public void checkIndex_Valid() {
+ Utils.checkIndex(1, 2);
+ }
+
+ @Test
+ public void checkIndex_NegativeSize() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Negative size: -1");
+ Utils.checkIndex(0, -1);
+ }
+
+ @Test
+ public void checkIndex_NegativeIndex() {
+ thrown.expect(IndexOutOfBoundsException.class);
+ thrown.expectMessage("Index out of bounds: size=10, index=-2");
+ Utils.checkIndex(-2, 10);
+ }
+
+ @Test
+ public void checkIndex_IndexEqualToSize() {
+ thrown.expect(IndexOutOfBoundsException.class);
+ thrown.expectMessage("Index out of bounds: size=5, index=5");
+ Utils.checkIndex(5, 5);
+ }
+
+ @Test
+ public void checkIndex_IndexGreaterThanSize() {
+ thrown.expect(IndexOutOfBoundsException.class);
+ thrown.expectMessage("Index out of bounds: size=10, index=11");
+ Utils.checkIndex(11, 10);
+ }
+}
diff --git a/findbugs-exclude.xml b/findbugs-exclude.xml
index 9de28c1c..094bd2fa 100644
--- a/findbugs-exclude.xml
+++ b/findbugs-exclude.xml
@@ -22,6 +22,12 @@
<Bug pattern="BC_UNCONFIRMED_CAST"/>
<Class name="io.opencensus.trace.internal.BaseMessageEventUtils" />
</Match>
+ <Match>
+ <!-- Reason: This test is testing for a NPE. -->
+ <Bug pattern="NP_NONNULL_PARAM_VIOLATION"/>
+ <Class name="io.opencensus.internal.UtilsTest" />
+ <Method name="checkNotNull" />
+ </Match>
<!-- Suppress some FindBugs warnings related to performance or robustness -->
<!-- in test classes, where those issues are less important. -->