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:springfox.bean.validators.plugins.BeanValidators.java

@VisibleForTesting
static <T extends Annotation> Optional<T> annotationFrom(AnnotatedMember nullableMember,
        Class<T> annotationType) {

    Optional<AnnotatedMember> member = Optional.fromNullable(nullableMember);
    Optional<T> notNull = Optional.absent();
    if (member.isPresent()) {
        notNull = FluentIterable.from(member.get().annotations()).filter(annotationType).first();
    }/*from  w  ww . j  a va2 s  .  co m*/
    return notNull;
}

From source file:com.complexible.common.base.Optionals.java

public static <T, E extends Exception> T require(final Optional<T> theOptional, final Class<E> theExceptionType,
        final String theMsg) throws E {
    if (theOptional.isPresent()) {
        return theOptional.get();
    }//from ww w. j a  va  2  s  . co m

    try {
        throw theExceptionType.getConstructor(String.class).newInstance(theMsg);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException("Invalid exception type provided");
    } catch (InvocationTargetException e) {
        throw new RuntimeException("Invalid exception type provided");
    } catch (InstantiationException e) {
        throw new RuntimeException("Invalid exception type provided");
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Invalid exception type provided");
    }
}

From source file:org.locationtech.geogig.storage.sqlite.SQLiteStorage.java

/**
 * Returns the .geogig directory for the platform object.
 *//*from w w w  .jav  a  2  s .  co  m*/
public static File geogigDir(Platform platform) {
    Optional<URL> url = new ResolveGeogigDir(platform).call();
    if (!url.isPresent()) {
        throw new RuntimeException("Unable to resolve .geogig directory");
    }
    try {
        return new File(url.get().toURI());
    } catch (URISyntaxException e) {
        throw new RuntimeException("Error resolving .geogig directory", e);
    }
}

From source file:org.apache.cassandra.utils.progress.jmx.LegacyJMXProgressSupport.java

protected static Optional<int[]> getLegacyUserdata(String tag, ProgressEvent event) {
    Optional<Status> status = getStatus(event);
    if (status.isPresent()) {
        int[] result = new int[2];
        result[0] = getCmd(tag);/*from  ww w. j  a  v  a 2  s  .  co m*/
        result[1] = status.get().ordinal();
        return Optional.of(result);
    }
    return Optional.absent();
}

From source file:org.fenixedu.bennu.core.domain.groups.PersistentDynamicGroup.java

@Atomic(mode = TxMode.WRITE)
public static PersistentDynamicGroup set(final String name, PersistentGroup overridingGroup) {
    Optional<PersistentDynamicGroup> persistent = PersistentDynamicGroup.getInstance(name);
    if (persistent.isPresent()) {
        return persistent.get().changeGroup(overridingGroup);
    } else {/*from w w  w .ja v  a 2  s .c  o m*/
        return new PersistentDynamicGroup(name, overridingGroup);
    }
}

From source file:org.anhonesteffort.flock.sync.key.KeySyncUtil.java

private static Optional<HidingCalDavCollection> getKeyCollection(Context context, DavAccount account)
        throws PropertyParseException, DavException, IOException {
    HidingCalDavStore store = DavAccountHelper.getHidingCalDavStore(context, account, null);
    Optional<String> calendarHomeSet = store.getCalendarHomeSet();
    if (calendarHomeSet.isPresent())
        return store.getCollection(calendarHomeSet.get().concat(PATH_KEY_COLLECTION));

    return Optional.absent();
}

From source file:com.puppycrawl.tools.checkstyle.checks.naming.ParameterNameCheck.java

/**
 * Checks whether a method is annotated with Override annotation.
 * @param ast method parameter definition token.
 * @return true if a method is annotated with Override annotation.
 *//*from www .  j av  a2  s  .c  o m*/
private static boolean isOverriddenMethod(DetailAST ast) {
    boolean overridden = false;

    final DetailAST parent = ast.getParent().getParent();
    final Optional<DetailAST> annotation = Optional.fromNullable(parent.getFirstChild().getFirstChild());

    if (annotation.isPresent() && annotation.get().getType() == TokenTypes.ANNOTATION) {
        final Optional<DetailAST> overrideToken = Optional
                .fromNullable(annotation.get().findFirstToken(TokenTypes.IDENT));
        if (overrideToken.isPresent() && "Override".equals(overrideToken.get().getText())) {
            overridden = true;
        }
    }
    return overridden;
}

From source file:com.android.camera.settings.CameraPictureSizesCacher.java

/**
 * Return list of Sizes for provided cameraId.  Check first to see if we
 * have it in the cache for the current android.os.Build.
 * Note: This method calls Camera.open(), so the camera must be closed
 * before calling or null will be returned if sizes were not previously
 * cached./*from   w w  w  . ja v a 2  s. c o m*/
 *
 * @param cameraId cameraID we would like sizes for.
 * @param context valid android application context.
 * @return List of valid sizes, or null if the Camera can not be opened.
 */
public static List<Size> getSizesForCamera(int cameraId, Context context) {
    Optional<List<Size>> cachedSizes = getCachedSizesForCamera(cameraId, context);
    if (cachedSizes.isPresent()) {
        return cachedSizes.get();
    }

    // No cached value, so need to query Camera API.
    Camera thisCamera;
    try {
        thisCamera = Camera.open(cameraId);
    } catch (RuntimeException e) {
        // Camera open will fail if already open.
        return null;
    }
    if (thisCamera != null) {
        String key_build = PICTURE_SIZES_BUILD_KEY + cameraId;
        String key_sizes = PICTURE_SIZES_SIZES_KEY + cameraId;
        SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(context);

        List<Size> sizes = Size.buildListFromCameraSizes(thisCamera.getParameters().getSupportedPictureSizes());
        thisCamera.release();
        SharedPreferences.Editor editor = defaultPrefs.edit();
        editor.putString(key_build, Build.DISPLAY);
        editor.putString(key_sizes, Size.listToString(sizes));
        editor.apply();
        return sizes;
    }
    return null;
}

From source file:org.opendaylight.protocol.bgp.rib.impl.CheckUtil.java

public static <R, T extends DataObject> R readData(final DataBroker dataBroker, final InstanceIdentifier<T> iid,
        final Function<T, R> function) throws ReadFailedException {
    AssertionError lastError = null;
    final Stopwatch sw = Stopwatch.createStarted();
    while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
        try (final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
            final Optional<T> data = tx.read(LogicalDatastoreType.OPERATIONAL, iid).checkedGet();
            if (data.isPresent()) {
                try {
                    return function.apply(data.get());
                } catch (final AssertionError e) {
                    lastError = e;//from  www  .  j ava 2 s  .  co m
                    Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
                }
            }
        }
    }
    Assert.fail(lastError.getMessage());
    throw lastError;
}

From source file:org.opendaylight.netvirt.natservice.ha.WeightedCentralizedSwitchScheduler.java

public static NaptSwitches getNaptSwitches(DataBroker dataBroker) {
    InstanceIdentifier<NaptSwitches> id = InstanceIdentifier.builder(NaptSwitches.class).build();
    Optional<NaptSwitches> naptSwitches = NatUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, id);
    return naptSwitches.isPresent() ? naptSwitches.get() : null;
}