Example usage for com.google.common.collect MoreCollectors onlyElement

List of usage examples for com.google.common.collect MoreCollectors onlyElement

Introduction

In this page you can find the example usage for com.google.common.collect MoreCollectors onlyElement.

Prototype

@SuppressWarnings("unchecked")
public static <T> Collector<T, ?, T> onlyElement() 

Source Link

Document

A collector that takes a stream containing exactly one element and returns that element.

Usage

From source file:org.ambraproject.wombat.service.ArticleServiceImpl.java

@Override
public ContentKey getManuscriptKey(ArticlePointer articleId) throws IOException {
    Map<String, ?> itemTable = getItemTable(articleId);
    Map<String, ?> articleItem = (Map<String, ?>) itemTable.values().stream()
            .filter(itemObj -> ((Map<String, ?>) itemObj).get("itemType").equals("article"))
            .collect(MoreCollectors.onlyElement());
    Map<String, ?> articleFiles = (Map<String, ?>) articleItem.get("files");
    Map<String, ?> manuscriptPointer = (Map<String, ?>) articleFiles.get("manuscript");

    String crepoKey = (String) manuscriptPointer.get("crepoKey");
    UUID crepoUuid = UUID.fromString((String) manuscriptPointer.get("crepoUuid"));
    ContentKey key = ContentKey.createForUuid(crepoKey, crepoUuid);
    String bucketName = (String) manuscriptPointer.get("bucketName");
    key.setBucketName(bucketName);//w  ww.  j av a  2 s. c  om
    return key;
}

From source file:org.ambraproject.wombat.controller.FigurePageController.java

/**
 * Serve a page displaying a single figure.
 *//* w w  w  . j ava2  s  .co m*/
@RequestMapping(name = "figurePage", value = "/article/figure")
public String renderFigurePage(Model model, @SiteParam Site site, RequestedDoiVersion figureId)
        throws IOException {
    AssetPointer assetPointer = articleResolutionService.toParentIngestion(figureId);
    model.addAttribute("figurePtr", assetPointer.asParameterMap());

    ArticlePointer articlePointer = assetPointer.getParentArticle();
    RequestedDoiVersion articleId = figureId.forDoi(articlePointer.getDoi());

    ArticleMetadata articleMetadata = articleMetadataFactory.get(site, articleId, articlePointer);
    model.addAttribute("article", articleMetadata.getIngestionMetadata());

    Map<String, ?> figureMetadata = articleMetadata.getFigureView().stream()
            .filter((Map<String, ?> fig) -> fig.get("doi").equals(assetPointer.getAssetDoi()))
            .collect(MoreCollectors.onlyElement());
    model.addAttribute("figure", figureMetadata);

    String descriptionHtml = getDescriptionHtml(site, articlePointer, figureMetadata);
    model.addAttribute("descriptionHtml", descriptionHtml);

    return site + "/ftl/article/figure";
}

From source file:com.google.errorprone.bugpatterns.TypeParameterShadowing.java

private static SuggestedFix renameTypeVariable(Tree sourceTree,
        List<? extends TypeParameterTree> typeParameters, Name typeVariable, String typeVarReplacement,
        VisitorState state) {//ww  w.j a v  a  2s.c o m

    TypeParameterTree matchingTypeParam = typeParameters.stream()
            .filter(t -> t.getName().contentEquals(typeVariable)).collect(MoreCollectors.onlyElement());
    Symbol typeVariableSymbol = ASTHelpers.getSymbol(matchingTypeParam);

    // replace only the type parameter name (and not any upper bounds)
    String name = matchingTypeParam.getName().toString();
    int pos = ((JCTree) matchingTypeParam).getStartPosition();
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder().replace(pos, pos + name.length(),
            typeVarReplacement);

    ((JCTree) sourceTree).accept(new TreeScanner() {
        @Override
        public void visitIdent(JCTree.JCIdent tree) {
            Symbol identSym = ASTHelpers.getSymbol(tree);
            if (Objects.equal(identSym, typeVariableSymbol)) {
                // Lambda parameters can be desugared early, so we need to make sure the source
                // is there. In the example below, we would try to suggest replacing the node 't'
                // with T2, since the compiler desugars to g((T t) -> false). The extra condition
                // prevents us from doing that.

                // Foo<T> {
                //   <G> void g(Predicate<G> p) {},
                //   <T> void blah() {
                //     g(t -> false);
                //   }
                // }
                if (Objects.equal(state.getSourceForNode(tree), name)) {
                    fixBuilder.replace(tree, typeVarReplacement);
                }
            }
        }
    });
    return fixBuilder.build();
}