下面列出了怎么用com.google.common.util.concurrent.Atomics的API类实例代码及写法,或者点击链接到github查看源代码。
@Nullable
private String queryMountPoint(@NonNull final String name)
throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
IOException {
final AtomicReference<String> ref = Atomics.newReference();
executeShellCommand("echo $" + name, new MultiLineReceiver() { //$NON-NLS-1$
@Override
public boolean isCancelled() {
return false;
}
@Override
public void processNewLines(String[] lines) {
for (String line : lines) {
if (!line.isEmpty()) {
// this should be the only one.
ref.set(line);
}
}
}
});
return ref.get();
}
@Provides
@Singleton
protected Supplier<CacheLoader<RegionAndName, Image>> provideRegionAndNameToImageSupplierCacheLoader(
final RegionAndIdToImage delegate) {
return Suppliers.<CacheLoader<RegionAndName, Image>>ofInstance(new CacheLoader<RegionAndName, Image>() {
private final AtomicReference<AuthorizationException> authException = Atomics.newReference();
@Override
public Image load(final RegionAndName key) throws Exception {
// raw lookup of an image
Supplier<Image> rawSupplier = new Supplier<Image>() {
@Override public Image get() {
try {
return delegate.load(key);
} catch (ExecutionException e) {
throw Throwables.propagate(e);
}
}
};
return new SetAndThrowAuthorizationExceptionSupplier<Image>(rawSupplier, authException).get();
}
});
}
private Operation waitGCEOperationDone(Operation operation) {
IaasProvider iaasInfo = getIaasProvider();
Injector injector = ContextBuilder.newBuilder(iaasInfo.getProvider())
.credentials(iaasInfo.getIdentity(), iaasInfo.getCredential())
.buildInjector();
Predicate<AtomicReference<Operation>> zoneOperationDonePredicate =
injector.getInstance(Key.get(new TypeLiteral<Predicate<AtomicReference<Operation>>>() {
}, Names.named("zone")));
AtomicReference<Operation> operationReference = Atomics.newReference(operation);
retry(zoneOperationDonePredicate, MAX_WAIT_TIME, 1, SECONDS).apply(operationReference);
return operationReference.get();
}
@Inject
RecoveryImpl(
File backupDir,
Function<Snapshot, TemporaryStorage> tempStorageFactory,
Storage primaryStorage,
SnapshotStore snapshotStore,
Command shutDownNow) {
this.backupDir = requireNonNull(backupDir);
this.tempStorageFactory = requireNonNull(tempStorageFactory);
this.recovery = Atomics.newReference();
this.primaryStorage = requireNonNull(primaryStorage);
this.snapshotStore = requireNonNull(snapshotStore);
this.shutDownNow = requireNonNull(shutDownNow);
}
private static FlatTrace.Context handleSelfDestruct(
final TraceFrame traceFrame,
final Deque<FlatTrace.Context> tracesContexts,
final FlatTrace.Context currentContext,
final List<FlatTrace.Builder> flatTraces) {
final Action.Builder actionBuilder = currentContext.getBuilder().getActionBuilder();
final Gas gasUsed =
Gas.fromHexString(actionBuilder.getGas())
.minus(traceFrame.getGasRemaining())
.plus(traceFrame.getGasCost().orElse(Gas.ZERO));
currentContext.setGasUsed(gasUsed.toLong());
final Bytes32[] stack = traceFrame.getStack().orElseThrow();
final Address refundAddress = toAddress(stack[stack.length - 1]);
final FlatTrace.Builder subTraceBuilder =
FlatTrace.builder()
.type("suicide")
.traceAddress(calculateSelfDescructAddress(tracesContexts));
final AtomicReference<Wei> weiBalance = Atomics.newReference(Wei.ZERO);
traceFrame
.getMaybeRefunds()
.ifPresent(refunds -> weiBalance.set(refunds.getOrDefault(refundAddress, Wei.ZERO)));
final Action.Builder callingAction = tracesContexts.peekLast().getBuilder().getActionBuilder();
final String actionAddress =
getActionAddress(callingAction, traceFrame.getRecipient().toHexString());
final Action.Builder subTraceActionBuilder =
Action.builder()
.address(actionAddress)
.refundAddress(refundAddress.toString())
.balance(TracingUtils.weiAsHex(weiBalance.get()));
flatTraces.add(
new FlatTrace.Context(subTraceBuilder.actionBuilder(subTraceActionBuilder)).getBuilder());
final FlatTrace.Context lastContext = tracesContexts.removeLast();
lastContext.getBuilder().incSubTraces();
final FlatTrace.Context nextContext = tracesContexts.peekLast();
if (nextContext != null) {
nextContext.getBuilder().incSubTraces();
}
return nextContext;
}