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

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

Introduction

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

Prototype

public abstract boolean isPresent();

Source Link

Document

Returns true if this holder contains a (non-null) instance.

Usage

From source file:org.automagic.deps.doctor.Utils.java

public static Optional<Node> getPluginComment(String parent, Artifact artifact, Document document) {

    Optional<Node> dependencyNode = getDependencyNode(artifact, document, parent);

    if (!dependencyNode.isPresent()) {
        return Optional.absent();
    }//from ww  w . ja  v  a 2 s . co  m

    if (dependencyNode.get().hasChildNodes()) {
        NodeList childNodes = dependencyNode.get().getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if (Node.COMMENT_NODE == node.getNodeType()
                    && contains(node.getNodeValue(), PomWriter.AUTO_COMMENT)) {
                return Optional.of(node);
            }
        }
    }

    return Optional.absent();
}

From source file:org.jclouds.openstack.keystone.v2_0.suppliers.LocationIdToURIFromAccessForTypeAndVersion.java

private static <K, V> void putIfPresent(K key, Optional<V> value, Builder<K, V> builder) {
    if (value.isPresent())
        builder.put(key, value.get());//  ww  w. ja  va  2 s  . com
}

From source file:org.n52.sos.util.I18NHelper.java

/**
 * Add offering description to {@link SosOffering} for the specific language
 * or the configured default language/*from   w  w  w  . jav  a 2  s. co m*/
 *
 * @param offering
 *            {@link SosOffering} to add description
 * @param locale
 *            the specific language
 */
public static void addOfferingDescription(SosOffering offering, Locale locale) {
    MultilingualString descriptions = getCache().getI18nDescriptionsForOffering(offering.getIdentifier());
    if (descriptions != null) {
        Optional<LocalizedString> description = descriptions.getLocalizationOrDefault(locale);
        if (description.isPresent()) {
            offering.setDescription(description.get().getText());
        }
    }
}

From source file:de.azapps.mirakel.helper.TaskHelper.java

/**
 * Returns the ID of the ColorResource for a DueDate
 *
 * @param origDue/*from ww  w .ja  va2 s.c  om*/
 *            The DueDate
 * @param isDone
 *            Is the Task done?
 * @return ID of the ColorResource
 */
public static int getTaskDueColor(final Context context, final Optional<Calendar> origDue,
        final boolean isDone) {
    final int colorResource;
    if (!origDue.isPresent()) {
        colorResource = R.color.Grey;
    } else {
        final LocalDate today = new LocalDate();
        final LocalDate nextWeek = new LocalDate().plusDays(7);
        final LocalDate due = new LocalDate(origDue.get());
        final int cmpr = today.compareTo(due);
        if (isDone) {
            colorResource = R.color.Grey;
        } else if (cmpr > 0) {
            colorResource = R.color.due_overdue;
        } else if (cmpr == 0) {
            colorResource = R.color.due_today;
        } else if (nextWeek.compareTo(due) >= 0) {
            colorResource = R.color.due_next;
        } else {
            colorResource = R.color.due_future;
        }
    }
    return context.getResources().getColor(colorResource);
}

From source file:io.urmia.job.run.Main.java

private static ServiceInstance<NodeType> getMyMatchingODSInstance(ServiceInstance<NodeType> me)
        throws Exception {
    int rc = ns.getRunningCount(me);
    System.err.println("my running count: " + rc);
    Optional<ServiceInstance<NodeType>> ods = ns.getOfType(NodeType.ODS, me.getAddress(), rc);
    if (!ods.isPresent())
        throw new RuntimeException("unable to find matching ODS at: " + me.getAddress() + ", index: " + rc);
    return ods.get();
}

From source file:google.registry.ui.server.registrar.SessionUtils.java

/** Returns first {@link Registrar} that {@code gaeUserId} is authorized to administer. */
private static Optional<Registrar> guessRegistrar(String gaeUserId) {
    RegistrarContact contact = ofy().load().type(RegistrarContact.class).filter("gaeUserId", gaeUserId).first()
            .now();//w  ww  . ja  va 2  s .  c o  m
    if (contact == null) {
        return Optional.absent();
    }
    Optional<Registrar> result = Optional.fromNullable(ofy().load().key(contact.getParent()).now());
    if (!result.isPresent()) {
        logger.severefmt("A contact record exists for non-existent registrar: %s.", Key.create(contact));
    }
    return result;
}

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

public static Optional<String> buildEncryptedKeyMaterial(Context context)
        throws IOException, GeneralSecurityException {
    Optional<byte[]> cipherKey = KeyStore.getCipherKey(context);
    Optional<byte[]> macKey = KeyStore.getMacKey(context);
    Optional<byte[]> salt = KeyStore.getKeyMaterialSalt(context);
    Optional<String> masterPassphrase = KeyStore.getMasterPassphrase(context);

    if (!masterPassphrase.isPresent() || !cipherKey.isPresent() || !macKey.isPresent() || !salt.isPresent())
        return Optional.absent();

    SecretKey[] masterKeys = KeyUtil.getCipherAndMacKeysForPassphrase(salt.get(), masterPassphrase.get());
    SecretKey masterCipherKey = masterKeys[0];
    SecretKey masterMacKey = masterKeys[1];
    MasterCipher masterCipher = new MasterCipher(masterCipherKey, masterMacKey);

    byte[] keyMaterial = Util.combine(cipherKey.get(), macKey.get());
    byte[] encryptedKeyMaterial = masterCipher.encryptAndEncode(keyMaterial);

    return Optional.of(new String(encryptedKeyMaterial));
}

From source file:org.geogit.api.hooks.Hookables.java

public static List<CommandHook> findHooksFor(AbstractGeoGitOp<?> operation) {

    final Class<? extends AbstractGeoGitOp> clazz = operation.getClass();

    List<CommandHook> hooks = Lists.newLinkedList();
    /*//ww w .j av  a2s.c om
     * First add any classpath hook, as they can be added to any command, regardless of having
     * the @Hookable annotation or not
     */
    for (CommandHook hook : classPathHooks) {
        if (hook.appliesTo(clazz)) {
            hooks.add(hook);
        }
    }

    /*
     * Now add any script hook that's configured for the operation iif it's @Hookable
     */
    final Optional<String> name = Hookables.getFilename(clazz);
    if (!name.isPresent()) {
        return hooks;
    }

    final File hooksDir = findHooksDirectory(operation);
    if (hooksDir == null) {
        return hooks;
    }

    if (name.isPresent()) {
        String preHookName = "pre_" + name.get().toLowerCase();
        String postHookName = "post_" + name.get().toLowerCase();
        File[] files = hooksDir.listFiles();
        for (File file : files) {
            String filename = file.getName();
            if (isHook(filename, preHookName)) {
                hooks.add(Scripting.createScriptHook(file, true));
            }
            if (isHook(filename, postHookName)) {
                hooks.add(Scripting.createScriptHook(file, false));
            }
        }
    }
    return hooks;

}

From source file:net.es.nsi.pce.pf.PfUtils.java

public static String getSourceStpOrFail(P2PServiceBaseType p2ps) {
    Optional<String> sourceStp = Optional.fromNullable(Strings.emptyToNull(p2ps.getSourceSTP()));
    if (sourceStp.isPresent()) {
        return sourceStp.get();
    }/*from ww w  .  j a v  a  2 s .  com*/

    throw Exceptions.missingParameter(Point2PointTypes.getSourceStp().getNamespace(),
            Point2PointTypes.getSourceStp().getType(), "null");
}

From source file:org.locationtech.geogig.hooks.Hookables.java

public static List<CommandHook> findHooksFor(AbstractGeoGigOp<?> operation) {

    @SuppressWarnings("unchecked")
    final Class<? extends AbstractGeoGigOp<?>> clazz = (Class<? extends AbstractGeoGigOp<?>>) operation
            .getClass();//from   ww w .j av a 2  s  .c  o m

    List<CommandHook> hooks = Lists.newLinkedList();
    /*
     * First add any classpath hook, as they can be added to any command, regardless of having
     * the @Hookable annotation or not
     */
    for (CommandHook hook : classPathHooks) {
        if (hook.appliesTo(clazz)) {
            hooks.add(hook);
        }
    }

    /*
     * Now add any script hook that's configured for the operation iif it's @Hookable
     */
    final Optional<String> name = Hookables.getFilename(clazz);
    if (!name.isPresent()) {
        return hooks;
    }

    final File hooksDir = findHooksDirectory(operation);
    if (hooksDir == null) {
        return hooks;
    }

    if (name.isPresent()) {
        String preHookName = "pre_" + name.get().toLowerCase();
        String postHookName = "post_" + name.get().toLowerCase();
        File[] files = hooksDir.listFiles();
        for (File file : files) {
            String filename = file.getName();
            if (isHook(filename, preHookName)) {
                hooks.add(Scripting.createScriptHook(file, true));
            }
            if (isHook(filename, postHookName)) {
                hooks.add(Scripting.createScriptHook(file, false));
            }
        }
    }
    return hooks;

}