Skip to content

Commit

Permalink
Tidy up some usages of lambdas and other Java 8 features, and a few o…
Browse files Browse the repository at this point in the history
…ther tweaks.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=267686084
  • Loading branch information
dimo414 authored and cgdecker committed Sep 13, 2019
1 parent 2f7afd6 commit 134db35
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 75 deletions.
18 changes: 8 additions & 10 deletions core/src/com/google/inject/internal/AbstractBindingProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,23 @@ protected void prepareBinding() {
* initialially processed.
*/
protected void scheduleInitialization(BindingImpl<?> binding) {
bindingData.addUninitializedBinding(asRunnable(binding));
bindingData.addUninitializedBinding(() -> initializeBinding(binding));
}

/**
* Schedule initialization for this binding to occur after all other static initialization of
* bindings.
*/
protected void scheduleDelayedInitialization(BindingImpl<?> binding) {
bindingData.addDelayedUninitializedBinding(asRunnable(binding));
bindingData.addDelayedUninitializedBinding(() -> initializeBinding(binding));
}

private Runnable asRunnable(final BindingImpl<?> binding) {
return () -> {
try {
binding.getInjector().initializeBinding(binding, errors.withSource(source));
} catch (ErrorsException e) {
errors.merge(e.getErrors());
}
};
private void initializeBinding(BindingImpl<?> binding) {
try {
binding.getInjector().initializeBinding(binding, errors.withSource(source));
} catch (ErrorsException e) {
errors.merge(e.getErrors());
}
}
}
}
15 changes: 7 additions & 8 deletions core/src/com/google/inject/internal/RealMapBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,10 @@ private boolean tryInitialize(InjectorImpl injector, Errors errors) {

// Don't do extra work unless we need to
if (permitsDuplicates) {
// Create a set builder for this key if it's the first time we've seen it
bindingMultimapMutable.computeIfAbsent(key, k -> ImmutableSet.builder());

// Add the Binding<V>
bindingMultimapMutable.get(key).add(valueBinding);
// Add the binding, creating a set builder if it's the first time we've seen it
bindingMultimapMutable
.computeIfAbsent(key, k -> ImmutableSet.builder())
.add(valueBinding);
}
}
}
Expand Down Expand Up @@ -518,13 +517,13 @@ private static <K, V> void reportDuplicateKeysError(

if (first) {
first = false;
sb.append("\"" + dupKey + "\", from bindings:\n");
sb.append("\"").append(dupKey).append("\", from bindings:\n");
} else {
sb.append("\n and key: \"" + dupKey + "\", from bindings:\n");
sb.append("\n and key: \"").append(dupKey).append("\", from bindings:\n");
}

for (Binding<V> dup : entry.getValue()) {
sb.append("\t at " + Errors.convert(dup.getSource()) + "\n");
sb.append("\t at ").append(Errors.convert(dup.getSource())).append("\n");
}
}

Expand Down
23 changes: 7 additions & 16 deletions core/src/com/google/inject/internal/WeakKeySet.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,15 @@ final class WeakKeySet {
* garbage collected.
*/
private final Cache<State, Set<KeyAndSource>> evictionCache =
CacheBuilder.newBuilder()
.weakKeys()
.removalListener(
(RemovalNotification<State, Set<KeyAndSource>> notification) -> {
Preconditions.checkState(RemovalCause.COLLECTED.equals(notification.getCause()));
CacheBuilder.newBuilder().weakKeys().removalListener(this::cleanupOnRemoval).build();

cleanUpForCollectedState(notification.getValue());
})
.build();
private void cleanupOnRemoval(RemovalNotification<State, Set<KeyAndSource>> notification) {
Preconditions.checkState(RemovalCause.COLLECTED.equals(notification.getCause()));

/**
* There may be multiple child injectors blacklisting a certain key so only remove the source
* that's relevant.
*/
private void cleanUpForCollectedState(Set<KeyAndSource> keysAndSources) {
// There may be multiple child injectors blacklisting a certain key so only remove the source
// that's relevant.
synchronized (lock) {
for (KeyAndSource keyAndSource : keysAndSources) {
for (KeyAndSource keyAndSource : notification.getValue()) {
Multiset<Object> set = backingMap.get(keyAndSource.key);
if (set != null) {
set.remove(keyAndSource.source);
Expand All @@ -92,9 +84,8 @@ public void add(Key<?> key, State state, Object source) {
if (source instanceof Class || source == SourceProvider.UNKNOWN_SOURCE) {
source = null;
}
Multiset<Object> sources = backingMap.computeIfAbsent(key, k -> LinkedHashMultiset.create());
Object convertedSource = Errors.convert(source);
sources.add(convertedSource);
backingMap.computeIfAbsent(key, k -> LinkedHashMultiset.create()).add(convertedSource);

// Avoid all the extra work if we can.
if (state.parent() != State.NONE) {
Expand Down
4 changes: 1 addition & 3 deletions core/src/com/google/inject/spi/InjectionPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,7 @@ void add(InjectableMethod injectableMethod) {
injectableMethod.method == lastMethod
? lastSignature
: new Signature(injectableMethod.method);
List<InjectableMethod> methods =
bySignature.computeIfAbsent(signature, k -> new ArrayList<>());
methods.add(injectableMethod);
bySignature.computeIfAbsent(signature, k -> new ArrayList<>()).add(injectableMethod);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/com/google/inject/util/Modules.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ public <T> Void visit(Binding<T> binding) {
// Record when a scope instance is used in a binding
Scope scope = getScopeInstanceOrNull(binding);
if (scope != null) {
List<Object> existing =
scopeInstancesInUse.computeIfAbsent(scope, k -> Lists.newArrayList());
existing.add(binding.getSource());
scopeInstancesInUse
.computeIfAbsent(scope, k -> Lists.newArrayList())
.add(binding.getSource());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Future<State> start() {
tasks.add(injector.getInstance(service).start());
}

return futureGet(tasks, State.STARTED);
return new FutureTask<>(() -> waitForTasks(tasks, State.STARTED));
}

@Override
Expand All @@ -94,7 +94,7 @@ public Future<State> stop() {
tasks.add(injector.getInstance(service).stop());
}

return futureGet(tasks, State.STOPPED);
return new FutureTask<>(() -> waitForTasks(tasks, State.STOPPED));
}

@Override
Expand All @@ -104,22 +104,19 @@ public State state() {
};
}

private FutureTask<Service.State> futureGet(
final List<Future<Service.State>> tasks, final Service.State state) {
return new FutureTask<>(
() -> {
boolean ok = true;
for (Future<Service.State> task : tasks) {
try {
ok = state == task.get();
} catch (InterruptedException e) {
return compositeState = Service.State.FAILED;
} catch (ExecutionException e) {
return compositeState = Service.State.FAILED;
}
}

return compositeState = ok ? state : Service.State.FAILED;
});
private Service.State waitForTasks(List<Future<Service.State>> tasks, Service.State state) {
boolean ok = true;
for (Future<Service.State> task : tasks) {
try {
ok = state == task.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return compositeState = Service.State.FAILED;
} catch (ExecutionException e) {
return compositeState = Service.State.FAILED;
}
}

return compositeState = ok ? state : Service.State.FAILED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,12 @@ private static Object validateAndCanonicalizeValue(Key<?> key, Object object) {
return NullObject.INSTANCE;
}

if (!key.getTypeLiteral().getRawType().isInstance(object)) {
throw new IllegalArgumentException(
"Value["
+ object
+ "] of type["
+ object.getClass().getName()
+ "] is not compatible with key["
+ key
+ "]");
}
Preconditions.checkArgument(
key.getTypeLiteral().getRawType().isInstance(object),
"Value[%s] of type[%s] is not compatible with key[%s]",
object,
object.getClass().getName(),
key);

return object;
}
Expand Down Expand Up @@ -429,14 +425,10 @@ private static void checkScopingState(boolean condition, String msg) {
}
}

private static final <T> Callable<T> wrap(
final Callable<T> delegate, final RequestScoper requestScoper) {
private static <T> Callable<T> wrap(Callable<T> delegate, RequestScoper requestScoper) {
return () -> {
RequestScoper.CloseableScope scope = requestScoper.open();
try {
try (RequestScoper.CloseableScope scope = requestScoper.open()) {
return delegate.call();
} finally {
scope.close();
}
};
}
Expand Down

0 comments on commit 134db35

Please sign in to comment.