|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.common.time; |
| 21 | + |
| 22 | +import java.time.ZoneId; |
| 23 | +import java.time.format.DateTimeParseException; |
| 24 | +import java.time.temporal.TemporalAccessor; |
| 25 | +import java.time.temporal.TemporalField; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +public interface DateFormatter { |
| 31 | + |
| 32 | + /** |
| 33 | + * Try to parse input to a java time TemporalAccessor |
| 34 | + * @param input An arbitrary string resembling the string representation of a date or time |
| 35 | + * @throws DateTimeParseException If parsing fails, this exception will be thrown. |
| 36 | + * Note that it can contained suppressed exceptions when several formatters failed parse this value |
| 37 | + * @return The java time object containing the parsed input |
| 38 | + */ |
| 39 | + TemporalAccessor parse(String input); |
| 40 | + |
| 41 | + /** |
| 42 | + * Create a copy of this formatter that is configured to parse dates in the specified time zone |
| 43 | + * |
| 44 | + * @param zoneId The time zone to act on |
| 45 | + * @return A copy of the date formatter this has been called on |
| 46 | + */ |
| 47 | + DateFormatter withZone(ZoneId zoneId); |
| 48 | + |
| 49 | + /** |
| 50 | + * Print the supplied java time accessor in a string based representation according to this formatter |
| 51 | + * |
| 52 | + * @param accessor The temporal accessor used to format |
| 53 | + * @return The string result for the formatting |
| 54 | + */ |
| 55 | + String format(TemporalAccessor accessor); |
| 56 | + |
| 57 | + /** |
| 58 | + * A name based format for this formatter. Can be one of the registered formatters like <code>epoch_millis</code> or |
| 59 | + * a configured format like <code>HH:mm:ss</code> |
| 60 | + * |
| 61 | + * @return The name of this formatter |
| 62 | + */ |
| 63 | + String pattern(); |
| 64 | + |
| 65 | + /** |
| 66 | + * Configure a formatter using default fields for a TemporalAccessor that should be used in case |
| 67 | + * the supplied date is not having all of those fields |
| 68 | + * |
| 69 | + * @param fields A <code>Map<TemporalField, Long></code> of fields to be used as fallbacks |
| 70 | + * @return A new date formatter instance, that will use those fields during parsing |
| 71 | + */ |
| 72 | + DateFormatter parseDefaulting(Map<TemporalField, Long> fields); |
| 73 | + |
| 74 | + /** |
| 75 | + * Merge several date formatters into a single one. Useful if you need to have several formatters with |
| 76 | + * different formats act as one, for example when you specify a |
| 77 | + * format like <code>date_hour||epoch_millis</code> |
| 78 | + * |
| 79 | + * @param formatters The list of date formatters to be merged together |
| 80 | + * @return The new date formtter containing the specified date formatters |
| 81 | + */ |
| 82 | + static DateFormatter merge(DateFormatter ... formatters) { |
| 83 | + return new MergedDateFormatter(formatters); |
| 84 | + } |
| 85 | + |
| 86 | + class MergedDateFormatter implements DateFormatter { |
| 87 | + |
| 88 | + private final String format; |
| 89 | + private final DateFormatter[] formatters; |
| 90 | + |
| 91 | + MergedDateFormatter(DateFormatter ... formatters) { |
| 92 | + this.formatters = formatters; |
| 93 | + this.format = Arrays.stream(formatters).map(DateFormatter::pattern).collect(Collectors.joining("||")); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public TemporalAccessor parse(String input) { |
| 98 | + DateTimeParseException failure = null; |
| 99 | + for (DateFormatter formatter : formatters) { |
| 100 | + try { |
| 101 | + return formatter.parse(input); |
| 102 | + } catch (DateTimeParseException e) { |
| 103 | + if (failure == null) { |
| 104 | + failure = e; |
| 105 | + } else { |
| 106 | + failure.addSuppressed(e); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + throw failure; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public DateFormatter withZone(ZoneId zoneId) { |
| 115 | + return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.withZone(zoneId)).toArray(DateFormatter[]::new)); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String format(TemporalAccessor accessor) { |
| 120 | + return formatters[0].format(accessor); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public String pattern() { |
| 125 | + return format; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public DateFormatter parseDefaulting(Map<TemporalField, Long> fields) { |
| 130 | + return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.parseDefaulting(fields)).toArray(DateFormatter[]::new)); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments