FormatInstructionsBuilder.java

1
package com.ancientprogramming.fixedformat4j.format.impl;
2
3
import com.ancientprogramming.fixedformat4j.annotation.Field;
4
import com.ancientprogramming.fixedformat4j.annotation.FixedFormatBoolean;
5
import com.ancientprogramming.fixedformat4j.annotation.FixedFormatDecimal;
6
import com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber;
7
import com.ancientprogramming.fixedformat4j.annotation.FixedFormatPattern;
8
import com.ancientprogramming.fixedformat4j.exception.FixedFormatException;
9
import com.ancientprogramming.fixedformat4j.format.FormatContext;
10
import com.ancientprogramming.fixedformat4j.format.FormatInstructions;
11
import com.ancientprogramming.fixedformat4j.format.data.FixedFormatBooleanData;
12
import com.ancientprogramming.fixedformat4j.format.data.FixedFormatDecimalData;
13
import com.ancientprogramming.fixedformat4j.format.data.FixedFormatNumberData;
14
import com.ancientprogramming.fixedformat4j.format.data.FixedFormatPatternData;
15
16
import java.lang.reflect.AnnotatedElement;
17
import java.lang.reflect.Method;
18
import java.math.RoundingMode;
19
import java.time.LocalDate;
20
import java.time.LocalDateTime;
21
22
import static java.lang.String.format;
23
24
/**
25
 * Builds {@link FormatInstructions} and {@link FormatContext} from field annotations.
26
 *
27
 * @author Jacob von Eyben - <a href="https://eybenconsult.com">https://eybenconsult.com</a>
28
 * @since 1.6.0
29
 */
30
class FormatInstructionsBuilder {
31
32
  FormatInstructions build(AnnotatedElement annotationSource, Field fieldAnno, Class<?> datatype) {
33
    FixedFormatPatternData patternData = patternData(annotationSource.getAnnotation(FixedFormatPattern.class), datatype);
34
    FixedFormatBooleanData booleanData = booleanData(annotationSource.getAnnotation(FixedFormatBoolean.class));
35
    FixedFormatNumberData numberData = numberData(annotationSource.getAnnotation(FixedFormatNumber.class));
36
    FixedFormatDecimalData decimalData = decimalData(annotationSource.getAnnotation(FixedFormatDecimal.class));
37 1 1. build : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::build → KILLED
    return new FormatInstructions(fieldAnno.length(), fieldAnno.align(), fieldAnno.paddingChar(), patternData, booleanData, numberData, decimalData);
38
  }
39
40
  @SuppressWarnings({"unchecked", "rawtypes"})
41
  FormatContext<?> context(Class<?> datatype, Field fieldAnno) {
42 1 1. context : negated conditional → KILLED
    if (fieldAnno != null) {
43 1 1. context : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::context → KILLED
      return new FormatContext(fieldAnno.offset(), datatype, fieldAnno.formatter());
44
    }
45
    return null;
46
  }
47
48
  Class<?> datatype(Method method, Field fieldAnno) {
49 2 1. datatype : negated conditional → KILLED
2. datatype : negated conditional → KILLED
    if (method.getName().startsWith("get") || method.getName().startsWith("is")) {
50 1 1. datatype : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::datatype → KILLED
      return method.getReturnType();
51
    }
52
    throw new FixedFormatException(format(
53
        "Cannot annotate method %s, with %s annotation. %s annotations must be placed on methods starting with 'get' or 'is'",
54
        method.getName(), fieldAnno.getClass().getName(), fieldAnno.getClass().getName()));
55
  }
56
57
  private FixedFormatPatternData patternData(FixedFormatPattern annotation, Class<?> datatype) {
58 1 1. patternData : negated conditional → KILLED
    if (annotation != null) {
59 1 1. patternData : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::patternData → KILLED
      return new FixedFormatPatternData(annotation.value());
60
    }
61 1 1. patternData : negated conditional → KILLED
    if (LocalDate.class.equals(datatype)) {
62 1 1. patternData : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::patternData → KILLED
      return FixedFormatPatternData.LOCALDATE_DEFAULT;
63
    }
64 1 1. patternData : negated conditional → KILLED
    if (LocalDateTime.class.equals(datatype)) {
65 1 1. patternData : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::patternData → KILLED
      return FixedFormatPatternData.DATETIME_DEFAULT;
66
    }
67 1 1. patternData : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::patternData → KILLED
    return FixedFormatPatternData.DEFAULT;
68
  }
69
70
  private FixedFormatBooleanData booleanData(FixedFormatBoolean annotation) {
71 1 1. booleanData : negated conditional → KILLED
    if (annotation != null) {
72 1 1. booleanData : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::booleanData → KILLED
      return new FixedFormatBooleanData(annotation.trueValue(), annotation.falseValue());
73
    }
74 1 1. booleanData : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::booleanData → KILLED
    return FixedFormatBooleanData.DEFAULT;
75
  }
76
77
  private FixedFormatNumberData numberData(FixedFormatNumber annotation) {
78 1 1. numberData : negated conditional → KILLED
    if (annotation != null) {
79 1 1. numberData : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::numberData → KILLED
      return new FixedFormatNumberData(annotation.sign(), annotation.positiveSign(), annotation.negativeSign());
80
    }
81 1 1. numberData : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::numberData → KILLED
    return FixedFormatNumberData.DEFAULT;
82
  }
83
84
  private FixedFormatDecimalData decimalData(FixedFormatDecimal annotation) {
85 1 1. decimalData : negated conditional → KILLED
    if (annotation != null) {
86 1 1. decimalData : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::decimalData → KILLED
      return new FixedFormatDecimalData(annotation.decimals(), annotation.useDecimalDelimiter(), annotation.decimalDelimiter(), RoundingMode.valueOf(annotation.roundingMode()));
87
    }
88 1 1. decimalData : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::decimalData → KILLED
    return FixedFormatDecimalData.DEFAULT;
89
  }
90
}

Mutations

37

1.1
Location : build
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_withFixedFormatNumber_capturesSignConfig()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::build → KILLED

42

1.1
Location : context
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:context_fromFieldAnnotation_hasCorrectOffsetAndType()]
negated conditional → KILLED

43

1.1
Location : context
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:context_fromFieldAnnotation_hasCorrectOffsetAndType()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::context → KILLED

49

1.1
Location : datatype
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:datatype_beanStandardMethod_returnsReturnType()]
negated conditional → KILLED

2.2
Location : datatype
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:datatype_nonBeanStandardMethod_throwsFixedFormatException()]
negated conditional → KILLED

50

1.1
Location : datatype
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:datatype_beanStandardMethod_returnsReturnType()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::datatype → KILLED

58

1.1
Location : patternData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_withFixedFormatNumber_capturesSignConfig()]
negated conditional → KILLED

59

1.1
Location : patternData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_withFixedFormatPattern_capturesPattern()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::patternData → KILLED

61

1.1
Location : patternData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_defaultAnnotations_returnsDefaultData()]
negated conditional → KILLED

62

1.1
Location : patternData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFixedFormatManagerImpl.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFixedFormatManagerImpl]/[method:testLocalDateNoPatternAnnotationUsesIsoLocalDateDefault()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::patternData → KILLED

64

1.1
Location : patternData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_defaultAnnotations_returnsDefaultData()]
negated conditional → KILLED

65

1.1
Location : patternData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFixedFormatManagerImpl.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFixedFormatManagerImpl]/[method:testLocalDateTimeNoPatternAnnotationUsesIsoDefault()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::patternData → KILLED

67

1.1
Location : patternData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_defaultAnnotations_returnsDefaultData()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::patternData → KILLED

71

1.1
Location : booleanData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_withFixedFormatNumber_capturesSignConfig()]
negated conditional → KILLED

72

1.1
Location : booleanData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_withFixedFormatBoolean_capturesTrueFalseValues()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::booleanData → KILLED

74

1.1
Location : booleanData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_defaultAnnotations_returnsDefaultData()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::booleanData → KILLED

78

1.1
Location : numberData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_withFixedFormatNumber_capturesSignConfig()]
negated conditional → KILLED

79

1.1
Location : numberData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_withFixedFormatNumber_capturesSignConfig()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::numberData → KILLED

81

1.1
Location : numberData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_defaultAnnotations_returnsDefaultData()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::numberData → KILLED

85

1.1
Location : decimalData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_withFixedFormatNumber_capturesSignConfig()]
negated conditional → KILLED

86

1.1
Location : decimalData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_withFixedFormatDecimal_capturesDecimals()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::decimalData → KILLED

88

1.1
Location : decimalData
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestFormatInstructionsBuilder]/[method:build_defaultAnnotations_returnsDefaultData()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/FormatInstructionsBuilder::decimalData → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.1