Skip to content
Permalink
Browse files

Tidy up some usages of lambdas and other Java 8 features, and a few o…

…ther tweaks.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=267686084
  • Loading branch information
dimo414 authored and cgdecker committed Sep 6, 2019
1 parent 2f7afd6 commit 134db35272998e73ded3c6ab36209d0a2f8906b9
@@ -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());
}
}
}
}
@@ -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);
}
}
}
@@ -518,13 +517,13 @@ private boolean tryInitialize(InjectorImpl injector, Errors errors) {

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");
}
}

@@ -51,23 +51,15 @@
* 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);
@@ -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) {
@@ -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);
}
}
}
@@ -277,9 +277,9 @@ public Void visit(PrivateElements privateElements) {
// 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());
}
}

@@ -84,7 +84,7 @@ public Service compose() {
tasks.add(injector.getInstance(service).start());
}

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

@Override
@@ -94,7 +94,7 @@ public Service compose() {
tasks.add(injector.getInstance(service).stop());
}

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

@Override
@@ -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;
}
}
@@ -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;
}
@@ -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();
}
};
}

0 comments on commit 134db35

Please sign in to comment.
You can’t perform that action at this time.