aboutsummaryrefslogtreecommitdiff
path: root/impl_core/src/main/java/io/opencensus/implcore/tags
diff options
context:
space:
mode:
authorBogdan Drutu <bdrutu@google.com>2018-08-21 12:50:38 -0700
committerGitHub <noreply@github.com>2018-08-21 12:50:38 -0700
commit07ede4e46f2256917b4d4fb0968c09817b33b10a (patch)
tree394a5c69be55d433036da38827620955dd8771e1 /impl_core/src/main/java/io/opencensus/implcore/tags
parent847bdcb323507916f2c9d851fc86228ec74ef038 (diff)
downloadopencensus-java-07ede4e46f2256917b4d4fb0968c09817b33b10a.tar.gz
Extract logic about current state in internal class. (#1381)
Diffstat (limited to 'impl_core/src/main/java/io/opencensus/implcore/tags')
-rw-r--r--impl_core/src/main/java/io/opencensus/implcore/tags/CurrentTaggingState.java55
-rw-r--r--impl_core/src/main/java/io/opencensus/implcore/tags/TaggerImpl.java17
-rw-r--r--impl_core/src/main/java/io/opencensus/implcore/tags/TagsComponentImplBase.java23
-rw-r--r--impl_core/src/main/java/io/opencensus/implcore/tags/propagation/TagContextBinarySerializerImpl.java12
-rw-r--r--impl_core/src/main/java/io/opencensus/implcore/tags/propagation/TagPropagationComponentImpl.java4
5 files changed, 34 insertions, 77 deletions
diff --git a/impl_core/src/main/java/io/opencensus/implcore/tags/CurrentTaggingState.java b/impl_core/src/main/java/io/opencensus/implcore/tags/CurrentTaggingState.java
deleted file mode 100644
index 8be055bb..00000000
--- a/impl_core/src/main/java/io/opencensus/implcore/tags/CurrentTaggingState.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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.implcore.tags;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import com.google.common.base.Preconditions;
-import io.opencensus.tags.TaggingState;
-import io.opencensus.tags.TagsComponent;
-import javax.annotation.concurrent.GuardedBy;
-import javax.annotation.concurrent.ThreadSafe;
-
-/**
- * The current {@link TaggingState} for a {@link TagsComponent}.
- *
- * <p>This class allows different tagging classes to share the state in a thread-safe way.
- */
-@ThreadSafe
-public final class CurrentTaggingState {
-
- @GuardedBy("this")
- private TaggingState currentState = TaggingState.ENABLED;
-
- @GuardedBy("this")
- private boolean isRead;
-
- public synchronized TaggingState get() {
- isRead = true;
- return getInternal();
- }
-
- public synchronized TaggingState getInternal() {
- return currentState;
- }
-
- // Sets current state to the given state.
- synchronized void set(TaggingState state) {
- Preconditions.checkState(!isRead, "State was already read, cannot set state.");
- currentState = checkNotNull(state, "state");
- }
-}
diff --git a/impl_core/src/main/java/io/opencensus/implcore/tags/TaggerImpl.java b/impl_core/src/main/java/io/opencensus/implcore/tags/TaggerImpl.java
index 0159ddbe..9f3a5429 100644
--- a/impl_core/src/main/java/io/opencensus/implcore/tags/TaggerImpl.java
+++ b/impl_core/src/main/java/io/opencensus/implcore/tags/TaggerImpl.java
@@ -17,13 +17,14 @@
package io.opencensus.implcore.tags;
import io.opencensus.common.Scope;
+import io.opencensus.implcore.internal.CurrentState;
+import io.opencensus.implcore.internal.CurrentState.State;
import io.opencensus.implcore.internal.NoopScope;
import io.opencensus.tags.InternalUtils;
import io.opencensus.tags.Tag;
import io.opencensus.tags.TagContext;
import io.opencensus.tags.TagContextBuilder;
import io.opencensus.tags.Tagger;
-import io.opencensus.tags.TaggingState;
import java.util.Iterator;
public final class TaggerImpl extends Tagger {
@@ -31,9 +32,9 @@ public final class TaggerImpl extends Tagger {
// withTagContext(...) always puts a TagContextImpl into scope, even if the argument is another
// TagContext subclass.
- private final CurrentTaggingState state;
+ private final CurrentState state;
- TaggerImpl(CurrentTaggingState state) {
+ TaggerImpl(CurrentState state) {
this.state = state;
}
@@ -44,35 +45,35 @@ public final class TaggerImpl extends Tagger {
@Override
public TagContextImpl getCurrentTagContext() {
- return state.getInternal() == TaggingState.DISABLED
+ return state.getInternal() == State.DISABLED
? TagContextImpl.EMPTY
: toTagContextImpl(CurrentTagContextUtils.getCurrentTagContext());
}
@Override
public TagContextBuilder emptyBuilder() {
- return state.getInternal() == TaggingState.DISABLED
+ return state.getInternal() == State.DISABLED
? NoopTagContextBuilder.INSTANCE
: new TagContextBuilderImpl();
}
@Override
public TagContextBuilder currentBuilder() {
- return state.getInternal() == TaggingState.DISABLED
+ return state.getInternal() == State.DISABLED
? NoopTagContextBuilder.INSTANCE
: toBuilder(CurrentTagContextUtils.getCurrentTagContext());
}
@Override
public TagContextBuilder toBuilder(TagContext tags) {
- return state.getInternal() == TaggingState.DISABLED
+ return state.getInternal() == State.DISABLED
? NoopTagContextBuilder.INSTANCE
: toTagContextBuilderImpl(tags);
}
@Override
public Scope withTagContext(TagContext tags) {
- return state.getInternal() == TaggingState.DISABLED
+ return state.getInternal() == State.DISABLED
? NoopScope.getInstance()
: CurrentTagContextUtils.withTagContext(toTagContextImpl(tags));
}
diff --git a/impl_core/src/main/java/io/opencensus/implcore/tags/TagsComponentImplBase.java b/impl_core/src/main/java/io/opencensus/implcore/tags/TagsComponentImplBase.java
index c05f2fa8..88c31bae 100644
--- a/impl_core/src/main/java/io/opencensus/implcore/tags/TagsComponentImplBase.java
+++ b/impl_core/src/main/java/io/opencensus/implcore/tags/TagsComponentImplBase.java
@@ -18,6 +18,8 @@ package io.opencensus.implcore.tags;
import static com.google.common.base.Preconditions.checkNotNull;
+import io.opencensus.implcore.internal.CurrentState;
+import io.opencensus.implcore.internal.CurrentState.State;
import io.opencensus.implcore.tags.propagation.TagPropagationComponentImpl;
import io.opencensus.tags.Tagger;
import io.opencensus.tags.TaggingState;
@@ -26,13 +28,14 @@ import io.opencensus.tags.propagation.TagPropagationComponent;
/** Base implementation of {@link TagsComponent}. */
public class TagsComponentImplBase extends TagsComponent {
+ private static final State DEFAULT_STATE = State.ENABLED;
- // The TaggingState shared between the TagsComponent, Tagger, and TagPropagationComponent
- private final CurrentTaggingState state = new CurrentTaggingState();
+ // The State shared between the TagsComponent, Tagger, and TagPropagationComponent
+ private final CurrentState currentState = new CurrentState(DEFAULT_STATE);
- private final Tagger tagger = new TaggerImpl(state);
+ private final Tagger tagger = new TaggerImpl(currentState);
private final TagPropagationComponent tagPropagationComponent =
- new TagPropagationComponentImpl(state);
+ new TagPropagationComponentImpl(currentState);
@Override
public Tagger getTagger() {
@@ -46,12 +49,20 @@ public class TagsComponentImplBase extends TagsComponent {
@Override
public TaggingState getState() {
- return state.get();
+ return stateToTaggingState(currentState.get());
}
@Override
@Deprecated
public void setState(TaggingState newState) {
- state.set(checkNotNull(newState, "newState"));
+ currentState.set(taggingStateToState(checkNotNull(newState, "newState")));
+ }
+
+ private static State taggingStateToState(TaggingState taggingState) {
+ return taggingState == TaggingState.ENABLED ? State.ENABLED : State.DISABLED;
+ }
+
+ private static TaggingState stateToTaggingState(State state) {
+ return state == State.ENABLED ? TaggingState.ENABLED : TaggingState.DISABLED;
}
}
diff --git a/impl_core/src/main/java/io/opencensus/implcore/tags/propagation/TagContextBinarySerializerImpl.java b/impl_core/src/main/java/io/opencensus/implcore/tags/propagation/TagContextBinarySerializerImpl.java
index 781cdff0..5a25da5b 100644
--- a/impl_core/src/main/java/io/opencensus/implcore/tags/propagation/TagContextBinarySerializerImpl.java
+++ b/impl_core/src/main/java/io/opencensus/implcore/tags/propagation/TagContextBinarySerializerImpl.java
@@ -16,10 +16,10 @@
package io.opencensus.implcore.tags.propagation;
-import io.opencensus.implcore.tags.CurrentTaggingState;
+import io.opencensus.implcore.internal.CurrentState;
+import io.opencensus.implcore.internal.CurrentState.State;
import io.opencensus.implcore.tags.TagContextImpl;
import io.opencensus.tags.TagContext;
-import io.opencensus.tags.TaggingState;
import io.opencensus.tags.propagation.TagContextBinarySerializer;
import io.opencensus.tags.propagation.TagContextDeserializationException;
import io.opencensus.tags.propagation.TagContextSerializationException;
@@ -27,22 +27,22 @@ import io.opencensus.tags.propagation.TagContextSerializationException;
final class TagContextBinarySerializerImpl extends TagContextBinarySerializer {
private static final byte[] EMPTY_BYTE_ARRAY = {};
- private final CurrentTaggingState state;
+ private final CurrentState state;
- TagContextBinarySerializerImpl(CurrentTaggingState state) {
+ TagContextBinarySerializerImpl(CurrentState state) {
this.state = state;
}
@Override
public byte[] toByteArray(TagContext tags) throws TagContextSerializationException {
- return state.getInternal() == TaggingState.DISABLED
+ return state.getInternal() == State.DISABLED
? EMPTY_BYTE_ARRAY
: SerializationUtils.serializeBinary(tags);
}
@Override
public TagContext fromByteArray(byte[] bytes) throws TagContextDeserializationException {
- return state.getInternal() == TaggingState.DISABLED
+ return state.getInternal() == State.DISABLED
? TagContextImpl.EMPTY
: SerializationUtils.deserializeBinary(bytes);
}
diff --git a/impl_core/src/main/java/io/opencensus/implcore/tags/propagation/TagPropagationComponentImpl.java b/impl_core/src/main/java/io/opencensus/implcore/tags/propagation/TagPropagationComponentImpl.java
index 5def0758..42ada755 100644
--- a/impl_core/src/main/java/io/opencensus/implcore/tags/propagation/TagPropagationComponentImpl.java
+++ b/impl_core/src/main/java/io/opencensus/implcore/tags/propagation/TagPropagationComponentImpl.java
@@ -16,14 +16,14 @@
package io.opencensus.implcore.tags.propagation;
-import io.opencensus.implcore.tags.CurrentTaggingState;
+import io.opencensus.implcore.internal.CurrentState;
import io.opencensus.tags.propagation.TagContextBinarySerializer;
import io.opencensus.tags.propagation.TagPropagationComponent;
public final class TagPropagationComponentImpl extends TagPropagationComponent {
private final TagContextBinarySerializer tagContextBinarySerializer;
- public TagPropagationComponentImpl(CurrentTaggingState state) {
+ public TagPropagationComponentImpl(CurrentState state) {
tagContextBinarySerializer = new TagContextBinarySerializerImpl(state);
}