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.groupbasedpolicy.neutron.ovsdb.util.OvsdbHelper.java

public static Node getNodeFromBridgeRef(OvsdbBridgeRef bridgeRef, DataBroker dataBroker) {
    InstanceIdentifier<Node> nodeIid = bridgeRef.getValue().firstIdentifierOf(Node.class);
    ReadTransaction transaction = dataBroker.newReadOnlyTransaction();
    Optional<?> node = readFromDs(LogicalDatastoreType.OPERATIONAL, nodeIid, transaction);
    if (node.isPresent()) {
        if (node.get() instanceof Node) {
            return (Node) node.get();
        }/*  ww w .  j  a  v  a 2 s  .c  o m*/
    }
    return null;
}

From source file:org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier.java

/**
 * Returns filename for this YANG module as specified in RFC 6020.
 *
 * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>
 *
 * Where Where revision-date is in format YYYY-mm-dd.
 *
 * <p>/*w w  w. ja  v a2 s  .com*/
 * See http://tools.ietf.org/html/rfc6020#section-5.2
 *
 * @return Filename for this source identifier.
 */
public static String toYangFileName(final String moduleName, final Optional<String> revision) {
    StringBuilder filename = new StringBuilder(moduleName);
    if (revision.isPresent()) {
        filename.append('@');
        filename.append(revision.get());
    }
    filename.append(YangConstants.RFC6020_YANG_FILE_EXTENSION);
    return filename.toString();
}

From source file:org.blockartistry.mod.ThermalRecycling.support.recipe.AppendHelper.java

public static void append(final List<ItemStack> list, final String item, final int quantity) {

    assert list != null;
    assert item != null;
    assert quantity > 0;

    final Optional<ItemStack> stack = ItemStackHelper.getItemStack(item, quantity);
    if (stack.isPresent())
        list.add(stack.get());
}

From source file:com.eucalyptus.auth.policy.ern.Ern.java

public static Ern parse(String ern) throws JSONException {
    final Matcher matcher = ARN_PATTERN.matcher(ern);
    if (!matcher.matches()) {
        throw new JSONException("'" + ern + "' is not a valid ARN");
    }/*ww w .  j  a  v  a 2  s .c om*/
    if (matcher.group(ARN_PATTERNGROUP_IAM) != null) {
        final Optional<String> pathName = Optional.fromNullable(matcher.group(ARN_PATTERNGROUP_IAM_ID));
        final String path;
        final String name;
        int lastSlash = pathName.isPresent() ? pathName.get().lastIndexOf('/') : 0;
        if (lastSlash == 0) {
            path = "/";
            name = pathName.isPresent() ? pathName.get().substring(1) : "*";
        } else {
            path = pathName.get().substring(0, lastSlash);
            name = pathName.get().substring(lastSlash + 1);
        }
        final String accountId = matcher.group(ARN_PATTERNGROUP_IAM_NAMESPACE);
        final String type = Objects.firstNonNull(matcher.group(ARN_PATTERNGROUP_IAM_USERGROUP), "*");
        return new EuareResourceName(accountId, type, path, name);
    } else if (matcher.group(ARN_PATTERNGROUP_EC2) != null) {
        String type = matcher.group(ARN_PATTERNGROUP_EC2_TYPE).toLowerCase();
        if (!PolicySpec.EC2_RESOURCES.contains(type)) {
            throw new JSONException("EC2 type '" + type + "' is not supported");
        }
        String id = matcher.group(ARN_PATTERNGROUP_EC2_ID).toLowerCase();
        if (PolicySpec.EC2_RESOURCE_ADDRESS.equals(type)) {
            AddressUtil.validateAddressRange(id);
        }
        return new Ec2ResourceName(type, id);
    } else if (matcher.group(ARN_PATTERNGROUP_S3) != null) {
        String bucket = matcher.group(ARN_PATTERNGROUP_S3_BUCKET);
        String object = matcher.group(ARN_PATTERNGROUP_S3_OBJECT);
        return new S3ResourceName(bucket, object);
    } else {
        return new WildcardResourceName();
    }
}

From source file:org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes.java

public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
    T current = tree;/* www.  j  a va  2  s  .  com*/

    int i = 1;
    for (PathArgument pathArg : path.getPathArguments()) {
        Optional<T> potential = current.getChild(pathArg);
        if (!potential.isPresent()) {
            throw new IllegalArgumentException(
                    String.format("Child %s is not present in tree.", path.getAncestor(i)));
        }
        current = potential.get();
        ++i;
    }
    return current;
}

From source file:com.groupon.utility.OvalBuilder.java

/**
 * Static factory initializes the specified builder with state from the
 * source instance./*from w  ww  .java  2s  .  co  m*/
 *
 * @param <T>    The type of object created by the builder.
 * @param <B>    The type of builder to return.
 * @param source The source of initial state.
 * @param target The target builder instance.
 * @return Target populated from source.
 */
public static <T, B extends Builder<? super T>> B clone(final T source, final B target) {
    for (final Method targetMethod : target.getClass().getMethods()) {
        if (isSetterMethod(targetMethod)) {
            final Optional<Method> getterMethod = getGetterForSetter(targetMethod, source.getClass());
            if (getterMethod.isPresent()) {
                try {
                    if (!getterMethod.get().isAccessible()) {
                        AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                            getterMethod.get().setAccessible(true);
                            return null;
                        });
                    }
                    Object value = getterMethod.get().invoke(source);
                    if (value instanceof Optional) {
                        value = ((Optional) value).orNull();
                    }
                    targetMethod.invoke(target, value);
                } catch (final IllegalAccessException | InvocationTargetException e) {
                    throw Throwables.propagate(e);
                }
            }
        }
    }
    return target;
}

From source file:com.arpnetworking.utility.OvalBuilder.java

/**
 * Static factory initializes the specified builder with state from the
 * source instance./*  w w  w.j a va  2 s.  c o  m*/
 *
 * @param <T> The type of object created by the builder.
 * @param <B> The type of builder to return.
 * @param source The source of initial state.
 * @param target The target builder instance.
 * @return Target populated from source.
 */
public static <T, B extends Builder<? super T>> B clone(final T source, final B target) {
    for (final Method targetMethod : target.getClass().getMethods()) {
        if (isSetterMethod(targetMethod)) {
            final Optional<Method> getterMethod = getGetterForSetter(targetMethod, source.getClass());
            if (getterMethod.isPresent()) {
                try {
                    if (!getterMethod.get().isAccessible()) {
                        getterMethod.get().setAccessible(true);
                    }
                    Object value = getterMethod.get().invoke(source);
                    if (value instanceof Optional) {
                        value = ((Optional) value).orNull();
                    }
                    targetMethod.invoke(target, value);
                } catch (final IllegalAccessException | InvocationTargetException e) {
                    Throwables.propagate(e);
                }
            }
        }
    }
    return target;
}

From source file:org.anhonesteffort.flock.sync.AbstractDavSyncAdapter.java

public static void showSubscriptionExpiredNotification(Context context) {
    Log.d(TAG, "showSubscriptionExpiredNotification()");

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);

    notificationBuilder.setContentTitle(context.getString(R.string.notification_flock_subscription_expired));
    notificationBuilder.setContentText(context.getString(R.string.notification_tap_to_update_subscription));
    notificationBuilder.setSmallIcon(R.drawable.alert_warning_light);
    notificationBuilder.setAutoCancel(true);

    Intent clickIntent = new Intent(context, ManageSubscriptionActivity.class);
    Optional<DavAccount> account = DavAccountHelper.getAccount(context);

    clickIntent.putExtra(ManageSubscriptionActivity.KEY_DAV_ACCOUNT_BUNDLE, account.get().toBundle());

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, clickIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(ID_NOTIFICATION_SUBSCRIPTION, notificationBuilder.build());
}

From source file:org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.java

/**
 * Build Route Key for supporting mp//from   w w  w  . j  a v  a 2  s .  c  o  m
 * Key is composed by 2 elements (route-key + path Id)
 *
 * @param routeQname route Qname
 * @param routeKeyQname route key Qname
 * @param pathIdQname path Id Qname
 * @param routeKeyValue route key value
 * @param maybePathIdLeaf path id container, it might me supported or not, in that case default 0 value will be assigned
 * @return Route Key Nid
 */
public static NodeIdentifierWithPredicates createNidKey(final QName routeQname, final QName routeKeyQname,
        final QName pathIdQname, final Object routeKeyValue,
        final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf) {
    // FIXME: a cache here would mean we instantiate the same identifier for each route making comparison quicker.
    final Object pathId = maybePathIdLeaf.isPresent() ? (maybePathIdLeaf.get()).getValue() : NON_PATH_ID;
    return createNodeIdentifierWithPredicates(routeQname, pathIdQname, pathId, routeKeyQname, routeKeyValue);
}

From source file:org.opendaylight.controller.cluster.datastore.IntegrationTestKit.java

public static ActorRef findLocalShard(ActorContext actorContext, String shardName) {
    ActorRef shard = null;/*  w  w  w  .  ja  v a  2s . c om*/
    for (int i = 0; i < 20 * 5 && shard == null; i++) {
        Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
        Optional<ActorRef> shardReply = actorContext.findLocalShard(shardName);
        if (shardReply.isPresent()) {
            shard = shardReply.get();
        }
    }
    return shard;
}