Sign.java

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.annotation;
17
18
import com.ancientprogramming.fixedformat4j.format.FormatInstructions;
19
import org.apache.commons.lang3.StringUtils;
20
21
/**
22
 * Sign defines where to place a sign defining a positive or negative number.
23
 * Is to be used in formatters operating numbers.
24
 * 
25
 * @author Jacob von Eyben - <a href="https://eybenconsult.com">https://eybenconsult.com</a>
26
 * @since 1.1.0
27
 */
28
public enum Sign {
29
30
  /**
31
   * Doesn't do anything with signs.
32
   * This just delegate to the {@link Align} defined in {@link FormatInstructions}.
33
   */
34
  NOSIGN {
35
    /** {@inheritDoc} */
36
    public String apply(String value, FormatInstructions instructions) {
37 1 1. apply : replaced return value with "" for com/ancientprogramming/fixedformat4j/annotation/Sign$1::apply → KILLED
      return instructions.getAlignment().apply(value, instructions.getLength(), instructions.getPaddingChar());
38
    }
39
40
    /** {@inheritDoc} */
41
    public String remove(String value, FormatInstructions instructions) {
42
      String result =  instructions.getAlignment().remove(value, instructions.getPaddingChar());
43 1 1. remove : negated conditional → KILLED
      if (StringUtils.isEmpty(result)) {
44
        result = "0";
45
      }
46 1 1. remove : replaced return value with "" for com/ancientprogramming/fixedformat4j/annotation/Sign$1::remove → KILLED
      return result;
47
48
    }
49
  },
50
51
  /**
52
   * Prepend the sign to the string
53
   */
54
  PREPEND {
55
    /** {@inheritDoc} */
56
    public String apply(String value, FormatInstructions instructions) {
57
      String sign = StringUtils.substring(value, 0, 1);
58 1 1. apply : negated conditional → KILLED
      if ("-".equals(sign)) {
59
        value = StringUtils.substring(value, 1);
60
      } else {
61
        sign = "+";
62
      }
63
      String result = instructions.getAlignment().apply(value, instructions.getLength(), instructions.getPaddingChar());
64 1 1. apply : replaced return value with "" for com/ancientprogramming/fixedformat4j/annotation/Sign$2::apply → KILLED
      return sign + StringUtils.substring(result, 1);
65
    }
66
67
    /** {@inheritDoc} */
68
    public String remove(String value, FormatInstructions instructions) {
69
      String sign = StringUtils.substring(value, 0, 1);
70
      String valueWithoutSign = StringUtils.substring(value, 1);
71
      String result = instructions.getAlignment().remove(valueWithoutSign, instructions.getPaddingChar());
72 1 1. remove : negated conditional → KILLED
      if (removeSign(instructions, sign, result)) {
73
        sign = "";
74
      }
75
76 1 1. remove : negated conditional → KILLED
      if (StringUtils.isEmpty(result)) {
77
        result = "0";
78
      }
79 1 1. remove : replaced return value with "" for com/ancientprogramming/fixedformat4j/annotation/Sign$2::remove → KILLED
      return sign + result;
80
    }
81
  },
82
83
  /**
84
   * Append the sign to the string
85
   */
86
  APPEND {
87
    /** {@inheritDoc} */
88
    public String apply(String value, FormatInstructions instructions) {
89
      String sign = StringUtils.substring(value, 0, 1);
90 1 1. apply : negated conditional → KILLED
      if ("-".equals(sign)) {
91
        value = StringUtils.substring(value, 1);
92
      } else {
93
        sign = "+";
94
      }
95
      String result = instructions.getAlignment().apply(value, instructions.getLength(), instructions.getPaddingChar());
96 1 1. apply : replaced return value with "" for com/ancientprogramming/fixedformat4j/annotation/Sign$3::apply → KILLED
      return StringUtils.substring(result, 1) + sign;
97
98
    }
99
    /** {@inheritDoc} */
100
    public String remove(String value, FormatInstructions instructions) {
101 1 1. remove : Replaced integer subtraction with addition → KILLED
      String sign = StringUtils.substring(value, value.length()-1);
102 1 1. remove : Replaced integer subtraction with addition → KILLED
      String valueWithoutSign = StringUtils.substring(value, 0, value.length()-1);
103
      String result = instructions.getAlignment().remove(valueWithoutSign, instructions.getPaddingChar());
104 1 1. remove : negated conditional → KILLED
      if (removeSign(instructions, sign, result)) {
105
        sign = "";
106
      }
107 1 1. remove : negated conditional → KILLED
      if (StringUtils.isEmpty(result)) {
108
        result = "0";
109
      }
110 1 1. remove : replaced return value with "" for com/ancientprogramming/fixedformat4j/annotation/Sign$3::remove → KILLED
      return sign + result;
111
    }
112
  };
113
114
  /**
115
   *remove sign in three cases:
116
   * 1. positive sign
117
   * 2. the unsigned value is empty (can happen if paddingchar is 0 and the value is zero)
118
   * 3. the unsigned value is 0 (can happen if paddingchar isn't 0 and the value is zero)
119
   * @param instructions
120
   * @param sign
121
   * @param valueWithoutSign
122
   * @return
123
   */
124
  private static boolean removeSign(FormatInstructions instructions, String sign, String valueWithoutSign) {
125 2 1. removeSign : replaced boolean return with true for com/ancientprogramming/fixedformat4j/annotation/Sign::removeSign → KILLED
2. removeSign : negated conditional → KILLED
    return instructions.getFixedFormatNumberData().getPositiveSign().equals(sign.charAt(0)) ||
126 1 1. removeSign : negated conditional → KILLED
        StringUtils.isEmpty(valueWithoutSign) ||
127 1 1. removeSign : negated conditional → KILLED
        "0".equals(valueWithoutSign);
128
  }
129
130
131
  /**
132
   * Applies the sign to {@code value} according to this strategy, padding the result to the
133
   * length specified in {@code instructions}.
134
   *
135
   * @param value        the raw numeric string (may include a leading {@code -})
136
   * @param instructions the formatting instructions for the field
137
   * @return the sign-formatted string padded to the required length
138
   */
139
  public abstract String apply(String value, FormatInstructions instructions);
140
141
  /**
142
   * Strips the sign character from {@code value} and returns the bare numeric string.
143
   *
144
   * @param value        the field value as read from the fixed-width record
145
   * @param instructions the formatting instructions for the field
146
   * @return the numeric string with the sign and padding removed
147
   */
148
  public abstract String remove(String value, FormatInstructions instructions);
149
}

Mutations

37

1.1
Location : apply
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignNoSign()]
replaced return value with "" for com/ancientprogramming/fixedformat4j/annotation/Sign$1::apply → KILLED

43

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

46

1.1
Location : remove
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestShortFormatter.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestShortFormatter]/[method:testMaxAndMinValue()]
replaced return value with "" for com/ancientprogramming/fixedformat4j/annotation/Sign$1::remove → KILLED

58

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

64

1.1
Location : apply
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestIntegerFormatter.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestIntegerFormatter]/[method:testZeroWithSign()]
replaced return value with "" for com/ancientprogramming/fixedformat4j/annotation/Sign$2::apply → KILLED

72

1.1
Location : remove
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignZeroWithPrepend()]
negated conditional → KILLED

76

1.1
Location : remove
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignZeroWithPrepend()]
negated conditional → KILLED

79

1.1
Location : remove
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignZeroWithPrepend()]
replaced return value with "" for com/ancientprogramming/fixedformat4j/annotation/Sign$2::remove → KILLED

90

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

96

1.1
Location : apply
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestShortFormatter.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestShortFormatter]/[method:testAppendSign()]
replaced return value with "" for com/ancientprogramming/fixedformat4j/annotation/Sign$3::apply → KILLED

101

1.1
Location : remove
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignZeroWithAppend()]
Replaced integer subtraction with addition → KILLED

102

1.1
Location : remove
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignZeroWithAppend()]
Replaced integer subtraction with addition → KILLED

104

1.1
Location : remove
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignZeroWithAppend()]
negated conditional → KILLED

107

1.1
Location : remove
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignZeroWithAppend()]
negated conditional → KILLED

110

1.1
Location : remove
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignZeroWithAppend()]
replaced return value with "" for com/ancientprogramming/fixedformat4j/annotation/Sign$3::remove → KILLED

125

1.1
Location : removeSign
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignAppend()]
replaced boolean return with true for com/ancientprogramming/fixedformat4j/annotation/Sign::removeSign → KILLED

2.2
Location : removeSign
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignAppend()]
negated conditional → KILLED

126

1.1
Location : removeSign
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignAppend()]
negated conditional → KILLED

127

1.1
Location : removeSign
Killed by : com.ancientprogramming.fixedformat4j.annotation.TestSign.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.annotation.TestSign]/[method:testSignAppend()]
negated conditional → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.1