| 1 | /* | |
| 2 | * Copyright 2004 the original author or authors. | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package com.ancientprogramming.fixedformat4j.format.impl; | |
| 17 | ||
| 18 | import com.ancientprogramming.fixedformat4j.exception.FixedFormatException; | |
| 19 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; | |
| 20 | import org.apache.commons.lang3.StringUtils; | |
| 21 | ||
| 22 | import java.text.DateFormat; | |
| 23 | import java.text.ParseException; | |
| 24 | import java.text.SimpleDateFormat; | |
| 25 | import java.util.Date; | |
| 26 | ||
| 27 | /** | |
| 28 | * Formatter for {@link java.util.Date} data. | |
| 29 | * The formatting and parsing is perfomed by using an instance of the {@link SimpleDateFormat} class. | |
| 30 | * | |
| 31 | * @author Jacob von Eyben - <a href="https://eybenconsult.com">https://eybenconsult.com</a> | |
| 32 | * @since 1.0.0 | |
| 33 | */ | |
| 34 | public class DateFormatter extends AbstractPatternFormatter<Date> { | |
| 35 | ||
| 36 | @Override | |
| 37 | protected int computeFormattedLengthForPattern(String pattern) { | |
| 38 |
1
1. computeFormattedLengthForPattern : replaced int return with 0 for com/ancientprogramming/fixedformat4j/format/impl/DateFormatter::computeFormattedLengthForPattern → SURVIVED |
return getFormatter(pattern).format(new Date(0)).length(); |
| 39 | } | |
| 40 | ||
| 41 | /** {@inheritDoc} */ | |
| 42 | public Date asObject(String string, FormatInstructions instructions) throws FixedFormatException { | |
| 43 | Date result = null; | |
| 44 | ||
| 45 |
1
1. asObject : negated conditional → KILLED |
if (!StringUtils.isEmpty(string)) { |
| 46 | try { | |
| 47 | result = getFormatter(instructions.getFixedFormatPatternData().getPattern()).parse(string); | |
| 48 | } catch (ParseException e) { | |
| 49 | throw new FixedFormatException(String.format("Could not parse value[%s] by pattern[%s] to %s", string, instructions.getFixedFormatPatternData().getPattern(), Date.class.getName())); | |
| 50 | } | |
| 51 | } | |
| 52 |
1
1. asObject : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/DateFormatter::asObject → KILLED |
return result; |
| 53 | } | |
| 54 | ||
| 55 | /** {@inheritDoc} */ | |
| 56 | public String asString(Date date, FormatInstructions instructions) { | |
| 57 | String result = null; | |
| 58 |
1
1. asString : negated conditional → KILLED |
if (date != null) { |
| 59 | result = getFormatter(instructions.getFixedFormatPatternData().getPattern()).format(date); | |
| 60 | } | |
| 61 |
1
1. asString : replaced return value with "" for com/ancientprogramming/fixedformat4j/format/impl/DateFormatter::asString → KILLED |
return result; |
| 62 | } | |
| 63 | ||
| 64 | DateFormat getFormatter(String pattern) { | |
| 65 |
1
1. getFormatter : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/DateFormatter::getFormatter → KILLED |
return new SimpleDateFormat(pattern); |
| 66 | } | |
| 67 | } | |
Mutations | ||
| 38 |
1.1 |
|
| 45 |
1.1 |
|
| 52 |
1.1 |
|
| 58 |
1.1 |
|
| 61 |
1.1 |
|
| 65 |
1.1 |