001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023
002package io.cui.tools.formatting;
003
004import static io.cui.tools.collect.MoreCollections.isEmpty;
005import static io.cui.tools.string.MoreStrings.emptyToNull;
006import java.io.Serializable;
007import java.util.ArrayList;
008import java.util.List;
009import java.util.stream.Collectors;
010import lombok.NonNull;
011
012/**
013 * Provide concatenation of strings by using
014 * {@linkplain String#join(CharSequence, CharSequence...)}.
015 * Furthermore, formatter supports different strategies for values handling. (see
016 * {@link ValueHandling})
017 *
018 * @author Eugen Fischer
019 */
020public class SimpleFormatter implements Serializable {
021
022    /**
023     */
024    public enum ValueHandling {
025        /**
026         * Format all available data. If some are null or empty skip them silently.
027         */
028        FORMAT_IF_ANY_AVAILABLE, /**
029         * Format all available data. If one of them is null or empty skip all silently.
030         */
031        FORMAT_IF_ALL_AVAILABLE;
032    }
033
034    /**
035     * serial version UID
036     */
037    private static final long serialVersionUID = -4761082365099064435L;
038    private final String separator;
039    private final ValueHandling handling;
040
041    /**
042     * Concatenate values by separator and return result inside the parentheses. Handle parameter
043     * according defined ValueHandling strategy.
044     *
045     * @param values ellipses of string values
046     *
047     * @return {@code null} if nothing to put in parentheses
048     */
049    public String formatParentheses(final String... values) {
050        return format(cleanUp(values));
051    }
052
053    /**
054     * Concatenate values by separator according defined ValueHandling strategy.
055     *
056     * @param values ellipses of string values
057     *
058     * @return {@code null} if nothing to concatenate
059     */
060    public String format(final String... values) {
061        return getJoined(cleanUp(values));
062    }
063
064    private List<String> cleanUp(final String... values) {
065        final List<String> result = new ArrayList<>(0);
066        if (null != values) {
067            for (final String item : values) {
068                final var value = emptyToNull(item);
069                if (null == value) {
070                    if (ValueHandling.FORMAT_IF_ALL_AVAILABLE.equals(handling)) {
071                        result.clear();
072                        break;
073                    }
074                } else {
075                    result.add(value);
076                }
077            }
078        }
079        return result;
080    }
081
082    private String getJoined(final List<String> values) {
083        if (isEmpty(values)) {
084            return null;
085        }
086        List<String> filtered = values.stream().filter(element -> !isEmpty(element)).collect(Collectors.toList());
087        if (isEmpty(filtered)) {
088            return null;
089        }
090        return String.join(separator, filtered);
091    }
092
093    private String format(final List<String> values) {
094        final var joined = getJoined(values);
095        if (null != joined) {
096            return String.format("(%s)", joined);
097        }
098        return null;
099    }
100
101    /**
102     * @return a newly created instance of {@link SimpleFormatterBuilder}
103     */
104    public static SimpleFormatterBuilder builder() {
105        return new SimpleFormatterBuilder();
106    }
107
108
109    /**
110     * Internal Builder representation
111     */
112    public static class SimpleFormatterBuilder implements Serializable {
113        private static final long serialVersionUID = 6414005370772800008L;
114
115        /**
116         * Use {@linkplain ValueHandling#FORMAT_IF_ALL_AVAILABLE} as value handling strategy
117         *
118         * @return initialized {@link BuilderWithStrategy} with defined value handling strategy
119         */
120        public BuilderWithStrategy skipResultIfAnyValueIsMissing() {
121            return new BuilderWithStrategy(ValueHandling.FORMAT_IF_ALL_AVAILABLE);
122        }
123
124        /**
125         * Use {@linkplain ValueHandling#FORMAT_IF_ANY_AVAILABLE} as value handling strategy
126         *
127         * @return initialized {@link BuilderWithStrategy} with defined value handling strategy
128         */
129        public BuilderWithStrategy ignoreMissingValues() {
130            return new BuilderWithStrategy(ValueHandling.FORMAT_IF_ANY_AVAILABLE);
131        }
132
133        /**
134         * Internal Builder representation incorporating a strategy
135         */
136        public static class BuilderWithStrategy implements Serializable {
137            private static final long serialVersionUID = -1987354973684803562L;
138            private final ValueHandling valueHandlingStrategy;
139
140            protected BuilderWithStrategy(final ValueHandling strategy) {
141                valueHandlingStrategy = strategy;
142            }
143
144            /**
145             * Create SimpleFormatter
146             *
147             * @param separator must not be null
148             *
149             * @return {@link SimpleFormatter} with defined value handling strategy and separator
150             */
151            public SimpleFormatter separatesBy(@NonNull final String separator) {
152                if (separator == null) {
153                    throw new java.lang.NullPointerException("separator is marked non-null but is null");
154                }
155                return new SimpleFormatter(separator, valueHandlingStrategy);
156            }
157
158            @java.lang.Override
159            @java.lang.SuppressWarnings("all")
160            @lombok.Generated
161            public java.lang.String toString() {
162                return "SimpleFormatter.SimpleFormatterBuilder.BuilderWithStrategy(valueHandlingStrategy=" + this.valueHandlingStrategy + ")";
163            }
164
165            @java.lang.Override
166            @java.lang.SuppressWarnings("all")
167            @lombok.Generated
168            public boolean equals(final java.lang.Object o) {
169                if (o == this) return true;
170                if (!(o instanceof SimpleFormatter.SimpleFormatterBuilder.BuilderWithStrategy)) return false;
171                final SimpleFormatter.SimpleFormatterBuilder.BuilderWithStrategy other = (SimpleFormatter.SimpleFormatterBuilder.BuilderWithStrategy) o;
172                if (!other.canEqual((java.lang.Object) this)) return false;
173                final java.lang.Object this$valueHandlingStrategy = this.valueHandlingStrategy;
174                final java.lang.Object other$valueHandlingStrategy = other.valueHandlingStrategy;
175                if (this$valueHandlingStrategy == null ? other$valueHandlingStrategy != null : !this$valueHandlingStrategy.equals(other$valueHandlingStrategy)) return false;
176                return true;
177            }
178
179            @java.lang.SuppressWarnings("all")
180            @lombok.Generated
181            protected boolean canEqual(final java.lang.Object other) {
182                return other instanceof SimpleFormatter.SimpleFormatterBuilder.BuilderWithStrategy;
183            }
184
185            @java.lang.Override
186            @java.lang.SuppressWarnings("all")
187            @lombok.Generated
188            public int hashCode() {
189                final int PRIME = 59;
190                int result = 1;
191                final java.lang.Object $valueHandlingStrategy = this.valueHandlingStrategy;
192                result = result * PRIME + ($valueHandlingStrategy == null ? 43 : $valueHandlingStrategy.hashCode());
193                return result;
194            }
195        }
196
197        @java.lang.Override
198        @java.lang.SuppressWarnings("all")
199        @lombok.Generated
200        public java.lang.String toString() {
201            return "SimpleFormatter.SimpleFormatterBuilder()";
202        }
203
204        @java.lang.Override
205        @java.lang.SuppressWarnings("all")
206        @lombok.Generated
207        public boolean equals(final java.lang.Object o) {
208            if (o == this) return true;
209            if (!(o instanceof SimpleFormatter.SimpleFormatterBuilder)) return false;
210            final SimpleFormatter.SimpleFormatterBuilder other = (SimpleFormatter.SimpleFormatterBuilder) o;
211            if (!other.canEqual((java.lang.Object) this)) return false;
212            return true;
213        }
214
215        @java.lang.SuppressWarnings("all")
216        @lombok.Generated
217        protected boolean canEqual(final java.lang.Object other) {
218            return other instanceof SimpleFormatter.SimpleFormatterBuilder;
219        }
220
221        @java.lang.Override
222        @java.lang.SuppressWarnings("all")
223        @lombok.Generated
224        public int hashCode() {
225            final int result = 1;
226            return result;
227        }
228    }
229
230    @java.lang.SuppressWarnings("all")
231    @lombok.Generated
232    public SimpleFormatter(final String separator, final ValueHandling handling) {
233        this.separator = separator;
234        this.handling = handling;
235    }
236
237    @java.lang.SuppressWarnings("all")
238    @lombok.Generated
239    public String getSeparator() {
240        return this.separator;
241    }
242
243    @java.lang.SuppressWarnings("all")
244    @lombok.Generated
245    public ValueHandling getHandling() {
246        return this.handling;
247    }
248
249    @java.lang.Override
250    @java.lang.SuppressWarnings("all")
251    @lombok.Generated
252    public boolean equals(final java.lang.Object o) {
253        if (o == this) return true;
254        if (!(o instanceof SimpleFormatter)) return false;
255        final SimpleFormatter other = (SimpleFormatter) o;
256        if (!other.canEqual((java.lang.Object) this)) return false;
257        final java.lang.Object this$separator = this.getSeparator();
258        final java.lang.Object other$separator = other.getSeparator();
259        if (this$separator == null ? other$separator != null : !this$separator.equals(other$separator)) return false;
260        final java.lang.Object this$handling = this.getHandling();
261        final java.lang.Object other$handling = other.getHandling();
262        if (this$handling == null ? other$handling != null : !this$handling.equals(other$handling)) return false;
263        return true;
264    }
265
266    @java.lang.SuppressWarnings("all")
267    @lombok.Generated
268    protected boolean canEqual(final java.lang.Object other) {
269        return other instanceof SimpleFormatter;
270    }
271
272    @java.lang.Override
273    @java.lang.SuppressWarnings("all")
274    @lombok.Generated
275    public int hashCode() {
276        final int PRIME = 59;
277        int result = 1;
278        final java.lang.Object $separator = this.getSeparator();
279        result = result * PRIME + ($separator == null ? 43 : $separator.hashCode());
280        final java.lang.Object $handling = this.getHandling();
281        result = result * PRIME + ($handling == null ? 43 : $handling.hashCode());
282        return result;
283    }
284
285    @java.lang.Override
286    @java.lang.SuppressWarnings("all")
287    @lombok.Generated
288    public java.lang.String toString() {
289        return "SimpleFormatter(separator=" + this.getSeparator() + ", handling=" + this.getHandling() + ")";
290    }
291}