Example usage for com.google.common.base Optional get

List of usage examples for com.google.common.base Optional get

Introduction

In this page you can find the example usage for com.google.common.base Optional get.

Prototype

public abstract T get();

Source Link

Document

Returns the contained instance, which must be present.

Usage

From source file:org.opendaylight.genius.utils.clustering.ClusteringUtils.java

public static Boolean isEntityOwner(EntityOwnershipService entityOwnershipService, Entity entity,
        long sleepBetweenRetries, int retries) {
    while (retries-- > 0) {
        Optional<EntityOwnershipState> entityState = entityOwnershipService.getOwnershipState(entity);
        if (entityState.isPresent()) {
            EntityOwnershipState entityOwnershipState = entityState.get();
            if (entityOwnershipState.hasOwner()) {
                return entityOwnershipState.isOwner();
            }/*w ww. jav  a  2s  .  c o m*/
        }
        LOG.trace("EntityOwnershipState for entity type {} is not yet available. {} retries left",
                entity.getType(), retries);
        try {
            Thread.sleep(sleepBetweenRetries);
        } catch (InterruptedException e) {
            break;
        }
    }
    return false;
}

From source file:net.caseif.flint.steel.util.helper.ChatHelper.java

public static boolean isTeamBarrierPresent(Player sender, Player recipient) {
    Optional<Challenger> senderCh = SteelCore.getChallenger(sender.getUniqueId());
    Optional<Challenger> recipCh = SteelCore.getChallenger(recipient.getUniqueId());

    if (senderCh.isPresent() && recipCh.isPresent()) {
        if (senderCh.get().getRound() == recipCh.get().getRound()) {
            if (senderCh.get().getRound().getConfigValue(ConfigNode.SEPARATE_TEAM_CHATS)) {
                return true;
            }//ww  w  .j ava  2s  .c  o  m
        }
    }
    return false;
}

From source file:org.opendaylight.groupbasedpolicy.renderer.iovisor.utils.IovisorModuleUtils.java

/**
 * Make sure the specified IOvisor module Uri exists in the datastore.
 * @param dataBroker An instance of the {@link DataBroker}
 * @param iovisorModuleUri The Uri of the {@link IovisorModule} we want to validate
 * @return <code>true</code> if validated, else, <code>false</code>
 *///from w w  w.j a v  a2 s  . c o  m
public static boolean validateIovisorModuleInstance(DataBroker dataBroker, Uri iovisorModuleUri) {
    Optional<IovisorModuleInstances> res = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
            IovisorIidFactory.iovisorModuleInstanceWildCardIid(), dataBroker.newReadOnlyTransaction());
    if (res.isPresent()) {
        for (IovisorModuleInstance instance : res.get().getIovisorModuleInstance()) {
            if (instance.getUri().equals(iovisorModuleUri)) {
                return true;
            }
        }
    }
    return false;
}

From source file:org.opendaylight.protocol.bgp.openconfig.impl.moduleconfig.DataBrokerFunction.java

public static <T extends ServiceRef & ChildOf<Module>> T getRibInstance(
        final BGPConfigModuleProvider configModuleOp, final Function<String, T> function,
        final String instanceName, final ReadOnlyTransaction rTx) {
    Preconditions.checkNotNull(rTx);/* ww  w.j  a  va 2  s.  co  m*/
    try {
        final Optional<Service> maybeService = configModuleOp
                .readConfigService(new ServiceKey(DomAsyncDataBroker.class), rTx);
        if (maybeService.isPresent()) {
            final Optional<Instance> maybeInstance = Iterables.tryFind(maybeService.get().getInstance(),
                    new Predicate<Instance>() {
                        @Override
                        public boolean apply(final Instance instance) {
                            final String moduleName = OpenConfigUtil.getModuleName(instance.getProvider());
                            if (moduleName.equals(instanceName)) {
                                return true;
                            }
                            return false;
                        }
                    });
            if (maybeInstance.isPresent()) {
                return function.apply(maybeInstance.get().getName());
            }
        }
        return null;
    } catch (ReadFailedException e) {
        throw new IllegalStateException("Failed to read service.", e);
    }
}

From source file:org.opendaylight.protocol.bgp.linkstate.impl.tlvs.OspfRouteTlvParser.java

public static OspfRouteType serializeModel(final ContainerNode prefixDesc) {
    final Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> ospfRoute = prefixDesc
            .getChild(OSPF_ROUTE_NID);/*from  w w w. j ava2  s  .c  o  m*/
    if (ospfRoute.isPresent()) {
        return OspfRouteType.forValue(domOspfRouteTypeValue((String) ospfRoute.get().getValue()));
    }
    return null;
}

From source file:org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils.java

public static <T extends StoreTreeNode<T>> Map.Entry<InstanceIdentifier, T> findClosestsOrFirstMatch(
        final T tree, final InstanceIdentifier path, final Predicate<T> predicate) {
    Optional<T> parent = Optional.<T>of(tree);
    Optional<T> current = Optional.<T>of(tree);

    int nesting = 0;
    Iterator<PathArgument> pathIter = path.getPath().iterator();
    while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) {
        parent = current;//www  .  j  a  va 2s.  c  o  m
        current = current.get().getChild(pathIter.next());
        nesting++;
    }
    if (current.isPresent()) {
        final InstanceIdentifier currentPath = new InstanceIdentifier(path.getPath().subList(0, nesting));
        return new SimpleEntry<InstanceIdentifier, T>(currentPath, current.get());
    }

    /*
     * Subtracting 1 from nesting level at this point is safe, because we
     * cannot reach here with nesting == 0: that would mean the above check
     * for current.isPresent() failed, which it cannot, as current is always
     * present. At any rate we check state just to be on the safe side.
     */
    Preconditions.checkState(nesting > 0);
    final InstanceIdentifier parentPath = new InstanceIdentifier(path.getPath().subList(0, nesting - 1));

    return new SimpleEntry<InstanceIdentifier, T>(parentPath, parent.get());
}

From source file:info.gehrels.voting.OptionalMatchers.java

public static <T> Matcher<Optional<T>> anOptionalWhoseValue(Matcher<? super T> valueMatcher) {
    return new TypeSafeDiagnosingMatcher<Optional<T>>() {
        @Override//  ww w  . jav a 2s.  c  om
        protected boolean matchesSafely(Optional<T> optional, Description mismatchDescription) {
            if (!optional.isPresent()) {
                mismatchDescription.appendText("an absent Optional");
                return false;
            }

            T value = optional.get();
            if (!valueMatcher.matches(value)) {
                mismatchDescription.appendText("an Optional whose value ");
                valueMatcher.describeMismatch(value, mismatchDescription);
                return false;
            }

            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("an Optional whose value ").appendDescriptionOf(valueMatcher);
        }
    };
}

From source file:at.ac.univie.isc.asio.d2rq.LoadD2rqModel.java

/**
 * Replace any existing {@link de.fuberlin.wiwiss.d2rq.vocab.D2RConfig#baseURI} with the given.
 * If no {@link de.fuberlin.wiwiss.d2rq.vocab.D2RConfig#Server} is present, create one and attach the given uri.
 *
 * @param model   given configuration/*  ww  w . ja  v  a  2s.co m*/
 * @param baseUri uri that should be injected
 * @return modified model
 */
static Model injectBaseUri(final Model model, final String baseUri) {
    final Optional<Resource> maybeServer = D2rqTools.findSingleOfType(model, D2RConfig.Server);
    final Resource server;
    if (maybeServer.isPresent()) {
        server = maybeServer.get();
    } else { // create an empty one
        server = model.createResource().addProperty(RDF.type, D2RConfig.Server);
    }
    server.removeAll(D2RConfig.baseURI);
    server.addProperty(D2RConfig.baseURI, model.createResource(baseUri));
    return model;
}

From source file:ec.nbdemetra.spreadsheet.SpreadsheetProviderBuddy.java

private static SpreadSheetCollection.AlignType getAlignType(DataSet dataSet) {
    Optional<SpreadSheetProvider> p = TsProviders.lookup(SpreadSheetProvider.class, SpreadSheetProvider.SOURCE);
    if (p.isPresent()) {
        try {//  w  ww  . j  a  v a  2  s  .co m
            return p.get().getSeries(dataSet).alignType;
        } catch (IOException ex) {
            // TODO: log this?
        }
    }
    return SpreadSheetCollection.AlignType.UNKNOWN;
}

From source file:org.anhonesteffort.flock.crypto.KeyHelper.java

public static Optional<String> buildEncodedSalt(Context context) throws IOException {
    Optional<byte[]> salt = KeyStore.getKeyMaterialSalt(context);

    if (!salt.isPresent())
        return Optional.absent();

    return Optional.of(Base64.encodeBytes(salt.get()));
}