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:org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec.java

protected TypeDefinitionAwareCodec(final Optional<T> typeDefinition, final Class<J> outputClass) {
    Preconditions.checkArgument(outputClass != null, "Output class must be specified.");
    this.typeDefinition = typeDefinition.orNull();
    this.inputClass = outputClass;
}

From source file:org.geogit.osm.map.internal.OSMMapOp.java

private Iterator<Feature> getFeatures(String ref) {
    Optional<ObjectId> id = command(RevParse.class).setRefSpec(ref).call();
    if (!id.isPresent()) {
        return Iterators.emptyIterator();
    }/*from www. j  a  va2 s.  c o  m*/
    LsTreeOp op = command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);

    Iterator<NodeRef> iterator = op.call();

    Function<NodeRef, Feature> function = new Function<NodeRef, Feature>() {

        @Override
        @Nullable
        public Feature apply(@Nullable NodeRef ref) {
            RevFeature revFeature = command(RevObjectParse.class).setObjectId(ref.objectId())
                    .call(RevFeature.class).get();
            SimpleFeatureType featureType;
            if (ref.path().startsWith(OSMUtils.NODE_TYPE_NAME)) {
                featureType = OSMUtils.nodeType();
            } else {
                featureType = OSMUtils.wayType();
            }
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
            RevFeatureType revFeatureType = RevFeatureType.build(featureType);
            List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
            ImmutableList<Optional<Object>> values = revFeature.getValues();
            for (int i = 0; i < descriptors.size(); i++) {
                PropertyDescriptor descriptor = descriptors.get(i);
                Optional<Object> value = values.get(i);
                featureBuilder.set(descriptor.getName(), value.orNull());
            }
            SimpleFeature feature = featureBuilder.buildFeature(ref.name());
            return feature;
        }

    };
    return Iterators.transform(iterator, function);
}

From source file:org.incode.module.commchannel.dom.impl.phoneorfax.PhoneOrFaxNumberRepository.java

@Programmatic
public PhoneOrFaxNumber findByPhoneOrFaxNumber(final Object owner, final String phoneNumber) {

    final Optional<PhoneOrFaxNumber> phoneNumberIfFound = findByPhoneOrFaxNumber(owner, phoneNumber,
            CommunicationChannelType.PHONE_NUMBER);
    if (phoneNumberIfFound.isPresent()) {
        return phoneNumberIfFound.get();
    }/* ww  w .  j a  v a  2 s. c om*/

    final Optional<PhoneOrFaxNumber> faxNumberIfFound = findByPhoneOrFaxNumber(owner, phoneNumber,
            CommunicationChannelType.FAX_NUMBER);
    return faxNumberIfFound.orNull();
}

From source file:org.onos.yangtools.yang.model.util.repo.SourceIdentifier.java

/**
 *
 * Creates new YANG Schema source identifier.
 *
 * @param name Name of schema//w w w .  j ava  2s  .c o  m
 * @param formattedRevision Revision of source in format YYYY-mm-dd
 */
public SourceIdentifier(final String name, final Optional<String> formattedRevision) {
    super();
    this.name = name;
    this.revision = formattedRevision.orNull();
}

From source file:org.locationtech.geogig.plumbing.index.MaterializedBuilderConsumer.java

private @Nullable Node materialize(@Nullable Node node, Map<ObjectId, RevFeature> objects) {
    Node materialized = null;//from   ww w  .  j a v a2  s.  co  m
    if (null != node) {
        ObjectId objectId = node.getObjectId();
        RevFeature f = objects.get(objectId);
        Preconditions.checkState(f != null, "Feature %s of node '%s' not found", objectId, node.getName());
        Map<String, Object> atts = new HashMap<>();
        extraDataProperties.forEach((attName, attIndex) -> {
            Optional<Object> value = f.get(attIndex.intValue());
            atts.put(attName, value.orNull());
        });

        Map<String, Object> extraData = node.getExtraData();
        if (extraData == null) {
            extraData = new HashMap<>();
        }
        extraData.put(IndexInfo.FEATURE_ATTRIBUTES_EXTRA_DATA, atts);

        String name = node.getName();
        ObjectId metadataId = node.getMetadataId().or(ObjectId.NULL);
        TYPE type = node.getType();
        Envelope orNull = node.bounds().orNull();
        materialized = Node.create(name, objectId, metadataId, type, orNull, extraData);
    }
    return materialized;
}

From source file:org.incode.module.commchannel.dom.impl.emailaddress.EmailAddressRepository.java

@Programmatic
public EmailAddress findByEmailAddress(final Object owner, final String emailAddress) {

    final List<CommunicationChannelOwnerLink> links = linkRepository
            .findByOwnerAndCommunicationChannelType(owner, CommunicationChannelType.EMAIL_ADDRESS);
    final Iterable<EmailAddress> emailAddresses = Iterables.transform(links,
            CommunicationChannelOwnerLink.Functions.communicationChannel(EmailAddress.class));
    final Optional<EmailAddress> emailAddressIfFound = Iterables.tryFind(emailAddresses,
            input -> Objects.equals(emailAddress, input.getEmailAddress()));
    return emailAddressIfFound.orNull();
}

From source file:net.pterodactylus.sone.web.ViewSonePage.java

/**
 * {@inheritDoc}//w  ww . j a va2s  . c o  m
 */
@Override
protected void processTemplate(FreenetRequest request, TemplateContext templateContext)
        throws RedirectException {
    super.processTemplate(request, templateContext);
    String soneId = request.getHttpRequest().getParam("sone");
    Optional<Sone> sone = webInterface.getCore().getSone(soneId);
    templateContext.set("sone", sone.orNull());
    templateContext.set("soneId", soneId);
    if (!sone.isPresent()) {
        return;
    }
    List<Post> sonePosts = sone.get().getPosts();
    sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone.get().getId()));
    Collections.sort(sonePosts, Post.TIME_COMPARATOR);
    Pagination<Post> postPagination = new Pagination<Post>(sonePosts,
            webInterface.getCore().getPreferences().getPostsPerPage())
                    .setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0));
    templateContext.set("postPagination", postPagination);
    templateContext.set("posts", postPagination.getItems());
    Set<PostReply> replies = sone.get().getReplies();
    final Map<Post, List<PostReply>> repliedPosts = new HashMap<Post, List<PostReply>>();
    for (PostReply reply : replies) {
        Optional<Post> post = reply.getPost();
        if (!post.isPresent() || repliedPosts.containsKey(post.get()) || sone.get().equals(post.get().getSone())
                || (sone.get().getId().equals(post.get().getRecipientId().orNull()))) {
            continue;
        }
        repliedPosts.put(post.get(), webInterface.getCore().getReplies(post.get().getId()));
    }
    List<Post> posts = new ArrayList<Post>(repliedPosts.keySet());
    Collections.sort(posts, new Comparator<Post>() {

        @Override
        public int compare(Post leftPost, Post rightPost) {
            return (int) Math.min(Integer.MAX_VALUE,
                    Math.max(Integer.MIN_VALUE, repliedPosts.get(rightPost).get(0).getTime()
                            - repliedPosts.get(leftPost).get(0).getTime()));
        }

    });

    Pagination<Post> repliedPostPagination = new Pagination<Post>(posts,
            webInterface.getCore().getPreferences().getPostsPerPage())
                    .setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("repliedPostPage"), 0));
    templateContext.set("repliedPostPagination", repliedPostPagination);
    templateContext.set("repliedPosts", repliedPostPagination.getItems());
}

From source file:org.incode.module.commchannel.dom.impl.postaladdress.PostalAddressRepository.java

@Programmatic
public PostalAddress findByAddress(final Object owner, final String placeId) {

    final List<CommunicationChannelOwnerLink> links = linkRepository
            .findByOwnerAndCommunicationChannelType(owner, CommunicationChannelType.POSTAL_ADDRESS);
    final Iterable<PostalAddress> postalAddresses = Iterables.transform(links,
            CommunicationChannelOwnerLink.Functions.communicationChannel(PostalAddress.class));
    final Optional<PostalAddress> postalAddressIfFound = Iterables.tryFind(postalAddresses,
            input -> Objects.equals(placeId, input.getPlaceId()));
    return postalAddressIfFound.orNull();
}

From source file:im.dadoo.teak.web.controller.ArchiveController.java

@RequestMapping(value = "/admin/archive", method = RequestMethod.POST)
public String save(@RequestParam String title, @RequestParam(required = false) String author,
        @RequestParam(required = false) String html, @RequestParam long categoryId,
        @RequestParam(required = false) MultipartFile thumbnail) throws IllegalStateException, IOException {
    Optional<String> path = this.fileAO.save(thumbnail);
    Optional<ArchivePO> archiveOPO = this.defaultArchiveBO.insert(title, author, html, path.orNull(),
            categoryId);/*from w  w  w.  j  av  a  2  s .  c om*/
    if (archiveOPO.isPresent()) {
        return "redirect:/admin/archive";
    } else {
        return "redirect:/404";
    }
}

From source file:org.geogit.api.plumbing.merge.MergeFeaturesOp.java

private Feature merge(RevFeature featureA, RevFeature featureB, RevFeature ancestor,
        RevFeatureType featureType) {/*  w  w  w.  j a  v  a 2 s.  co  m*/

    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) featureType.type());
    ImmutableList<Optional<Object>> valuesA = featureA.getValues();
    ImmutableList<Optional<Object>> valuesB = featureB.getValues();
    ImmutableList<Optional<Object>> valuesAncestor = ancestor.getValues();
    ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
    for (int i = 0; i < descriptors.size(); i++) {
        Name name = descriptors.get(i).getName();
        Optional<Object> valueAncestor = valuesAncestor.get(i);
        Optional<Object> valueA = valuesA.get(i);
        if (!valueA.equals(valueAncestor)) {
            featureBuilder.set(name, valueA.orNull());
        } else {
            Optional<Object> valueB = valuesB.get(i);
            if (!valueB.equals(valueAncestor)) {
                featureBuilder.set(name, valueB.orNull());
            } else {
                featureBuilder.set(name, valueAncestor.orNull());
            }
        }
    }
    return featureBuilder.buildFeature(nodeRefA.name());

}