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

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

Introduction

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

Prototype

public static <T> Optional<T> fromNullable(@Nullable T nullableReference) 

Source Link

Document

If nullableReference is non-null, returns an Optional instance containing that reference; otherwise returns Optional#absent .

Usage

From source file:com.shufudong.GuavaExample.collect.OptionalExample.java

/**
 * @return/*from  w w w  .ja  v  a  2 s. com*/
 * @category <p>??</p>
 * <ol>
 * <li>Optional.of(T)Optional??nullT?T=null</li>
 * <li>Optional.absent()Optional?</li>
 * <li>Optional.fromNullable(T)T?OptionalT???[Optional.fromNullable(null)Optional.absent()</li>
 * </ol>
 * @throw
 */
public static void testOptional2() throws Exception {
    Optional<Integer> possible = Optional.of(6);
    Optional<Integer> absentOpt = Optional.absent();
    Optional<Integer> NullableOpt = Optional.fromNullable(null);
    Optional<Integer> NoNullableOpt = Optional.fromNullable(10);
    if (possible.isPresent()) {
        System.out.println("possible isPresent:" + possible.isPresent());
        System.out.println("possible value:" + possible.get());
    }
    if (absentOpt.isPresent()) {
        System.out.println("absentOpt isPresent:" + absentOpt.isPresent());
    }
    if (NullableOpt.isPresent()) {
        System.out.println("fromNullableOpt isPresent:" + NullableOpt.isPresent());
    }
    if (NoNullableOpt.isPresent()) {
        System.out.println("NoNullableOpt isPresent:" + NoNullableOpt.isPresent());
    }
}

From source file:org.elasticsearch.river.subversion.crawler.LogEntryFilter.java

public LogEntryFilter(boolean contentToBeFiltered, boolean crawlingToBePrevented, String reason) {
    this.contentToBeFiltered = contentToBeFiltered;
    this.crawlingToBePrevented = crawlingToBePrevented;
    this.reason = Optional.fromNullable(reason);
}

From source file:org.geogig.osm.internal.log.ReadOSMMappingLogEntry.java

@Override
protected Optional<OSMMappingLogEntry> _call() {
    BlobStore blobStore = context().blobStore();
    final String pathPrefix = "osm/map/";
    final String path = pathPrefix + this.path;
    Optional<String> blob = Blobs.getBlobAsString(blobStore, path);
    OSMMappingLogEntry entry = null;/*  ww  w .  j a  v a2s .c om*/
    if (blob.isPresent()) {
        entry = OSMMappingLogEntry.fromString(blob.get());
    }
    return Optional.fromNullable(entry);
}

From source file:org.apache.hadoop.hdfs.protocolPB.ReconfigurationProtocolUtils.java

public static ReconfigurationTaskStatus getReconfigurationStatus(
        GetReconfigurationStatusResponseProto response) {
    Map<PropertyChange, Optional<String>> statusMap = null;
    long startTime;
    long endTime = 0;

    startTime = response.getStartTime();
    if (response.hasEndTime()) {
        endTime = response.getEndTime();
    }/*from  w  w  w  . j ava  2  s.co  m*/
    if (response.getChangesCount() > 0) {
        statusMap = Maps.newHashMap();
        for (GetReconfigurationStatusConfigChangeProto change : response.getChangesList()) {
            PropertyChange pc = new PropertyChange(change.getName(), change.getNewValue(),
                    change.getOldValue());
            String errorMessage = null;
            if (change.hasErrorMessage()) {
                errorMessage = change.getErrorMessage();
            }
            statusMap.put(pc, Optional.fromNullable(errorMessage));
        }
    }
    return new ReconfigurationTaskStatus(startTime, endTime, statusMap);
}

From source file:org.sonar.db.ce.CeActivityDao.java

public Optional<CeActivityDto> selectByUuid(DbSession dbSession, String uuid) {
    return Optional.fromNullable(mapper(dbSession).selectByUuid(uuid));
}

From source file:ec.tss.tsproviders.spreadsheet.engine.CellParser.java

@Nonnull
public Optional<T> tryParse(@Nonnull Sheet sheet, int rowIndex, int columnIndex) {
    return Optional.fromNullable(parse(sheet, rowIndex, columnIndex));
}

From source file:org.croudtrip.db.UserDAO.java

public Optional<User> findByEmail(String email) {
    return Optional.fromNullable(
            uniqueResult(namedQuery(User.QUERY_NAME_FIND_BY_EMAIL).setParameter(User.QUERY_PARAM_MAIL, email)));
}

From source file:net.pterodactylus.sonitus.data.DataPacket.java

/**
 * Creates a new data packet./*from  ww w  .j  a  v a  2  s .co m*/
 *
 * @param metadata
 *       The metadata (may be {@code null})
 * @param buffer
 *       The audio data
 */
public DataPacket(Metadata metadata, byte[] buffer) {
    this(Optional.fromNullable(metadata), buffer);
}

From source file:springfox.documentation.schema.Annotations.java

@SuppressWarnings("PMD")
private static <A extends Annotation> Optional<A> tryGetGetterAnnotation(
        BeanPropertyDefinition beanPropertyDefinition, Class<A> annotationClass) {

    if (beanPropertyDefinition.hasGetter()) {
        return Optional.fromNullable(beanPropertyDefinition.getGetter().getAnnotation(annotationClass));
    }//  w  w w. j  a v  a2  s  .c  om
    return Optional.absent();
}

From source file:com.spotify.heroic.elasticsearch.NodeClientSetup.java

@JsonCreator
public NodeClientSetup(String clusterName, List<String> seeds) {
    this.clusterName = Optional.fromNullable(clusterName).or(DEFAULT_CLUSTER_NAME);
    this.seeds = seedsToDiscovery(seeds);
}