|
| 1 | +/* |
| 2 | + * CDDL HEADER START |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * |
| 8 | + * See LICENSE.txt included in this distribution for the specific |
| 9 | + * language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * When distributing Covered Code, include this CDDL HEADER in each |
| 12 | + * file and include the License file at LICENSE.txt. |
| 13 | + * If applicable, add the following below this CDDL HEADER, with the |
| 14 | + * fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | + * information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | + * |
| 17 | + * CDDL HEADER END |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Copyright (c) 2024, Oracle and/or its affiliates. |
| 22 | + * Portions Copyright (c) 2024, Gino Augustine <gino.augustine@oracle.com>. |
| 23 | + */ |
| 24 | +package org.opengrok.indexer.util; |
| 25 | + |
| 26 | +import org.jetbrains.annotations.NotNull; |
| 27 | + |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.Objects; |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.StringJoiner; |
| 33 | +import java.util.function.BiConsumer; |
| 34 | +import java.util.function.BinaryOperator; |
| 35 | +import java.util.function.Function; |
| 36 | +import java.util.function.Supplier; |
| 37 | +import java.util.stream.Collector; |
| 38 | + |
| 39 | +/** |
| 40 | + * Custom collector to collect error messages for list of projects and join them with comma. |
| 41 | + * |
| 42 | + * |
| 43 | + * @author Gino Augustine |
| 44 | + */ |
| 45 | +public class ErrorMessageCollector implements Collector<String, StringJoiner, Optional<String>> { |
| 46 | + |
| 47 | + private final String prefix; |
| 48 | + private final String emptyString; |
| 49 | + private final boolean returnNullWhenEmpty; |
| 50 | + |
| 51 | + /** |
| 52 | + * Creates a collector with given prefix and |
| 53 | + * returns optional empty for empty collection. |
| 54 | + * @param prefix prefix before the joined string |
| 55 | + */ |
| 56 | + public ErrorMessageCollector(@NotNull String prefix) { |
| 57 | + Objects.requireNonNull(prefix); |
| 58 | + this.prefix = prefix; |
| 59 | + this.emptyString = ""; |
| 60 | + this.returnNullWhenEmpty = true; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Creates a string joiner with given prefix and empty string. |
| 65 | + * @param prefix prefix before the joined string |
| 66 | + * @param emptyString string to display if collection is empty |
| 67 | + */ |
| 68 | + public ErrorMessageCollector(@NotNull String prefix, @NotNull String emptyString) { |
| 69 | + Objects.requireNonNull(prefix); |
| 70 | + Objects.requireNonNull(emptyString); |
| 71 | + this.prefix = prefix; |
| 72 | + this.emptyString = emptyString; |
| 73 | + this.returnNullWhenEmpty = false; |
| 74 | + |
| 75 | + } |
| 76 | + /** |
| 77 | + * A function that creates and returns a new mutable result container. |
| 78 | + * |
| 79 | + * @return a function which returns a new, mutable result container |
| 80 | + */ |
| 81 | + @Override |
| 82 | + public Supplier<StringJoiner> supplier() { |
| 83 | + return () -> { |
| 84 | + var joiner = new StringJoiner(", ", prefix, ""); |
| 85 | + joiner.setEmptyValue(emptyString); |
| 86 | + return joiner; |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * A function that folds a value into a mutable result container. |
| 92 | + * |
| 93 | + * @return a function which folds a value into a mutable result container |
| 94 | + */ |
| 95 | + @Override |
| 96 | + public BiConsumer<StringJoiner, String> accumulator() { |
| 97 | + return StringJoiner::add; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * A function that accepts two partial results and merges them. The |
| 102 | + * combiner function may fold state from one argument into the other and |
| 103 | + * return that, or may return a new result container. |
| 104 | + * |
| 105 | + * @return a function which combines two partial results into a combined |
| 106 | + * result |
| 107 | + */ |
| 108 | + @Override |
| 109 | + public BinaryOperator<StringJoiner> combiner() { |
| 110 | + return StringJoiner::merge; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Perform the final transformation from the intermediate accumulation type |
| 115 | + * {@code A} to the final result type {@code R}. |
| 116 | + * |
| 117 | + * <p>If the characteristic {@code IDENTITY_FINISH} is |
| 118 | + * set, this function may be presumed to be an identity transform with an |
| 119 | + * unchecked cast from {@code A} to {@code R}. |
| 120 | + * |
| 121 | + * @return a function which transforms the intermediate result to the final |
| 122 | + * result |
| 123 | + */ |
| 124 | + @Override |
| 125 | + public Function<StringJoiner, Optional<String>> finisher() { |
| 126 | + return stringJoiner -> |
| 127 | + Optional.of(stringJoiner.toString()) |
| 128 | + .filter(msg -> !(msg.isEmpty() && returnNullWhenEmpty)); |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns a {@code Set} of {@code Collector.Characteristics} indicating |
| 134 | + * the characteristics of this Collector. This set should be immutable. |
| 135 | + * |
| 136 | + * @return an immutable set of collector characteristics |
| 137 | + */ |
| 138 | + @Override |
| 139 | + public Set<Characteristics> characteristics() { |
| 140 | + return Collections.emptySet(); |
| 141 | + } |
| 142 | +} |
0 commit comments