aboutsummaryrefslogtreecommitdiff
path: root/api/src/main/java/io/opencensus/internal
diff options
context:
space:
mode:
Diffstat (limited to 'api/src/main/java/io/opencensus/internal')
-rw-r--r--api/src/main/java/io/opencensus/internal/DefaultVisibilityForTesting.java37
-rw-r--r--api/src/main/java/io/opencensus/internal/NoopScope.java38
-rw-r--r--api/src/main/java/io/opencensus/internal/Provider.java49
-rw-r--r--api/src/main/java/io/opencensus/internal/StringUtils.java42
-rw-r--r--api/src/main/java/io/opencensus/internal/Utils.java191
-rw-r--r--api/src/main/java/io/opencensus/internal/ZeroTimeClock.java49
-rw-r--r--api/src/main/java/io/opencensus/internal/package-info.java24
7 files changed, 430 insertions, 0 deletions
diff --git a/api/src/main/java/io/opencensus/internal/DefaultVisibilityForTesting.java b/api/src/main/java/io/opencensus/internal/DefaultVisibilityForTesting.java
new file mode 100644
index 00000000..e90a6573
--- /dev/null
+++ b/api/src/main/java/io/opencensus/internal/DefaultVisibilityForTesting.java
@@ -0,0 +1,37 @@
+/*
+ * 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 java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Indicates that an element is package-private instead of private only for the purpose of testing.
+ * This annotation is only meant to be used as documentation in the source code.
+ */
+@Retention(RetentionPolicy.SOURCE)
+@Target({
+ ElementType.ANNOTATION_TYPE,
+ ElementType.CONSTRUCTOR,
+ ElementType.FIELD,
+ ElementType.METHOD,
+ ElementType.PACKAGE,
+ ElementType.TYPE
+})
+public @interface DefaultVisibilityForTesting {}
diff --git a/api/src/main/java/io/opencensus/internal/NoopScope.java b/api/src/main/java/io/opencensus/internal/NoopScope.java
new file mode 100644
index 00000000..f4a8da07
--- /dev/null
+++ b/api/src/main/java/io/opencensus/internal/NoopScope.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2017, 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 io.opencensus.common.Scope;
+
+/** A {@link Scope} that does nothing when it is created or closed. */
+public final class NoopScope implements Scope {
+ private static final Scope INSTANCE = new NoopScope();
+
+ private NoopScope() {}
+
+ /**
+ * Returns a {@code NoopScope}.
+ *
+ * @return a {@code NoopScope}.
+ */
+ public static Scope getInstance() {
+ return INSTANCE;
+ }
+
+ @Override
+ public void close() {}
+}
diff --git a/api/src/main/java/io/opencensus/internal/Provider.java b/api/src/main/java/io/opencensus/internal/Provider.java
new file mode 100644
index 00000000..8cfb7294
--- /dev/null
+++ b/api/src/main/java/io/opencensus/internal/Provider.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2016-17, 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 java.util.ServiceConfigurationError;
+
+/**
+ * OpenCensus service provider mechanism.
+ *
+ * <pre>{@code
+ * // Initialize a variable using reflection.
+ * foo = Provider.createInstance(
+ * Class.forName("FooImpl", true, classLoader), Foo.class);
+ * }</pre>
+ */
+public final class Provider {
+ private Provider() {}
+
+ /**
+ * Tries to create an instance of the given rawClass as a subclass of the given superclass.
+ *
+ * @param rawClass The class that is initialized.
+ * @param superclass The initialized class must be a subclass of this.
+ * @return an instance of the class given rawClass which is a subclass of the given superclass.
+ * @throws ServiceConfigurationError if any error happens.
+ */
+ public static <T> T createInstance(Class<?> rawClass, Class<T> superclass) {
+ try {
+ return rawClass.asSubclass(superclass).getConstructor().newInstance();
+ } catch (Exception e) {
+ throw new ServiceConfigurationError(
+ "Provider " + rawClass.getName() + " could not be instantiated.", e);
+ }
+ }
+}
diff --git a/api/src/main/java/io/opencensus/internal/StringUtils.java b/api/src/main/java/io/opencensus/internal/StringUtils.java
new file mode 100644
index 00000000..717e333c
--- /dev/null
+++ b/api/src/main/java/io/opencensus/internal/StringUtils.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2016-17, 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;
+
+/** Internal utility methods for working with tag keys, tag values, and metric names. */
+public final class StringUtils {
+
+ /**
+ * Determines whether the {@code String} contains only printable characters.
+ *
+ * @param str the {@code String} to be validated.
+ * @return whether the {@code String} contains only printable characters.
+ */
+ public static boolean isPrintableString(String str) {
+ for (int i = 0; i < str.length(); i++) {
+ if (!isPrintableChar(str.charAt(i))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static boolean isPrintableChar(char ch) {
+ return ch >= ' ' && ch <= '~';
+ }
+
+ private StringUtils() {}
+}
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..df5c9840
--- /dev/null
+++ b/api/src/main/java/io/opencensus/internal/Utils.java
@@ -0,0 +1,191 @@
+/*
+ * 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 java.util.List;
+
+/*>>>
+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 errorMessage the message to use for the exception. Will be converted to a string using
+ * {@link String#valueOf(Object)}.
+ */
+ public static void checkArgument(
+ boolean isValid, @javax.annotation.Nullable Object errorMessage) {
+ if (!isValid) {
+ throw new IllegalArgumentException(String.valueOf(errorMessage));
+ }
+ }
+
+ /**
+ * Throws an {@link IllegalArgumentException} if the argument is false. This method is similar to
+ * {@code Preconditions.checkArgument(boolean, Object)} from Guava.
+ *
+ * @param expression a boolean expression
+ * @param errorMessageTemplate a template for the exception message should the check fail. The
+ * message is formed by replacing each {@code %s} placeholder in the template with an
+ * argument. These are matched by position - the first {@code %s} gets {@code
+ * errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in
+ * square braces. Unmatched placeholders will be left as-is.
+ * @param errorMessageArgs the arguments to be substituted into the message template. Arguments
+ * are converted to strings using {@link String#valueOf(Object)}.
+ * @throws IllegalArgumentException if {@code expression} is false
+ * @throws NullPointerException if the check fails and either {@code errorMessageTemplate} or
+ * {@code errorMessageArgs} is null (don't let this happen)
+ */
+ public static void checkArgument(
+ boolean expression,
+ String errorMessageTemplate,
+ @javax.annotation.Nullable Object... errorMessageArgs) {
+ if (!expression) {
+ throw new IllegalArgumentException(format(errorMessageTemplate, errorMessageArgs));
+ }
+ }
+
+ /**
+ * 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 errorMessage the message to use for the exception. Will be converted to a string using
+ * {@link String#valueOf(Object)}.
+ */
+ public static void checkState(boolean isValid, @javax.annotation.Nullable Object errorMessage) {
+ if (!isValid) {
+ throw new IllegalStateException(String.valueOf(errorMessage));
+ }
+ }
+
+ /**
+ * 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 errorMessage the message to use for the exception. Will be converted to a string using
+ * {@link String#valueOf(Object)}.
+ * @return the argument, if it passes the null check.
+ */
+ public static <T /*>>> extends @NonNull Object*/> T checkNotNull(
+ T arg, @javax.annotation.Nullable Object errorMessage) {
+ if (arg == null) {
+ throw new NullPointerException(String.valueOf(errorMessage));
+ }
+ return arg;
+ }
+
+ /**
+ * Throws a {@link NullPointerException} if any of the list elements is null.
+ *
+ * @param list the argument list to check for null.
+ * @param errorMessage the message to use for the exception. Will be converted to a string using
+ * {@link String#valueOf(Object)}.
+ */
+ public static <T /*>>> extends @NonNull Object*/> void checkListElementNotNull(
+ List<T> list, @javax.annotation.Nullable Object errorMessage) {
+ for (T element : list) {
+ if (element == null) {
+ throw new NullPointerException(String.valueOf(errorMessage));
+ }
+ }
+ }
+
+ /**
+ * Compares two Objects for equality. This functionality is provided by {@code
+ * Objects.equal(Object, Object)} in Java 7.
+ */
+ public static boolean equalsObjects(
+ @javax.annotation.Nullable Object x, @javax.annotation.Nullable Object y) {
+ return x == null ? y == null : x.equals(y);
+ }
+
+ /**
+ * Substitutes each {@code %s} in {@code template} with an argument. These are matched by
+ * position: the first {@code %s} gets {@code args[0]}, etc. If there are more arguments than
+ * placeholders, the unmatched arguments will be appended to the end of the formatted message in
+ * square braces.
+ *
+ * <p>Copied from {@code Preconditions.format(String, Object...)} from Guava
+ *
+ * @param template a non-null string containing 0 or more {@code %s} placeholders.
+ * @param args the arguments to be substituted into the message template. Arguments are converted
+ * to strings using {@link String#valueOf(Object)}. Arguments can be null.
+ */
+ // Note that this is somewhat-improperly used from Verify.java as well.
+ private static String format(String template, @javax.annotation.Nullable Object... args) {
+ // If no arguments return the template.
+ if (args == null) {
+ return template;
+ }
+
+ // start substituting the arguments into the '%s' placeholders
+ StringBuilder builder = new StringBuilder(template.length() + 16 * args.length);
+ int templateStart = 0;
+ int i = 0;
+ while (i < args.length) {
+ int placeholderStart = template.indexOf("%s", templateStart);
+ if (placeholderStart == -1) {
+ break;
+ }
+ builder.append(template, templateStart, placeholderStart);
+ builder.append(args[i++]);
+ templateStart = placeholderStart + 2;
+ }
+ builder.append(template, templateStart, template.length());
+
+ // if we run out of placeholders, append the extra args in square braces
+ if (i < args.length) {
+ builder.append(" [");
+ builder.append(args[i++]);
+ while (i < args.length) {
+ builder.append(", ");
+ builder.append(args[i++]);
+ }
+ builder.append(']');
+ }
+
+ return builder.toString();
+ }
+}
diff --git a/api/src/main/java/io/opencensus/internal/ZeroTimeClock.java b/api/src/main/java/io/opencensus/internal/ZeroTimeClock.java
new file mode 100644
index 00000000..fda13e9e
--- /dev/null
+++ b/api/src/main/java/io/opencensus/internal/ZeroTimeClock.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2017, 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 io.opencensus.common.Clock;
+import io.opencensus.common.Timestamp;
+import javax.annotation.concurrent.Immutable;
+
+/** A {@link Clock} that always returns 0. */
+@Immutable
+public final class ZeroTimeClock extends Clock {
+ private static final ZeroTimeClock INSTANCE = new ZeroTimeClock();
+ private static final Timestamp ZERO_TIMESTAMP = Timestamp.create(0, 0);
+
+ private ZeroTimeClock() {}
+
+ /**
+ * Returns a {@code ZeroTimeClock}.
+ *
+ * @return a {@code ZeroTimeClock}.
+ */
+ public static ZeroTimeClock getInstance() {
+ return INSTANCE;
+ }
+
+ @Override
+ public Timestamp now() {
+ return ZERO_TIMESTAMP;
+ }
+
+ @Override
+ public long nowNanos() {
+ return 0;
+ }
+}
diff --git a/api/src/main/java/io/opencensus/internal/package-info.java b/api/src/main/java/io/opencensus/internal/package-info.java
new file mode 100644
index 00000000..5dd35b23
--- /dev/null
+++ b/api/src/main/java/io/opencensus/internal/package-info.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2017, 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.
+ */
+
+/**
+ * Interfaces and implementations that are internal to OpenCensus.
+ *
+ * <p>All the content under this package and its subpackages are considered annotated with {@link
+ * io.opencensus.common.Internal}.
+ */
+@io.opencensus.common.Internal
+package io.opencensus.internal;