Skip to content

Commit 6eb1899

Browse files
authored
Remove Context.getValue() method. (#528)
* Remove Context.getValue() method which should not be necessary. * Add readme about open telemetry manager. --------- Signed-off-by: Sjoerd Talsma <sjoerdtalsma@users.noreply.github.com>
1 parent 725f158 commit 6eb1899

File tree

19 files changed

+32
-104
lines changed

19 files changed

+32
-104
lines changed

context-propagation-api/src/main/java/nl/talsmasoftware/context/api/Context.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,6 @@
4646
*/
4747
public interface Context<T> extends Closeable {
4848

49-
// TODO think about removing Context.getValue as ContextManager.getActiveContextValue() should suffice.
50-
51-
/**
52-
* The value associated with this context.
53-
*
54-
* <p>
55-
* Behaviour is not specified after the context is {@linkplain #close() closed} and depends
56-
* on the {@linkplain ContextManager} implementation.<br>
57-
*
58-
* @return The value associated with this context.
59-
*/
60-
T getValue();
61-
6249
/**
6350
* Closes this context.
6451
*

context-propagation-api/src/main/java/nl/talsmasoftware/context/api/ContextManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public interface ContextManager<T> {
4444
* The value of the currently active context, or {@code null} if no context is active.
4545
*
4646
* @return The value of the active context, or {@code null} if no context is active.
47-
* @see Context#getValue()
4847
* @since 2.0.0
4948
*/
5049
T getActiveContextValue();

context-propagation-api/src/main/java/nl/talsmasoftware/context/api/package-info.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
*
1919
* <h2>{@linkplain nl.talsmasoftware.context.api.Context}</h2>
2020
* <p>
21-
* A {@linkplain nl.talsmasoftware.context.api.Context context} contains
22-
* a {@linkplain nl.talsmasoftware.context.api.Context#getValue() value}.<br>
21+
* A {@linkplain nl.talsmasoftware.context.api.Context context} contains a value.<br>
2322
* There can be one active context per thread. A context remains active until it is closed or another context
2423
* is activated in the same thread. Normally, a context is backed by a {@linkplain java.lang.ThreadLocal ThreadLocal}
2524
* variable.

context-propagation-api/src/test/java/nl/talsmasoftware/context/dummy/DummyContext.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ public boolean isClosed() {
4141
return closed.get();
4242
}
4343

44-
public String getValue() {
45-
return value;
46-
}
47-
4844
public void close() {
4945
if (closed.compareAndSet(false, true) && INSTANCE.get() == this) {
5046
DummyContext current = INSTANCE.get();
@@ -60,8 +56,8 @@ public void close() {
6056
}
6157

6258
public static String currentValue() {
63-
final Context<String> currentContext = INSTANCE.get();
64-
return currentContext != null ? currentContext.getValue() : null;
59+
final DummyContext currentContext = INSTANCE.get();
60+
return currentContext != null ? currentContext.value : null;
6561
}
6662

6763
public static void setCurrentValue(String value) {

context-propagation-api/src/test/java/nl/talsmasoftware/context/dummy/ThrowingContextManager.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@ private Ctx(String newValue) {
8585
STORAGE.set(this);
8686
}
8787

88-
@Override
89-
public String getValue() {
90-
return value;
91-
}
92-
9388
@Override
9489
public void close() {
9590
if (onClose != null) try {
@@ -112,7 +107,7 @@ public void close() {
112107

113108
private static String currentValue() {
114109
Ctx current = STORAGE.get();
115-
return current != null ? current.getValue() : null;
110+
return current != null ? current.value : null;
116111
}
117112

118113
private static void remove() {

context-propagation-core/src/main/java/nl/talsmasoftware/context/core/threadlocal/AbstractThreadLocalContext.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ public abstract class AbstractThreadLocalContext<T> implements Context<T> {
5656
protected final Context<T> parentContext;
5757

5858
/**
59-
* The actual value, so subclasses can still access it after the context has been closed,
60-
* because the default {@link #getValue()} implementation will return <code>null</code> in that case.<br>
59+
* The actual value, so subclasses can access it.<br>
6160
* Please be careful accessing the value after the context was closed:
62-
* There is no pre-defined meaningful way to handle this situation, as this depends heavily
63-
* on the desired features of the particular implementation.
61+
* There is no pre-defined meaningful way to handle this situation,
62+
* as this depends heavily on the desired features of the particular implementation.
6463
*/
6564
protected final T value;
6665

@@ -107,15 +106,6 @@ protected boolean isClosed() {
107106
return closed;
108107
}
109108

110-
/**
111-
* Returns the value of this context instance, or <code>null</code> if it was already closed.
112-
*
113-
* @return The value of this context.
114-
*/
115-
public T getValue() {
116-
return closed ? null : value;
117-
}
118-
119109
/**
120110
* Closes this context and in case this context is the active context,
121111
* restores the active context to the (unclosed) parent context.<br>
@@ -131,7 +121,7 @@ public void close() {
131121

132122
/**
133123
* Returns the classname of this context followed by <code>"{closed}"</code> if it has been closed already;
134-
* otherwise the contained {@link #getValue() value} by this context will be added.
124+
* otherwise the contained value by this context will be added.
135125
*
136126
* @return String representing this context class and either the current value or the fact that it was closed.
137127
*/

context-propagation-core/src/test/java/nl/talsmasoftware/context/dummy/DummyContext.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package nl.talsmasoftware.context.dummy;
1717

18-
import nl.talsmasoftware.context.api.Context;
1918
import nl.talsmasoftware.context.core.threadlocal.AbstractThreadLocalContext;
2019

2120
/**
@@ -35,8 +34,8 @@ public boolean isClosed() {
3534
}
3635

3736
public static String currentValue() {
38-
final Context<String> currentContext = INSTANCE.get();
39-
return currentContext != null ? currentContext.getValue() : null;
37+
final DummyContext currentContext = INSTANCE.get();
38+
return currentContext != null ? currentContext.value : null;
4039
}
4140

4241
public static void setCurrentValue(String value) {

context-propagation-core/src/test/java/nl/talsmasoftware/context/dummy/ThrowingContextManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private Ctx(String newValue) {
7777

7878
private static String currentValue() {
7979
Ctx current = AbstractThreadLocalContext.current(Ctx.class);
80-
return current != null ? current.getValue() : null;
80+
return current != null ? current.value : null;
8181
}
8282

8383
private static void remove() {

managers/context-manager-locale/src/main/java/nl/talsmasoftware/context/managers/locale/CurrentLocaleHolder.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static Context<Locale> set(Locale locale) {
115115
* @see #getOrDefault()
116116
*/
117117
public static Optional<Locale> get() {
118-
return Optional.ofNullable(LOCALE.get()).map(CurrentLocaleHolder::getValue);
118+
return Optional.ofNullable(LOCALE.get()).map(holder -> holder.locale);
119119
}
120120

121121
/**
@@ -130,16 +130,6 @@ public static Locale getOrDefault() {
130130
return get().orElseGet(Locale::getDefault);
131131
}
132132

133-
/**
134-
* The locale in this holder.
135-
*
136-
* @return The locale in this holder.
137-
*/
138-
@Override
139-
public Locale getValue() {
140-
return locale;
141-
}
142-
143133
/**
144134
* Find the nearest unclosed holder from this holder.
145135
*

managers/context-manager-locale/src/test/java/nl/talsmasoftware/context/managers/locale/CurrentLocaleHolderTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,18 @@ void verifyPropagationIntoNewThread() throws InterruptedException {
5151
void outOfSequenceClosingMustBeSupported() {
5252
// ctx1 = GERMAN
5353
Context<Locale> ctx1 = CurrentLocaleHolder.set(Locale.GERMAN);
54-
assertThat(ctx1.getValue()).isEqualTo(Locale.GERMAN);
5554
assertThat(CurrentLocaleHolder.getOrDefault()).isEqualTo(Locale.GERMAN);
5655

5756
// ctx2 = ENGLISH
5857
Context<Locale> ctx2 = CurrentLocaleHolder.set(Locale.ENGLISH);
59-
assertThat(ctx2.getValue()).isEqualTo(Locale.ENGLISH);
6058
assertThat(CurrentLocaleHolder.getOrDefault()).isEqualTo(Locale.ENGLISH);
6159

6260
// out-of-sequence, ctx1 is closed first.
6361
ctx1.close();
64-
assertThat(ctx1.getValue()).isEqualTo(Locale.GERMAN);
65-
assertThat(ctx2.getValue()).isEqualTo(Locale.ENGLISH);
6662
assertThat(CurrentLocaleHolder.getOrDefault()).isEqualTo(Locale.ENGLISH);
6763

6864
// when ctx2 is closed, because ctx1 was already closed, the remaining context is empty
6965
ctx2.close();
70-
assertThat(ctx1.getValue()).isEqualTo(Locale.GERMAN);
71-
assertThat(ctx2.getValue()).isEqualTo(Locale.ENGLISH);
7266
assertThat(CurrentLocaleHolder.get()).isEmpty();
7367
}
7468

@@ -83,7 +77,6 @@ void testToString_closed() {
8377
Context<Locale> closedContext = CurrentLocaleHolder.set(Locale.ENGLISH);
8478
closedContext.close();
8579

86-
assertThat(closedContext.getValue()).isEqualTo(Locale.ENGLISH);
8780
assertThat(closedContext).hasToString("CurrentLocaleHolder{closed}");
8881
}
8982

0 commit comments

Comments
 (0)