Example usage for com.google.common.collect Iterables getOnlyElement

List of usage examples for com.google.common.collect Iterables getOnlyElement

Introduction

In this page you can find the example usage for com.google.common.collect Iterables getOnlyElement.

Prototype

public static <T> T getOnlyElement(Iterable<T> iterable) 

Source Link

Document

Returns the single element contained in iterable .

Usage

From source file:brooklyn.entity.software.mysql.DynamicToyMySqlEntityBuilder.java

public static String getOsTag(Entity e) {
    // e.g. "osx10.6-x86_64"; see http://www.mysql.com/downloads/mysql/#downloads
    OsDetails os = ((SshMachineLocation) Iterables.getOnlyElement(e.getLocations())).getOsDetails();
    if (os == null)
        return "linux2.6-i686";
    if (os.isMac()) {
        String osp1 = os.getVersion() == null ? "osx10.5" //lowest common denominator
                : new ComparableVersion(os.getVersion()).isGreaterThanOrEqualTo(OsVersions.MAC_10_6) ? "osx10.6"
                        : new ComparableVersion(os.getVersion()).isGreaterThanOrEqualTo(OsVersions.MAC_10_5)
                                ? "osx10.5"
                                : "osx10.5"; //lowest common denominator
        String osp2 = os.is64bit() ? "x86_64" : "x86";
        return osp1 + "-" + osp2;
    }// ww w.  j  a va 2  s.co m
    //assume generic linux
    String osp1 = "linux2.6";
    String osp2 = os.is64bit() ? "x86_64" : "i686";
    return osp1 + "-" + osp2;
}

From source file:at.ac.univie.isc.asio.CaptureEvents.java

/**
 * Get the single captured event of the expected type. Fails fast if more than one event was
 * captured.
 */
public EVENT single() {
    return Iterables.getOnlyElement(captured());
}

From source file:org.apache.flex.compiler.internal.definitions.references.ResolvedQualifiersReference.java

@Override
public IDefinition resolve(ICompilerProject project, ASScope scope, DependencyType dependencyType,
        boolean canEscapeWith) {
    if (qualifiers.size() == 1) {
        INamespaceDefinition qualifier = Iterables.getOnlyElement(qualifiers);
        return scope.findPropertyQualified(project, qualifier, getName(), dependencyType, canEscapeWith);
    }/*from  www.j  ava 2 s  .  co m*/

    return ((CompilerProject) project).getCacheForScope(scope).findPropertyMultiname(this, dependencyType);
}

From source file:com.opengamma.financial.analytics.model.hullwhitediscounting.HullWhiteDiscountingPVFunction.java

@Override
public CompiledFunctionDefinition compile(final FunctionCompilationContext context, final Instant atInstant) {
    return new HullWhiteCompiledFunction(getTargetToDefinitionConverter(context),
            getDefinitionToDerivativeConverter(context), true) {

        @Override//from w ww. j a v  a2 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 HullWhiteOneFactorProviderInterface data = getMergedProviders(inputs, fxMatrix);
            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:org.apache.shindig.social.opensocial.service.AlbumHandler.java

@Operation(httpMethods = "POST", bodyParam = "album")
public Future<?> create(SocialRequestItem request) throws ProtocolException {
    // Retrieve userIds and albumIds
    Set<UserId> userIds = request.getUsers();
    List<String> albumIds = request.getListParameter("albumId");

    // Preconditions - exactly one userId specified, no albumIds specified
    HandlerPreconditions.requireNotEmpty(userIds, "No userId specified");
    HandlerPreconditions.requireSingular(userIds, "Multiple userIds not supported");
    HandlerPreconditions.requireEmpty(albumIds, "Cannot specify albumId in create");

    return service.createAlbum(Iterables.getOnlyElement(userIds), request.getAppId(),
            request.getTypedParameter("album", Album.class), request.getToken());
}

From source file:com.opengamma.financial.analytics.model.black.BlackDiscountingWeightedVegaIRFutureOptionFunction.java

@Override
public CompiledFunctionDefinition compile(final FunctionCompilationContext context, final Instant atInstant) {
    return new BlackDiscountingCompiledFunction(getTargetToDefinitionConverter(context),
            getDefinitionToDerivativeConverter(context), true) {

        @Override/* w w w  .  j  a va2 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 ValueRequirement desiredValue = Iterables.getOnlyElement(desiredValues);
            final ValueProperties constraints = desiredValue.getConstraints();
            final Set<String> baseDayProperty = constraints.getValues(PROPERTY_BASE_DAYS);
            final ValueProperties.Builder resultConstraints = constraints.copy();
            final double baseDays;
            if (baseDayProperty.size() == 1) {
                baseDays = Double.parseDouble(Iterables.getOnlyElement(baseDayProperty));
                resultConstraints.with(PROPERTY_BASE_DAYS, baseDayProperty);
            } else {
                baseDays = DEFAULT_BASE_DAYS;
                resultConstraints.with(PROPERTY_BASE_DAYS, Double.toString(DEFAULT_BASE_DAYS));
            }
            final double positionVega = (Double) inputs.getValue(POSITION_VEGA);
            final IRFutureOptionSecurity security = (IRFutureOptionSecurity) target.getTrade().getSecurity();
            final Expiry expiry = security.getExpiry();
            if (expiry.getAccuracy().equals(ExpiryAccuracy.MONTH_YEAR)
                    || expiry.getAccuracy().equals(ExpiryAccuracy.YEAR)) {
                throw new OpenGammaRuntimeException(
                        "Security's expiry is not accurate to the day, which is required: "
                                + security.toString());
            }
            final long daysToExpiry = ChronoUnit.DAYS.between(
                    LocalDate.now(executionContext.getValuationClock()), expiry.getExpiry().toLocalDate());
            final double weighting = Math.sqrt(baseDays / Math.max(daysToExpiry, 1.0));
            final double weightedVega = weighting * positionVega;
            final ValueSpecification valueSpecification = new ValueSpecification(POSITION_WEIGHTED_VEGA,
                    target.toSpecification(), resultConstraints.get());
            final ComputedValue result = new ComputedValue(valueSpecification, weightedVega);
            return Sets.newHashSet(result);
        }

        @Override
        public Set<ValueRequirement> getRequirements(final FunctionCompilationContext compilationContext,
                final ComputationTarget target, final ValueRequirement desiredValue) {
            if (super.getRequirements(compilationContext, target, desiredValue) == null) {
                return null;
            }
            final ValueProperties properties = desiredValue.getConstraints();
            return Collections.singleton(
                    new ValueRequirement(POSITION_VEGA, target.toSpecification(), properties.copy().get()));
        }

        @Override
        protected ValueProperties.Builder getResultProperties(
                final FunctionCompilationContext compilationContext, final ComputationTarget target) {
            final ValueProperties.Builder properties = super.getResultProperties(compilationContext, target);
            return properties.withAny(PROPERTY_BASE_DAYS);
        }
    };
}

From source file:com.opengamma.financial.analytics.model.hullwhitediscounting.HullWhiteMarketQuoteFunction.java

@Override
public CompiledFunctionDefinition compile(final FunctionCompilationContext context, final Instant atInstant) {
    return new HullWhiteCompiledFunction(getTargetToDefinitionConverter(context),
            getDefinitionToDerivativeConverter(context), false) {

        @Override//w w w .j  a va 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 HullWhiteOneFactorProviderInterface data = getMergedProviders(inputs, fxMatrix);
            final ValueRequirement desiredValue = Iterables.getOnlyElement(desiredValues);
            final double marketQuote = derivative.accept(CALCULATOR, data);
            final ValueProperties properties = desiredValue.getConstraints().copy().get();
            final ValueSpecification spec = new ValueSpecification(MARKET_QUOTE, target.toSpecification(),
                    properties);
            return Collections.singleton(new ComputedValue(spec, marketQuote));
        }

        @Override
        public boolean canApplyTo(final FunctionCompilationContext compilationContext,
                final ComputationTarget target) {
            final Security security = target.getTrade().getSecurity();
            return security instanceof DeliverableSwapFutureSecurity
                    || security instanceof InterestRateFutureSecurity;
        }

    };
}

From source file:org.obm.icalendar.ical4jwrapper.ICalendarRecur.java

public Integer byMonth() {
    Collection<Integer> monthList = recur.getMonthList();
    if (monthList != null && !monthList.isEmpty()) {
        return Iterables.getOnlyElement(monthList);
    }/*from   ww  w .ja  va2 s  .  co  m*/
    return null;
}

From source file:io.druid.segment.filter.ExpressionFilter.java

@Override
public boolean supportsBitmapIndex(final BitmapIndexSelector selector) {
    if (requiredBindings.isEmpty()) {
        // Constant expression.
        return true;
    } else if (requiredBindings.size() == 1) {
        // Single-column expression. We can use bitmap indexes if this column has an index and does not have
        // multiple values. The lack of multiple values is important because expression filters treat multi-value
        // arrays as nulls, which doesn't permit index based filtering.
        final String column = Iterables.getOnlyElement(requiredBindings);
        return selector.getBitmapIndex(column) != null && !selector.hasMultipleValues(column);
    } else {//from   w  ww  .j  a  v  a  2s  . com
        // Multi-column expression.
        return false;
    }
}

From source file:com.opengamma.financial.analytics.model.black.BlackDiscountingSpotDeltaFXOptionFunction.java

@Override
public CompiledFunctionDefinition compile(final FunctionCompilationContext context, final Instant atInstant) {
    return new BlackDiscountingCompiledFunction(getTargetToDefinitionConverter(context),
            getDefinitionToDerivativeConverter(context), false) {

        @Override/*ww  w .jav  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 BlackForexSmileProvider blackData = getBlackSurface(executionContext, inputs, target,
                    fxMatrix);
            final double delta = derivative.accept(CALCULATOR, blackData);
            final ValueRequirement desiredValue = Iterables.getOnlyElement(desiredValues);
            final ValueProperties properties = desiredValue.getConstraints().copy().get();
            final ValueSpecification spec = new ValueSpecification(DELTA, target.toSpecification(), properties);
            return Collections.singleton(new ComputedValue(spec, delta));
        }

    };
}