Skip to content

Commit ccaf546

Browse files
authored
Merge pull request #26 from joutvhu/development
Fix Alignment
2 parents e545d78 + ab1bfe3 commit ccaf546

File tree

5 files changed

+102
-9
lines changed

5 files changed

+102
-9
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
group = 'com.github.joutvhu'
10-
version '1.1.2'
10+
version '1.1.3'
1111
sourceCompatibility = 1.8
1212
targetCompatibility = 1.8
1313

@@ -46,6 +46,7 @@ dependencies {
4646

4747
testImplementation 'junit:junit:4.13'
4848
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
49+
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.6.2'
4950
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
5051
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.6.2'
5152

src/main/java/com/joutvhu/fixedwidth/parser/support/FixedStringAssembler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ public StringAssembler pad(FixedTypeInfo info, Alignment defaultAlignment) {
150150

151151
switch (alignment) {
152152
case LEFT:
153-
value = CommonUtil.leftPadValue(value, length, padding);
153+
value = CommonUtil.rightPadValue(value, length, padding);
154154
break;
155155
case RIGHT:
156-
value = CommonUtil.rightPadValue(value, length, padding);
156+
value = CommonUtil.leftPadValue(value, length, padding);
157157
break;
158158
case CENTER:
159159
value = CommonUtil.centerPadValue(value, length, padding);
@@ -191,10 +191,10 @@ public StringAssembler trim(FixedTypeInfo info, Alignment defaultAlignment) {
191191

192192
switch (alignment) {
193193
case LEFT:
194-
value = CommonUtil.trimLeftBy(value, padding);
194+
value = CommonUtil.trimRightBy(value, padding);
195195
break;
196196
case RIGHT:
197-
value = CommonUtil.trimRightBy(value, padding);
197+
value = CommonUtil.trimLeftBy(value, padding);
198198
break;
199199
case CENTER:
200200
value = CommonUtil.trimBy(value, padding);

src/main/java/com/joutvhu/fixedwidth/parser/support/FixedTypeInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ public boolean getDefaultKeepPadding() {
159159
public Alignment getDefaultAlignment() {
160160
if (alignment == null || Alignment.AUTO.equals(alignment)) {
161161
if (TypeConstants.INTEGER_NUMBER_TYPES.contains(type) || TypeConstants.DECIMAL_NUMBER_TYPES.contains(type))
162-
return Alignment.LEFT;
163-
return Alignment.RIGHT;
162+
return Alignment.RIGHT;
163+
return Alignment.LEFT;
164164
}
165165
return alignment;
166166
}

src/main/java/com/joutvhu/fixedwidth/parser/util/CommonUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ public String centerPadValue(String value, int size, char pad) {
141141
else if (len < size) {
142142
int padSize = size - len;
143143
int halfSize = padSize / 2;
144-
return StringUtils.repeat(pad, (padSize & 1) == 0 ? halfSize + 1 : halfSize) +
145-
value + StringUtils.repeat(pad, halfSize);
144+
return StringUtils.repeat(pad, halfSize) + value +
145+
StringUtils.repeat(pad, (padSize & 1) == 0 ? halfSize + 1 : halfSize);
146146
} else return StringUtils.substring(value, 0, size);
147147
}
148148

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.joutvhu.fixedwidth.parser;
2+
3+
import com.joutvhu.fixedwidth.parser.annotation.FixedField;
4+
import com.joutvhu.fixedwidth.parser.annotation.FixedObject;
5+
import com.joutvhu.fixedwidth.parser.domain.Alignment;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Data;
8+
import org.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.MethodSource;
12+
13+
import java.util.Arrays;
14+
import java.util.List;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
18+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
19+
class AlignmentTests {
20+
21+
private FixedParser fixedParser;
22+
23+
@BeforeAll
24+
public void beforeTest() {
25+
this.fixedParser = FixedParser.parser();
26+
}
27+
28+
@ParameterizedTest
29+
@MethodSource("samples")
30+
void validateFormattedFieldWithPadding(Sample sample) {
31+
String output = fixedParser.export(sample.person);
32+
33+
assertEquals(sample.expected, output);
34+
}
35+
36+
@SuppressWarnings("unused")
37+
static List<Sample> samples() {
38+
return Arrays.asList(
39+
new Sample(new AutoPerson("Bob"), "Bob "),
40+
41+
// FIXME "left" alignment means padding to the right to me
42+
new Sample(new LeftPerson("Bob"), "Bob "),
43+
44+
// FIXME "right" alignment means padding to the left for me
45+
new Sample(new RightPerson("Bob"), " Bob"),
46+
47+
new Sample(new CenterPerson("Bob"), " Bob "),
48+
49+
// FIXME fixed length = 10, "Emma".length = 4, this means 3 chars to the left, and 3 chars to the right
50+
new Sample(new CenterPerson("Emma"), " Emma ")
51+
);
52+
}
53+
54+
@Data
55+
@AllArgsConstructor
56+
static class Sample {
57+
private Object person;
58+
private String expected;
59+
}
60+
61+
@FixedObject
62+
@Data
63+
@AllArgsConstructor
64+
public static class AutoPerson {
65+
@FixedField(length = 10, alignment = Alignment.AUTO)
66+
private String name;
67+
}
68+
69+
@FixedObject
70+
@Data
71+
@AllArgsConstructor
72+
public static class LeftPerson {
73+
@FixedField(length = 10, alignment = Alignment.LEFT)
74+
private String name;
75+
}
76+
77+
@FixedObject
78+
@Data
79+
@AllArgsConstructor
80+
public static class RightPerson {
81+
@FixedField(length = 10, alignment = Alignment.RIGHT)
82+
private String name;
83+
}
84+
85+
@FixedObject
86+
@Data
87+
@AllArgsConstructor
88+
public static class CenterPerson {
89+
@FixedField(length = 10, alignment = Alignment.CENTER)
90+
private String name;
91+
}
92+
}

0 commit comments

Comments
 (0)