001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.common.parsers.date; 015 016import org.gbif.common.parsers.core.ParseResult; 017 018import java.io.Serializable; 019import java.time.temporal.TemporalAccessor; 020 021import javax.annotation.Nullable; 022 023import org.apache.commons.lang3.ArrayUtils; 024 025/** 026 * Wrap a set of DateTimeFormatters to <code>parse</code> method. 027 * <p> 028 * If the ISO format parsing process fails, use <code>orderings</code> formatters to parse the date. 029 */ 030public class CustomizedTextDateParser implements TemporalParser, Serializable { 031 private DateComponentOrdering[] orderings; 032 private TextDateParser parser = new TextDateParser(); 033 034 /** 035 * @param orderings a set of DateTimeFormatters 036 */ 037 public static TemporalParser getInstance(DateComponentOrdering[] orderings) { 038 CustomizedTextDateParser ptdp = new CustomizedTextDateParser(); 039 ptdp.orderings = orderings; 040 return ptdp; 041 } 042 043 @Override 044 public ParseResult<TemporalAccessor> parse(String input) { 045 if (ArrayUtils.isNotEmpty(orderings)) { 046 return parser.parse(input, orderings); 047 } else { 048 return parser.parse(input); 049 } 050 } 051 052 @Override 053 public ParseResult<TemporalAccessor> parse(String input, 054 @Nullable DateComponentOrdering ordering) { 055 return parser.parse(input, ordering); 056 } 057 058 @Override 059 public ParseResult<TemporalAccessor> parse(String input, 060 @Nullable DateComponentOrdering[] orderings) { 061 return parser.parse(input, orderings); 062 } 063 064 @Override 065 public ParseResult<TemporalAccessor> parse( 066 @Nullable String year, 067 @Nullable String month, 068 @Nullable String day) { 069 return parser.parse(year, month, day); 070 } 071 072 @Override 073 public ParseResult<TemporalAccessor> parse( 074 @Nullable Integer year, 075 @Nullable Integer month, 076 @Nullable Integer day) { 077 return parser.parse(year, month, day); 078 } 079 080 @Override 081 public ParseResult<TemporalAccessor> parse( 082 @Nullable String year, 083 @Nullable String dayOfYear) { 084 return parser.parse(year, dayOfYear); 085 } 086 087 @Override 088 public ParseResult<TemporalAccessor> parse( 089 @Nullable Integer year, 090 @Nullable Integer dayOfYear) { 091 return parser.parse(year, dayOfYear); 092 } 093}