List of usage examples for com.google.common.collect Iterables getOnlyElement
public static <T> T getOnlyElement(Iterable<T> iterable)
From source file:org.jclouds.gogrid.compute.strategy.GoGridRebootNodeStrategy.java
@Override public NodeMetadata execute(String id) { Server server = Iterables.getOnlyElement(client.getServerServices().getServersById(new Long(id))); client.getServerServices().power(server.getName(), PowerCommand.RESTART); serverLatestJobCompleted.apply(server); client.getServerServices().power(server.getName(), PowerCommand.START); serverLatestJobCompletedShort.apply(server); return getNode.execute(id); }
From source file:com.opengamma.financial.analytics.model.curve.forward.InstantaneousForwardCurveFunction.java
@Override public Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) throws AsynchronousExecution { final YieldAndDiscountCurve curve = (YieldAndDiscountCurve) inputs.getValue(YIELD_CURVE); final int n = 1000; final double[] xData = new double[n]; final double[] yData = new double[n]; for (int i = 0; i < n; i++) { final double t = i / 40.; xData[i] = t;//from w ww.j a va2 s .c om yData[i] = curve.getForwardRate(t); } final NodalDoublesCurve forwardCurve = NodalDoublesCurve.from(xData, yData); final ValueProperties properties = Iterables.getOnlyElement(desiredValues).getConstraints(); final ValueSpecification spec = new ValueSpecification(INSTANTANEOUS_FORWARD_CURVE, target.toSpecification(), properties); return Collections.singleton(new ComputedValue(spec, forwardCurve)); }
From source file:com.opengamma.financial.analytics.model.equity.varianceswap.EquityForwardFromSpotAndYieldCurveFunction.java
@Override public Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) { final ValueRequirement desiredValue = Iterables.getOnlyElement(desiredValues); final String curveName = desiredValue.getConstraint(ValuePropertyNames.CURVE); final String curveCalculationConfig = desiredValue .getConstraint(ValuePropertyNames.CURVE_CALCULATION_CONFIG); // 1. Get the expiry _time_ from the trade final EquityVarianceSwapSecurity security = (EquityVarianceSwapSecurity) target.getSecurity(); final double expiry = TimeCalculator.getTimeBetween(ZonedDateTime.now(executionContext.getValuationClock()), security.getLastObservationDate()); // 2. Get the discount curve and spot value final Object discountObject = inputs .getValue(getDiscountRequirement(security, curveName, curveCalculationConfig)); if (discountObject == null) { throw new OpenGammaRuntimeException("Could not get Discount Curve"); }/*from w w w . ja v a2s. c o m*/ final YieldAndDiscountCurve discountCurve = (YieldAndDiscountCurve) discountObject; final Object spotObject = inputs.getValue(getSpotRequirement(security)); if (spotObject == null) { throw new OpenGammaRuntimeException("Could not get Underlying's Spot value"); } final double spot = (Double) spotObject; // 3. Compute the forward final double discountFactor = discountCurve.getDiscountFactor(expiry); Validate.isTrue(discountFactor != 0, "The discount curve has returned a zero value for a discount bond. Check rates."); final double forward = spot / discountFactor; final ValueSpecification valueSpec = getValueSpecification(target.toSpecification(), security); return Collections.singleton(new ComputedValue(valueSpec, forward)); }
From source file:com.facebook.buck.jvm.java.abi.JarReader.java
private DirectoryReader getInner() throws IOException { // Actually had a file descriptor leak because something was reopening this after it was closed, // so let's make sure we crash and detect it if it happens again. Preconditions.checkState(!closed);/* w w w .j av a 2s.co m*/ if (inner == null) { fileSystem = FileSystems.newFileSystem(jarPath, null); inner = new DirectoryReader(Iterables.getOnlyElement(fileSystem.getRootDirectories())); } return inner; }
From source file:de.rnd7.libtvdb.TVDBAccessor.java
/** * //from www .j a va2 s . com * @param showID * (e.g. 72108 for NCIS) * @return * @throws IOException * @throws ParseException */ public TVShow getShow(final int showID) throws TVDBException { try { final URL url = new URL(String.format(TVDBAccessor.API_URL + "/%s/series/%d/all/%s.zip", this.apiKey, showID, this.language)); final CachedElement element = this.cache.get(URLCacheKey.of(url)); try (InputStream inputStream = element.openStream()) { final InputStream xmlStream = SeriesZipTransformator.toSeriesFile(inputStream, this.language); final Data data = ModelTransformator.parse(xmlStream); final List<TVShow> shows = ModelTransformator.toLibModel(data); return Iterables.getOnlyElement(shows); } } catch (final Exception e) { throw new TVDBException(e); } }
From source file:org.apache.beam.runners.direct.FlattenEvaluatorFactory.java
private <InputT> TransformEvaluator<InputT> createInMemoryEvaluator( final AppliedPTransform<PCollectionList<InputT>, PCollection<InputT>, PCollections<InputT>> application) { final UncommittedBundle<InputT> outputBundle = evaluationContext .createBundle((PCollection<InputT>) Iterables.getOnlyElement(application.getOutputs().values())); final TransformResult<InputT> result = StepTransformResult.<InputT>withoutHold(application) .addOutput(outputBundle).build(); return new FlattenEvaluator<>(outputBundle, result); }
From source file:org.jclouds.ibm.smartcloud.compute.suppliers.IBMSmartCloudLocationSupplier.java
@Override public Set<? extends Location> get() { Builder<Location> locations = ImmutableSet.builder(); Set<? extends org.jclouds.ibm.smartcloud.domain.Location> list = sync.listLocations(); Location provider = Iterables.getOnlyElement(super.get()); if (list.size() == 0) locations.add(provider);//from www . j a v a2 s. com else for (org.jclouds.ibm.smartcloud.domain.Location from : list) { if (from.getState() != State.OFFLINE) { 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() + "")); locations.add(builder.build()); } } return locations.build(); }
From source file:org.sosy_lab.cpachecker.cfa.blocks.builder.LoopPartitioning.java
private void initLoopMap() { loopHeaderToLoopBody = new HashMap<>(); if (cfa.getLoopStructure().isPresent()) { for (Loop loop : cfa.getLoopStructure().get().getAllLoops()) { if (loop.getLoopHeads().size() == 1) { //currently only loops with single loop heads supported loopHeaderToLoopBody.put(Iterables.getOnlyElement(loop.getLoopHeads()), loop.getLoopNodes()); }// w w w.j a v a 2s .c om } } }
From source file:org.jclouds.location.suppliers.all.RegionToProvider.java
@Override public Set<? extends Location> get() { Builder<Location> locations = ImmutableSet.builder(); Location provider = Iterables.getOnlyElement(justProvider.get()); Set<String> regions = regionsSupplier.get(); checkState(regions.size() > 0, "no regions found for provider %s, using supplier %s", provider, regionsSupplier);/*from w w w . j ava 2s .com*/ Map<String, Supplier<Set<String>>> isoCodesById = isoCodesByIdSupplier.get(); for (String region : regions) { LocationBuilder builder = new LocationBuilder().scope(LocationScope.REGION).id(region) .description(region).parent(provider); if (isoCodesById.containsKey(region)) builder.iso3166Codes(isoCodesById.get(region).get()); locations.add(builder.build()); } return locations.build(); }
From source file:io.crate.planner.consumer.ConsumingPlanner.java
@Nullable public static TableRelation getSingleTableRelation(Map<QualifiedName, AnalyzedRelation> sources) { if (sources.size() != 1) { return null; }//from www. j a v a 2 s. c om AnalyzedRelation sourceRelation = Iterables.getOnlyElement(sources.values()); if (sourceRelation instanceof TableRelation) { return (TableRelation) sourceRelation; } return null; }