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

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

Introduction

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

Prototype

@Nullable
public abstract T orNull();

Source Link

Document

Returns the contained instance if it is present; null otherwise.

Usage

From source file:com.offbytwo.jenkins.model.JobWithDetails.java

public Build getBuildByNumber(final int buildNumber) {

    Predicate<Build> isMatchingBuildNumber = new Predicate<Build>() {

        @Override/*ww  w.  j a v a2  s  . c  om*/
        public boolean apply(Build input) {
            return input.getNumber() == buildNumber;
        }
    };

    Optional<Build> optionalBuild = Iterables.tryFind(builds, isMatchingBuildNumber);
    //TODO: Check if we could use Build#NO...instead of Null?
    return optionalBuild.orNull() == null ? null : buildWithClient(optionalBuild.orNull());
}

From source file:org.locationtech.geogig.cli.porcelain.Conflicts.java

private void printConflictDiff(Conflict conflict, ConsoleReader console, GeoGIG geogig) throws IOException {
    FullDiffPrinter diffPrinter = new FullDiffPrinter(false, true);
    console.println("---" + conflict.getPath() + "---");

    ObjectId theirsHeadId;/*from ww  w  . j  av  a 2 s  .c  o m*/
    Optional<Ref> mergeHead = geogig.command(RefParse.class).setName(Ref.MERGE_HEAD).call();
    if (mergeHead.isPresent()) {
        theirsHeadId = mergeHead.get().getObjectId();
    } else {
        File branchFile = new File(getRebaseFolder(), "branch");
        Preconditions.checkState(branchFile.exists(), "Cannot find merge/rebase head reference");
        try {
            String currentBranch = Files.readFirstLine(branchFile, Charsets.UTF_8);
            Optional<Ref> rebaseHead = geogig.command(RefParse.class).setName(currentBranch).call();
            theirsHeadId = rebaseHead.get().getObjectId();
        } catch (IOException e) {
            throw new IllegalStateException("Cannot read current branch info file");
        }

    }
    Optional<RevCommit> theirsHead = geogig.command(RevObjectParse.class).setObjectId(theirsHeadId)
            .call(RevCommit.class);
    ObjectId oursHeadId = geogig.command(RefParse.class).setName(Ref.ORIG_HEAD).call().get().getObjectId();
    Optional<RevCommit> oursHead = geogig.command(RevObjectParse.class).setObjectId(oursHeadId)
            .call(RevCommit.class);
    Optional<ObjectId> commonAncestor = geogig.command(FindCommonAncestor.class).setLeft(theirsHead.get())
            .setRight(oursHead.get()).call();

    String ancestorPath = commonAncestor.get().toString() + ":" + conflict.getPath();
    Optional<NodeRef> ancestorNodeRef = geogig.command(FeatureNodeRefFromRefspec.class).setRefspec(ancestorPath)
            .call();
    String path = Ref.ORIG_HEAD + ":" + conflict.getPath();
    Optional<NodeRef> oursNodeRef = geogig.command(FeatureNodeRefFromRefspec.class).setRefspec(path).call();
    DiffEntry diffEntry = new DiffEntry(ancestorNodeRef.orNull(), oursNodeRef.orNull());
    console.println("Ours");
    diffPrinter.print(geogig, console, diffEntry);
    path = theirsHeadId + ":" + conflict.getPath();
    Optional<NodeRef> theirsNodeRef = geogig.command(FeatureNodeRefFromRefspec.class).setRefspec(path).call();
    diffEntry = new DiffEntry(ancestorNodeRef.orNull(), theirsNodeRef.orNull());
    console.println("Theirs");
    diffPrinter.print(geogig, console, diffEntry);

}

From source file:com.facebook.buck.android.PreDexMerge.java

@Override
public BuildOutput initializeFromDisk(OnDiskBuildInfo onDiskBuildInfo) {
    Optional<Sha1HashCode> primaryDexHash = onDiskBuildInfo.getHash(PRIMARY_DEX_HASH_KEY);
    // We only save the hash for split-dex builds.
    if (dexSplitMode.isShouldSplitDex()) {
        Preconditions.checkState(primaryDexHash.isPresent());
    }/*from w w  w.  j a  v a2  s. c o m*/

    return new BuildOutput(primaryDexHash.orNull(),
            FluentIterable.from(onDiskBuildInfo.getValues(SECONDARY_DEX_DIRECTORIES_KEY).get())
                    .transform(MorePaths.TO_PATH).toSet());
}

From source file:org.rakam.http.WebServiceModule.java

@Inject
public WebServiceModule(Set<HttpService> httpServices, Set<Tag> tags, Set<CustomParameter> customParameters,
        Set<RequestPreProcessorItem> requestPreProcessorItems, Set<WebSocketService> webSocketServices,
        @NotFoundHandler Optional<HttpRequestHandler> requestHandler, HttpServerConfig config) {
    this.httpServices = httpServices;
    this.webSocketServices = webSocketServices;
    this.requestPreProcessorItems = requestPreProcessorItems;
    this.config = config;
    this.tags = tags;
    this.customParameters = customParameters;
    this.requestHandler = requestHandler.orNull();
}

From source file:org.robotninjas.barge.log.RaftLogBase.java

public void votedFor(@Nonnull Optional<Replica> vote) {
    LOGGER.debug("Voting for {}", vote.orNull());
    votedFor = vote;//from   w ww  .j  ava 2s . c o m
    recordVote(vote);
}

From source file:net.shibboleth.idp.session.AbstractIdPSession.java

/** {@inheritDoc} */
@Override/*  w ww . j ava  2  s.  c  o m*/
@Nullable
public SPSession getSPSession(@Nonnull @NotEmpty final String serviceId) {
    Optional<SPSession> mapped = spSessions.get(StringSupport.trimOrNull(serviceId));
    return (mapped != null) ? mapped.orNull() : null;
}

From source file:bear.plugins.misc.Releases.java

public Optional<Release> getRelease(String folder) {
    checkLoaded();/*from w  w  w  .j  av  a  2s.  c  om*/

    Optional<Release> optional = computeRelease(folder);

    if (!folderMap.containsKey(folder)) {
        folderMap.put(folder, optional.orNull());
    }

    if (!folders.contains(folder)) {
        folders.add(folder);
    }

    return fromNullable(folderMap.get(folder));
}

From source file:org.geogit.osm.internal.OSMUnmapOp.java

@Override
public RevTree call() {

    Optional<OSMMappingLogEntry> entry = command(ReadOSMMappingLogEntry.class).setPath(path).call();
    if (entry.isPresent()) {
        Optional<Mapping> opt = command(ReadOSMMapping.class).setEntry(entry.get()).call();
        if (opt.isPresent()) {
            mapping = opt.get();/* ww w  . ja  v a 2  s .  com*/
        }
    }

    Iterator<NodeRef> iter = command(LsTreeOp.class).setReference(path).setStrategy(Strategy.FEATURES_ONLY)
            .call();

    FeatureMapFlusher flusher = new FeatureMapFlusher(getWorkTree());
    while (iter.hasNext()) {
        NodeRef node = iter.next();
        RevFeature revFeature = command(RevObjectParse.class).setObjectId(node.objectId())
                .call(RevFeature.class).get();
        RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(node.getMetadataId())
                .call(RevFeatureType.class).get();
        List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
        ImmutableList<Optional<Object>> values = revFeature.getValues();
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(
                (SimpleFeatureType) revFeatureType.type());
        String id = null;
        for (int i = 0; i < descriptors.size(); i++) {
            PropertyDescriptor descriptor = descriptors.get(i);
            if (descriptor.getName().getLocalPart().equals("id")) {
                id = values.get(i).get().toString();
            }
            Optional<Object> value = values.get(i);
            featureBuilder.set(descriptor.getName(), value.orNull());
        }
        Preconditions.checkNotNull(id, "No 'id' attribute found");
        SimpleFeature feature = featureBuilder.buildFeature(id);
        unmapFeature(feature, flusher);

    }

    flusher.flushAll();

    // The above code will unmap all added or modified elements, but not deleted ones.
    // We now process the deletions, by comparing the current state of the mapped tree
    // with its state just after the mapping was created.

    if (entry.isPresent()) {
        Iterator<DiffEntry> diffs = command(DiffTree.class).setFilterPath(path)
                .setNewTree(getWorkTree().getTree().getId()).setOldTree(entry.get().getPostMappingId()).call();

        while (diffs.hasNext()) {
            DiffEntry diff = diffs.next();
            if (diff.changeType().equals(DiffEntry.ChangeType.REMOVED)) {

                ObjectId featureId = diff.getOldObject().getNode().getObjectId();
                RevFeature revFeature = command(RevObjectParse.class).setObjectId(featureId)
                        .call(RevFeature.class).get();
                RevFeatureType revFeatureType = command(RevObjectParse.class)
                        .setObjectId(diff.getOldObject().getMetadataId()).call(RevFeatureType.class).get();
                List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
                ImmutableList<Optional<Object>> values = revFeature.getValues();
                SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(
                        (SimpleFeatureType) revFeatureType.type());
                String id = null;
                for (int i = 0; i < descriptors.size(); i++) {
                    PropertyDescriptor descriptor = descriptors.get(i);
                    if (descriptor.getName().getLocalPart().equals("id")) {
                        id = values.get(i).get().toString();
                    }
                    Optional<Object> value = values.get(i);
                    featureBuilder.set(descriptor.getName(), value.orNull());
                }
                Preconditions.checkNotNull(id, "No 'id' attribute found");
                SimpleFeature feature = featureBuilder.buildFeature(id);
                Class<?> clazz = feature.getDefaultGeometryProperty().getType().getBinding();
                String deletePath = clazz.equals(Point.class) ? OSMUtils.NODE_TYPE_NAME
                        : OSMUtils.WAY_TYPE_NAME;
                getWorkTree().delete(deletePath, id);
            }
        }
    }

    return getWorkTree().getTree();

}

From source file:org.locationtech.geogig.spring.service.RepositoryService.java

public Integer getDepth(RepositoryProvider provider, String repoName, ObjectId commitId) {
    Optional<Integer> depth = Optional.absent();
    Repository repository = getRepository(provider, repoName);
    if (repository != null) {
        if (commitId != null) {
            depth = Optional.of(repository.graphDatabase().getDepth(commitId));
        } else {//from  www. j  a v  a  2s .  com
            depth = repository.getDepth();
        }
    }
    return depth.orNull();
}

From source file:net.shibboleth.idp.session.AbstractIdPSession.java

/** {@inheritDoc} */
@Override//  w  w w. j  ava 2s  .com
@Nullable
public AuthenticationResult getAuthenticationResult(@Nonnull @NotEmpty final String flowId) {
    Optional<AuthenticationResult> mapped = authenticationResults.get(StringSupport.trimOrNull(flowId));
    return (mapped != null) ? mapped.orNull() : null;
}