List of usage examples for com.google.common.collect Iterables getOnlyElement
public static <T> T getOnlyElement(Iterable<T> iterable)
From source file:com.google.errorprone.bugpatterns.time.JodaDurationWithMillis.java
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!MATCHER.matches(tree, state)) { return Description.NO_MATCH; }//from ww w. ja va 2 s . c om SuggestedFix.Builder builder = SuggestedFix.builder(); String replacement = SuggestedFixes.qualifyType(state, builder, "org.joda.time.Duration") + ".millis("; ExpressionTree millisArg = Iterables.getOnlyElement(tree.getArguments()); builder.replace(((JCTree) tree).getStartPosition(), ((JCTree) millisArg).getStartPosition(), replacement); return describeMatch(tree, builder.build()); }
From source file:org.jclouds.trmk.vcloud_0_8.location.OrgAndVDCToLocationSupplier.java
protected Builder<Location> buildJustProviderOrVDCs() { Builder<Location> locations = ImmutableSet.builder(); Location provider = Iterables.getOnlyElement(super.get()); if (orgNameToResource.get().size() == 0) return locations.add(provider); Map<String, Supplier<Set<String>>> isoCodesById = isoCodesByIdSupplier.get(); for (ReferenceType org : orgNameToResource.get().values()) { LocationBuilder builder = new LocationBuilder().scope(LocationScope.REGION) .id(org.getHref().toASCIIString()).description(org.getName()).parent(provider); if (isoCodesById.containsKey(org.getHref().toASCIIString())) builder.iso3166Codes(isoCodesById.get(org.getHref().toASCIIString()).get()); Location orgL = builder.build(); for (ReferenceType vdc : orgNameToVDCResource.get().get(org.getName()).getVDCs().values()) { builder = new LocationBuilder().scope(LocationScope.ZONE).id(vdc.getHref().toASCIIString()) .description(vdc.getName()).parent(orgL); if (isoCodesById.containsKey(vdc.getHref().toASCIIString())) builder.iso3166Codes(isoCodesById.get(vdc.getHref().toASCIIString()).get()); locations.add(builder.build()); }/*from ww w . j a v a2s. c o m*/ } return locations; }
From source file:com.opengamma.financial.analytics.model.black.BlackDiscountingForwardDeltaFXOptionFunction.java
@Override public CompiledFunctionDefinition compile(final FunctionCompilationContext context, final Instant atInstant) { return new BlackDiscountingCompiledFunction(getTargetToDefinitionConverter(context), getDefinitionToDerivativeConverter(context), false) { @Override/*from w w w. ja v a 2s.c o m*/ protected Set<ComputedValue> getValues(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues, final InstrumentDerivative derivative, final FXMatrix fxMatrix) { final BlackForexSmileProvider blackData = getBlackSurface(executionContext, inputs, target, fxMatrix); final double forwardDelta = derivative.accept(CALCULATOR, blackData); final ValueRequirement desiredValue = Iterables.getOnlyElement(desiredValues); final ValueProperties properties = desiredValue.getConstraints().copy().get(); final ValueSpecification spec = new ValueSpecification(FORWARD_DELTA, target.toSpecification(), properties); return Collections.singleton(new ComputedValue(spec, forwardDelta)); } }; }
From source file:com.opengamma.financial.analytics.model.black.BlackDiscountingForwardGammaFXOptionFunction.java
@Override public CompiledFunctionDefinition compile(final FunctionCompilationContext context, final Instant atInstant) { return new BlackDiscountingCompiledFunction(getTargetToDefinitionConverter(context), getDefinitionToDerivativeConverter(context), false) { @Override//from w ww . j a v a 2 s .c o m protected Set<ComputedValue> getValues(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues, final InstrumentDerivative derivative, final FXMatrix fxMatrix) { final BlackForexSmileProvider blackData = getBlackSurface(executionContext, inputs, target, fxMatrix); final double forwardGamma = derivative.accept(CALCULATOR, blackData); final ValueRequirement desiredValue = Iterables.getOnlyElement(desiredValues); final ValueProperties properties = desiredValue.getConstraints().copy().get(); final ValueSpecification spec = new ValueSpecification(FORWARD_GAMMA, target.toSpecification(), properties); return Collections.singleton(new ComputedValue(spec, forwardGamma)); } }; }
From source file:com.groupon.jenkins.extensions.DotCiExtensionsHelper.java
private <T extends DotCiExtension> List<T> createPlugins(List<?> pluginSpecs, Class<T> extensionClass) { if (Iterables.isEmpty(pluginSpecs)) { return Collections.emptyList(); }//www. java 2s. c o m List<T> plugins = new ArrayList<T>(pluginSpecs.size()); for (Object pluginSpec : pluginSpecs) { String pluginName; Object options; if (pluginSpec instanceof String) { pluginName = (String) pluginSpec; options = new HashMap<Object, Object>(); } else { // has to be a Map Map<String, Object> pluginSpecMap = (Map<String, Object>) pluginSpec; pluginName = Iterables.getOnlyElement(pluginSpecMap.keySet()); options = pluginSpecMap.get(pluginName); } plugins.add(create(pluginName, options, extensionClass)); } return plugins; }
From source file:dagger.internal.codegen.Scope.java
/** * Returns at most one associated scoped annotation from the source code element, throwing an * exception if there are more than one. *///from w w w . jav a 2s .com static Optional<Scope> uniqueScopeOf(Element element) { ImmutableSet<? extends AnnotationMirror> scopeAnnotations = getScopes(element); if (scopeAnnotations.isEmpty()) { return Optional.empty(); } return Optional.of(scope(Iterables.getOnlyElement(scopeAnnotations))); }
From source file:org.jclouds.rimuhosting.miro.location.RimuHostingLocationSupplier.java
@Override public Set<? extends Location> get() { Builder<Location> locations = ImmutableSet.builder(); Iterable<DataCenter> list = Iterables .filter(Iterables.transform(sync.getPricingPlanList(), new Function<PricingPlan, DataCenter>() { @Override//from ww w . j av a2 s . c o m public DataCenter apply(PricingPlan arg0) { return arg0.getDataCenter(); } }), Predicates.<DataCenter>notNull()); Location provider = Iterables.getOnlyElement(super.get()); if (Iterables.size(list) == 0) locations.add(provider); else { Map<String, Supplier<Set<String>>> isoCodesById = isoCodesByIdSupplier.get(); for (DataCenter from : list) { LocationBuilder builder = new LocationBuilder().scope(LocationScope.ZONE).id(from.getId()) .description(from.getName()).parent(provider); if (isoCodesById.containsKey(from.getId())) builder.iso3166Codes(isoCodesById.get(from.getId()).get()); locations.add(builder.build()); } } return locations.build(); }
From source file:com.opengamma.financial.analytics.model.discounting.DiscountingInflationPVFunction.java
@Override public CompiledFunctionDefinition compile(final FunctionCompilationContext context, final Instant atInstant) { return new DiscountingInflationCompiledFunction(getTargetToDefinitionConverter(context), getDefinitionToDerivativeConverter(context), true) { @Override/*from w ww.j a v a 2 s .c om*/ protected Set<ComputedValue> getValues(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues, final InstrumentDerivative derivative, final FXMatrix fxMatrix) { final InflationProviderInterface data = (InflationProviderInterface) inputs.getValue(CURVE_BUNDLE); final ValueRequirement desiredValue = Iterables.getOnlyElement(desiredValues); final ValueProperties properties = desiredValue.getConstraints().copy().get(); final Currency currency = FinancialSecurityUtils.getCurrency(target.getTrade().getSecurity()); final MultipleCurrencyAmount mca = derivative.accept(CALCULATOR, data); final ValueSpecification spec = new ValueSpecification(PRESENT_VALUE, target.toSpecification(), properties); return Collections.singleton(new ComputedValue(spec, mca.getAmount(currency))); } }; }
From source file:com.opengamma.financial.analytics.model.discounting.DiscountingInflationNetAmountFunction.java
@Override public CompiledFunctionDefinition compile(final FunctionCompilationContext context, final Instant atInstant) { return new DiscountingInflationCompiledFunction(getTargetToDefinitionConverter(context), getDefinitionToDerivativeConverter(context), true) { @Override//from w ww .j a v a 2 s . co m protected Set<ComputedValue> getValues(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues, final InstrumentDerivative derivative, final FXMatrix fxMatrix) { final InflationProviderInterface data = (InflationProviderInterface) inputs.getValue(CURVE_BUNDLE); final ValueRequirement desiredValue = Iterables.getOnlyElement(desiredValues); final ValueProperties properties = desiredValue.getConstraints().copy().get(); final Currency currency = FinancialSecurityUtils.getCurrency(target.getTrade().getSecurity()); final MultipleCurrencyAmount mca = derivative.accept(CALCULATOR, data); final ValueSpecification spec = new ValueSpecification(INFLATION_NET_AMOUNT, target.toSpecification(), properties); return Collections.singleton(new ComputedValue(spec, mca.getAmount(currency))); } }; }
From source file:org.jclouds.cache.CacheServerInstaller.java
private NodeMetadata getCacheServer(final Template template) throws RunNodesException { try {//w ww. j av a2s .c om return Iterables.getOnlyElement( ctx.getComputeService().listNodesDetailsMatching(new Predicate<ComputeMetadata>() { @Override public boolean apply(ComputeMetadata input) { checkArgument(input instanceof NodeMetadata); NodeMetadata node = (NodeMetadata) input; return node.getGroup().startsWith(CACHE_SERVER_GROUP) && node.getGroup().endsWith( template.getImage().getOperatingSystem().getFamily().name().toLowerCase()); } })); } catch (NoSuchElementException e) { logger.info("No cache server was found. Creating a new one [Template: %s ]", template); NodeMetadata node = Iterables.getOnlyElement(ctx.getComputeService().createNodesInGroup( CACHE_SERVER_GROUP + "-" + template.getImage().getOperatingSystem().getFamily().name().toLowerCase(), 1, template)); logger.info("New cache server node created: %s ", node); ctx.getComputeService().runScriptOnNode(node.getId(), AdminAccess.standard()); return node; } catch (IllegalArgumentException e) { throw new IllegalStateException("Found multiple cache servers!"); } }