|
| 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.joda; |
| 21 | + |
| 22 | +import org.elasticsearch.common.time.DateFormatter; |
| 23 | +import org.elasticsearch.common.time.FormatNames; |
| 24 | + |
| 25 | +import java.util.LinkedHashMap; |
| 26 | +import java.util.LinkedHashSet; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.stream.Collectors; |
| 31 | + |
| 32 | +public class JodaDeprecationPatterns { |
| 33 | + public static final String USE_NEW_FORMAT_SPECIFIERS = "Use new java.time date format specifiiers."; |
| 34 | + private static Map<String, String> JODA_PATTERNS_DEPRECATIONS = new LinkedHashMap<>(); |
| 35 | + |
| 36 | + static { |
| 37 | + JODA_PATTERNS_DEPRECATIONS.put("Y", "'Y' year-of-era should be replaced with 'y'. Use 'Y' for week-based-year."); |
| 38 | + JODA_PATTERNS_DEPRECATIONS.put("y", "'y' year should be replaced with 'u'. Use 'y' for year-of-era."); |
| 39 | + JODA_PATTERNS_DEPRECATIONS.put("C", "'C' century of era is no longer supported."); |
| 40 | + JODA_PATTERNS_DEPRECATIONS.put("x", "'x' weak-year should be replaced with 'Y'. Use 'x' for zone-offset."); |
| 41 | + JODA_PATTERNS_DEPRECATIONS.put("Z", |
| 42 | + "'Z' time zone offset/id fails when parsing 'Z' for Zulu timezone. Consider using 'X'."); |
| 43 | + JODA_PATTERNS_DEPRECATIONS.put("z", |
| 44 | + "'z' time zone text. Will print 'Z' for Zulu given UTC timezone."); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Checks if date parsing pattern is deprecated. |
| 49 | + * Deprecated here means: when it was not already prefixed with 8 (meaning already upgraded) |
| 50 | + * and it is not a predefined pattern from <code>FormatNames</code> like basic_date_time_no_millis |
| 51 | + * and it uses pattern characters which changed meaning from joda to java like Y becomes y. |
| 52 | + * @param pattern - a format to be checked |
| 53 | + * @return true if format is deprecated, otherwise false |
| 54 | + */ |
| 55 | + public static boolean isDeprecatedPattern(String pattern) { |
| 56 | + List<String> patterns = DateFormatter.splitCombinedPatterns(pattern); |
| 57 | + |
| 58 | + for (String subPattern : patterns) { |
| 59 | + boolean isDeprecated = subPattern.startsWith("8") == false && FormatNames.exist(subPattern) == false && |
| 60 | + JODA_PATTERNS_DEPRECATIONS.keySet().stream() |
| 61 | + .filter(s -> subPattern.contains(s)) |
| 62 | + .findAny() |
| 63 | + .isPresent(); |
| 64 | + if (isDeprecated) { |
| 65 | + return true; |
| 66 | + } |
| 67 | + } |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Formats deprecation message for suggestion field in a warning header. |
| 73 | + * Joins all warnings in a one message. |
| 74 | + * @param pattern - a pattern to be formatted |
| 75 | + * @return a formatted deprecation message |
| 76 | + */ |
| 77 | + public static String formatSuggestion(String pattern) { |
| 78 | + List<String> patterns = DateFormatter.splitCombinedPatterns(pattern); |
| 79 | + |
| 80 | + Set<String> warnings = new LinkedHashSet<>(); |
| 81 | + for (String subPattern : patterns) { |
| 82 | + if (isDeprecatedPattern(subPattern)) { |
| 83 | + String suggestion = JODA_PATTERNS_DEPRECATIONS.entrySet().stream() |
| 84 | + .filter(s -> subPattern.contains(s.getKey())) |
| 85 | + .map(s -> s.getValue()) |
| 86 | + .collect(Collectors.joining("; ")); |
| 87 | + warnings.add(suggestion); |
| 88 | + } |
| 89 | + } |
| 90 | + String combinedWarning = warnings.stream() |
| 91 | + .collect(Collectors.joining("; ")); |
| 92 | + return combinedWarning; |
| 93 | + } |
| 94 | +} |
0 commit comments