| 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; | |
| 17 | ||
| 18 | /** | |
| 19 | * Handles default formatting and parsing based on FixedFormatAnnotation values. | |
| 20 | * | |
| 21 | * @author Jacob von Eyben - <a href="https://eybenconsult.com">https://eybenconsult.com</a> | |
| 22 | * @since 1.0.0 | |
| 23 | */ | |
| 24 | public abstract class AbstractFixedFormatter<T> implements FixedFormatter<T> { | |
| 25 | /** | |
| 26 | * {@inheritDoc} | |
| 27 | * <p> | |
| 28 | * Strips padding from {@code value} according to the alignment and padding char defined in | |
| 29 | * {@code instructions}, then delegates to {@link #asObject(String, FormatInstructions)}. | |
| 30 | * Returns {@code null} when {@code value} is {@code null}. | |
| 31 | */ | |
| 32 | public T parse(String value, FormatInstructions instructions) { | |
| 33 | T result = null; | |
| 34 |
1
1. parse : negated conditional → KILLED |
if (value != null) { |
| 35 | result = asObject(getRemovePadding(value, instructions), instructions); | |
| 36 | } | |
| 37 |
1
1. parse : replaced return value with null for com/ancientprogramming/fixedformat4j/format/AbstractFixedFormatter::parse → KILLED |
return result; |
| 38 | } | |
| 39 | ||
| 40 | /** | |
| 41 | * Strips padding characters from {@code value} before it is passed to {@link #asObject}. | |
| 42 | * | |
| 43 | * <p>This is the extension point for subclasses that need to customise pre-parse | |
| 44 | * padding removal. Override this method — not {@link #getRemovePadding} — in new code. | |
| 45 | * | |
| 46 | * <p><strong>Call chain (since 1.6.1):</strong> | |
| 47 | * {@link #parse} → {@link #getRemovePadding} → {@code stripPadding} | |
| 48 | * | |
| 49 | * <p>In version 1.7.0 {@link #getRemovePadding} will be removed and {@link #parse} | |
| 50 | * will call {@code stripPadding} directly. | |
| 51 | * | |
| 52 | * @param value the raw field string extracted from the fixed-width record | |
| 53 | * @param instructions formatting metadata (alignment, padding character, length, …) | |
| 54 | * @return the value with padding removed, ready to be converted by {@link #asObject} | |
| 55 | */ | |
| 56 | protected String stripPadding(String value, FormatInstructions instructions) { | |
| 57 |
1
1. stripPadding : replaced return value with "" for com/ancientprogramming/fixedformat4j/format/AbstractFixedFormatter::stripPadding → KILLED |
return instructions.getAlignment().remove(value, instructions.getPaddingChar()); |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * @deprecated Use {@link #stripPadding} instead. This method will be removed in 1.7.0. | |
| 62 | * | |
| 63 | * <p>Kept for binary compatibility: {@link #parse} still calls this method so that | |
| 64 | * subclasses which overrode {@code getRemovePadding} in earlier versions continue to | |
| 65 | * have their logic applied without any code changes. | |
| 66 | * | |
| 67 | * <p><strong>Migration:</strong> rename your override from {@code getRemovePadding} | |
| 68 | * to {@code stripPadding} — the signature is identical. | |
| 69 | * | |
| 70 | * <p><strong>Call chain (since 1.6.1):</strong> | |
| 71 | * {@link #parse} → {@code getRemovePadding} → {@link #stripPadding} | |
| 72 | */ | |
| 73 | @Deprecated | |
| 74 | protected String getRemovePadding(String value, FormatInstructions instructions) { | |
| 75 |
1
1. getRemovePadding : replaced return value with "" for com/ancientprogramming/fixedformat4j/format/AbstractFixedFormatter::getRemovePadding → KILLED |
return stripPadding(value, instructions); |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * {@inheritDoc} | |
| 80 | * <p> | |
| 81 | * Converts {@code value} to its string representation via | |
| 82 | * {@link #asString(Object, FormatInstructions)}, then applies padding and alignment as defined | |
| 83 | * in {@code instructions}. | |
| 84 | */ | |
| 85 | public String format(T value, FormatInstructions instructions) { | |
| 86 |
1
1. format : replaced return value with "" for com/ancientprogramming/fixedformat4j/format/AbstractFixedFormatter::format → KILLED |
return instructions.getAlignment().apply(asString(value, instructions), instructions.getLength(), instructions.getPaddingChar()); |
| 87 | } | |
| 88 | ||
| 89 | /** | |
| 90 | * Converts the trimmed string {@code string} to an instance of {@code T}. | |
| 91 | * Padding has already been removed from {@code string} before this method is called. | |
| 92 | * | |
| 93 | * @param string the raw (padding-stripped) field value | |
| 94 | * @param instructions formatting instructions for the field | |
| 95 | * @return the parsed value, or {@code null} if the field is empty | |
| 96 | */ | |
| 97 | public abstract T asObject(String string, FormatInstructions instructions); | |
| 98 | ||
| 99 | /** | |
| 100 | * Converts {@code obj} to its raw string representation before padding is applied. | |
| 101 | * | |
| 102 | * @param obj the value to convert; may be {@code null} | |
| 103 | * @param instructions formatting instructions for the field | |
| 104 | * @return the string representation of {@code obj} | |
| 105 | */ | |
| 106 | public abstract String asString(T obj, FormatInstructions instructions); | |
| 107 | } | |
Mutations | ||
| 34 |
1.1 |
|
| 37 |
1.1 |
|
| 57 |
1.1 |
|
| 75 |
1.1 |
|
| 86 |
1.1 |