Skip to content

Commit

Permalink
Polishing contribution
Browse files Browse the repository at this point in the history
Closes gh-98
  • Loading branch information
rstoyanchev committed May 11, 2023
1 parent a1dc613 commit fcbab16
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,82 +160,85 @@ static ContextSnapshot captureAllUsing(Predicate<Object> keyPredicate, ContextRe
return DefaultContextSnapshot.captureAll(registry, keyPredicate, contexts);
}

/**
* Create a {@link ContextSnapshot} by reading values from the given context object.
* @param context the context to read values from
* @return the created {@link ContextSnapshot}
* @deprecated as of 1.0.3 in favor of {@link #captureFromContext(Object...)}
*/
@Deprecated
static ContextSnapshot captureFrom(Object context) {
return captureFrom(context, ContextRegistry.getInstance());
}

/**
* Create a {@link ContextSnapshot} by reading values from the given context objects.
* <p>
* Values captured multiple times are overridden in the snapshot by the order of
* contexts given as arguments.
* @param contexts the contexts to read values from
* @return the created {@link ContextSnapshot}
* @since 1.0.3
*/
static ContextSnapshot captureFromContext(Object... contexts) {
return DefaultContextSnapshot.captureFromContexts(key -> true, ContextRegistry.getInstance(), contexts);
return DefaultContextSnapshot.captureFromContext(key -> true, ContextRegistry.getInstance(), null, contexts);
}

/**
* Create a {@link ContextSnapshot} by reading values from the given context object.
* @param context the context to read values from
* Create a {@link ContextSnapshot} by reading values from the given context objects.
* <p>
* Values captured multiple times are overridden in the snapshot by the order of
* contexts given as arguments.
* @param registry the registry to use
* @param contexts the contexts to read values from
* @return the created {@link ContextSnapshot}
* @deprecated as of 1.0.3 in favor of
* {@link #captureFromContext(ContextRegistry, Object...)}
* @since 1.0.3
*/
@Deprecated
static ContextSnapshot captureFrom(Object context, ContextRegistry registry) {
return DefaultContextSnapshot.captureFromContext(key -> true, registry, context, null);
static ContextSnapshot captureFromContext(ContextRegistry registry, Object... contexts) {
return DefaultContextSnapshot.captureFromContext(key -> true, registry, null, contexts);
}

/**
* Create a {@link ContextSnapshot} by reading values from the given context objects.
* <p>
* Values captured multiple times are overridden in the snapshot by the order of
* contexts given as arguments.
* @param keyPredicate predicate for context value keys
* @param registry the registry to use
* @param contexts the contexts to read values from
* @return the created {@link ContextSnapshot}
* @since 1.0.3
*/
static ContextSnapshot captureFromContext(ContextRegistry registry, Object... contexts) {
return DefaultContextSnapshot.captureFromContexts(key -> true, registry, contexts);
static ContextSnapshot captureFromContext(Predicate<Object> keyPredicate, ContextRegistry registry,
Object... contexts) {
return DefaultContextSnapshot.captureFromContext(keyPredicate, registry, null, contexts);
}

/**
* Create a {@link ContextSnapshot} by reading values from the given context object.
* @param context the context to read values from
* @return the created {@link ContextSnapshot}
* @deprecated as of 1.0.3 in favor of {@link #captureFromContext(Object...)}
*/
@Deprecated
static ContextSnapshot captureFrom(Object context) {
return captureFrom(context, ContextRegistry.getInstance());
}

/**
* Create a {@link ContextSnapshot} by reading values from the given context object.
* @param context the context to read values from
* @param keyPredicate predicate for context value keys
* @param registry the registry to use
* @return the created {@link ContextSnapshot}
* @deprecated as of 1.0.3 in favor of
* {@link #captureFromContext(Predicate, ContextRegistry, Object...)}
* {@link #captureFromContext(ContextRegistry, Object...)}
*/
@Deprecated
static ContextSnapshot captureFrom(Object context, Predicate<Object> keyPredicate, ContextRegistry registry) {
return DefaultContextSnapshot.captureFromContext(keyPredicate, registry, context, null);
static ContextSnapshot captureFrom(Object context, ContextRegistry registry) {
return DefaultContextSnapshot.captureFromContext(key -> true, registry, null, context);
}

/**
* Create a {@link ContextSnapshot} by reading values from the given context objects.
* <p>
* Values captured multiple times are overridden in the snapshot by the order of
* contexts given as arguments.
* Create a {@link ContextSnapshot} by reading values from the given context object.
* @param context the context to read values from
* @param keyPredicate predicate for context value keys
* @param registry the registry to use
* @param contexts the contexts to read values from
* @return the created {@link ContextSnapshot}
* @deprecated as of 1.0.3 in favor of
* {@link #captureFromContext(Predicate, ContextRegistry, Object...)}
*/
static ContextSnapshot captureFromContext(Predicate<Object> keyPredicate, ContextRegistry registry,
Object... contexts) {
return DefaultContextSnapshot.captureFromContexts(keyPredicate, registry, contexts);
@Deprecated
static ContextSnapshot captureFrom(Object context, Predicate<Object> keyPredicate, ContextRegistry registry) {
return DefaultContextSnapshot.captureFromContext(keyPredicate, registry, null, context);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
*/
final class DefaultContextSnapshot extends HashMap<Object, Object> implements ContextSnapshot {

private static final ContextSnapshot emptyContextSnapshot = new DefaultContextSnapshot(new ContextRegistry());
private static final DefaultContextSnapshot emptyContextSnapshot = new DefaultContextSnapshot(
new ContextRegistry());

private final ContextRegistry contextRegistry;

Expand Down Expand Up @@ -130,7 +131,7 @@ static ContextSnapshot captureAll(ContextRegistry contextRegistry, Predicate<Obj

DefaultContextSnapshot snapshot = captureFromThreadLocals(keyPredicate, contextRegistry);
for (Object context : contexts) {
snapshot = captureFromContext(keyPredicate, contextRegistry, context, snapshot);
snapshot = captureFromContext(keyPredicate, contextRegistry, snapshot, context);
}
return (snapshot != null ? snapshot : emptyContextSnapshot);
}
Expand All @@ -152,23 +153,16 @@ private static DefaultContextSnapshot captureFromThreadLocals(Predicate<Object>
return snapshot;
}

static ContextSnapshot captureFromContexts(Predicate<Object> keyPredicate, ContextRegistry contextRegistry,
Object... contexts) {
DefaultContextSnapshot snapshot = null;
for (Object context : contexts) {
snapshot = captureFromContext(keyPredicate, contextRegistry, context, snapshot);
}
return (snapshot != null ? snapshot : emptyContextSnapshot);
}

@SuppressWarnings("unchecked")
static DefaultContextSnapshot captureFromContext(Predicate<Object> keyPredicate, ContextRegistry contextRegistry,
Object context, @Nullable DefaultContextSnapshot snapshot) {
@Nullable DefaultContextSnapshot snapshot, Object... contexts) {

ContextAccessor<?, ?> accessor = contextRegistry.getContextAccessorForRead(context);
snapshot = (snapshot != null ? snapshot : new DefaultContextSnapshot(contextRegistry));
((ContextAccessor<Object, ?>) accessor).readValues(context, keyPredicate, snapshot);
return snapshot;
for (Object context : contexts) {
ContextAccessor<?, ?> accessor = contextRegistry.getContextAccessorForRead(context);
snapshot = (snapshot != null ? snapshot : new DefaultContextSnapshot(contextRegistry));
((ContextAccessor<Object, ?>) accessor).readValues(context, keyPredicate, snapshot);
}
return (snapshot != null ? snapshot : emptyContextSnapshot);
}

@Override
Expand Down

0 comments on commit fcbab16

Please sign in to comment.