Skip to content

Commit

Permalink
Fix rules and getArray column value implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonpereiradev committed Apr 15, 2019
1 parent 7324dc1 commit 97a4f3d
Show file tree
Hide file tree
Showing 44 changed files with 355 additions and 83 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.jonpereiradev</groupId>
<artifactId>jfile-reader</artifactId>
<version>0.5.0</version>
<version>0.6.0</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>API for read and parse a file into an object</description>
Expand Down
215 changes: 188 additions & 27 deletions src/main/java/com/jonpereiradev/jfile/reader/file/JFileColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.regex.Pattern;

public class JFileColumn implements Comparable<JFileColumn> {

private static final Pattern DEFAULT_ARRAY_SEPARATOR = Pattern.compile(",\\s*");
private static final ConcurrentMap<Class<?>, Function<JFileColumn, ?>> MAPPER = new ConcurrentHashMap<>();

static {
Expand Down Expand Up @@ -56,24 +60,6 @@ public JFileColumn(JFileReaderContext context, int position, String content) {
this.content = content;
}

@Override
public int compareTo(JFileColumn o) {
return Integer.compare(position, o.position);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JFileColumn that = (JFileColumn) o;
return position == that.position;
}

@Override
public int hashCode() {
return Objects.hash(position);
}

public int getPosition() {
return position;
}
Expand All @@ -87,6 +73,14 @@ public String getText() {
return content;
}

public String[] getTextArray() {
return getTextArray(DEFAULT_ARRAY_SEPARATOR);
}

public String[] getTextArray(Pattern pattern) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getText).toArray(String[]::new));
}

public Character getCharacter() {
if (StringUtils.isBlank(content) || content.length() > 1) {
return null;
Expand All @@ -95,44 +89,112 @@ public Character getCharacter() {
return content.charAt(0);
}

public Character[] getCharacterArray() {
return getCharacterArray(DEFAULT_ARRAY_SEPARATOR);
}

public Character[] getCharacterArray(Pattern pattern) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getCharacter).toArray(Character[]::new));
}

public Short getShort() {
if (StringUtils.isBlank(content)) {
return null;
}

return Short.valueOf(content);
try {
return Short.valueOf(content);
} catch (NumberFormatException e) {
return null;
}
}

public Short[] getShortArray() {
return getShortArray(DEFAULT_ARRAY_SEPARATOR);
}

public Short[] getShortArray(Pattern pattern) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getShort).toArray(Short[]::new));
}

public Integer getInt() {
if (StringUtils.isBlank(content)) {
return null;
}

return Integer.valueOf(content);
try {
return Integer.valueOf(content);
} catch (NumberFormatException e) {
return null;
}
}

public Integer[] getIntArray() {
return getIntArray(DEFAULT_ARRAY_SEPARATOR);
}

public Integer[] getIntArray(Pattern pattern) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getInt).toArray(Integer[]::new));
}

public Long getLong() {
if (StringUtils.isBlank(content)) {
return null;
}

return Long.valueOf(content);
try {
return Long.valueOf(content);
} catch (NumberFormatException e) {
return null;
}
}

public Long[] getLongArray() {
return getLongArray(DEFAULT_ARRAY_SEPARATOR);
}

public Long[] getLongArray(Pattern pattern) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getLong).toArray(Long[]::new));
}

public Float getFloat() {
if (StringUtils.isBlank(content)) {
return null;
}

return Float.valueOf(content);
try {
return Float.valueOf(content);
} catch (NumberFormatException e) {
return null;
}
}

public Float[] getFloatArray() {
return getFloatArray(DEFAULT_ARRAY_SEPARATOR);
}

public Float[] getFloatArray(Pattern pattern) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getFloat).toArray(Float[]::new));
}

public Double getDouble() {
if (StringUtils.isBlank(content)) {
return null;
}

return Double.valueOf(content);
try {
return Double.valueOf(content);
} catch (NumberFormatException e) {
return null;
}
}

public Double[] getDoubleArray() {
return getDoubleArray(DEFAULT_ARRAY_SEPARATOR);
}

public Double[] getDoubleArray(Pattern pattern) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getDouble).toArray(Double[]::new));
}

public Boolean getBoolean() {
Expand All @@ -151,12 +213,32 @@ public Boolean getBoolean() {
return BooleanUtils.toBooleanObject(booleanString);
}

public Boolean[] getBooleanArray() {
return getBooleanArray(DEFAULT_ARRAY_SEPARATOR);
}

public Boolean[] getBooleanArray(Pattern pattern) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getBoolean).toArray(Boolean[]::new));
}

public BigInteger getBigInteger() {
if (StringUtils.isBlank(content)) {
return null;
}

return new BigInteger(content);
try {
return new BigInteger(content);
} catch (NumberFormatException e) {
return null;
}
}

public BigInteger[] getBigIntegerArray() {
return getBigIntegerArray(DEFAULT_ARRAY_SEPARATOR);
}

public BigInteger[] getBigIntegerArray(Pattern pattern) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(JFileColumn::getBigInteger).toArray(BigInteger[]::new));
}

public BigDecimal getBigDecimal() {
Expand All @@ -171,10 +253,22 @@ public BigDecimal getBigDecimal(DecimalFormat bigDecimalFormatter) {
try {
return (BigDecimal) bigDecimalFormatter.parse(content);
} catch (ParseException e) {
throw new NumberFormatException(e.getMessage());
return null;
}
}

public BigDecimal[] getBigDecimalArray() {
return getBigDecimalArray(DEFAULT_ARRAY_SEPARATOR);
}

public BigDecimal[] getBigDecimalArray(Pattern pattern) {
return getBigDecimalArray(pattern, context.getBigDecimalFormatter());
}

public BigDecimal[] getBigDecimalArray(Pattern pattern, DecimalFormat bigDecimalFormatter) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(o -> o.getBigDecimal(bigDecimalFormatter)).toArray(BigDecimal[]::new));
}

public Date getDate() {
return getDate(context.getDateFormat());
}
Expand All @@ -191,6 +285,18 @@ public Date getDate(DateFormat pattern) {
}
}

public Date[] getDateArray() {
return getDateArray(DEFAULT_ARRAY_SEPARATOR);
}

public Date[] getDateArray(Pattern pattern) {
return getDateArray(pattern, context.getDateFormat());
}

public Date[] getDateArray(Pattern pattern, DateFormat dateFormat) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(o -> o.getDate(dateFormat)).toArray(Date[]::new));
}

public LocalDate getLocalDate() {
return getLocalDate(context.getLocalDateFormatter());
}
Expand All @@ -200,7 +306,23 @@ public LocalDate getLocalDate(DateTimeFormatter dateTimeFormatter) {
return null;
}

return LocalDate.parse(content, dateTimeFormatter);
try {
return LocalDate.parse(content, dateTimeFormatter);
} catch (DateTimeParseException e) {
return null;
}
}

public LocalDate[] getLocalDateArray() {
return getLocalDateArray(DEFAULT_ARRAY_SEPARATOR);
}

public LocalDate[] getLocalDateArray(Pattern pattern) {
return getLocalDateArray(pattern, context.getLocalDateFormatter());
}

public LocalDate[] getLocalDateArray(Pattern pattern, DateTimeFormatter dateTimeFormatter) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(o -> o.getLocalDate(dateTimeFormatter)).toArray(LocalDate[]::new));
}

public LocalDateTime getLocalDateTime() {
Expand All @@ -212,10 +334,49 @@ public LocalDateTime getLocalDateTime(DateTimeFormatter dateTimeFormatter) {
return null;
}

return LocalDateTime.parse(content, dateTimeFormatter);
try {
return LocalDateTime.parse(content, dateTimeFormatter);
} catch (DateTimeParseException e) {
return null;
}
}

public LocalDateTime[] getLocalDateTimeArray() {
return getLocalDateTimeArray(DEFAULT_ARRAY_SEPARATOR);
}

public LocalDateTime[] getLocalDateTimeArray(Pattern pattern) {
return getLocalDateTimeArray(pattern, context.getLocalDateTimeFormatter());
}

public LocalDateTime[] getLocalDateTimeArray(Pattern pattern, DateTimeFormatter dateTimeFormatter) {
return getArrayOf(pattern, array -> Arrays.stream(array).map(o -> o.getLocalDateTime(dateTimeFormatter)).toArray(LocalDateTime[]::new));
}

private <T> T[] getArrayOf(Pattern pattern, Function<JFileColumn[], T[]> function) {
String[] split = pattern.split(content);
return function.apply(Arrays.stream(split).map(s -> new JFileColumn(context, position, s)).toArray(JFileColumn[]::new));
}

public JFileReaderContext getContext() {
return context;
}

@Override
public int compareTo(JFileColumn o) {
return Integer.compare(position, o.position);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JFileColumn that = (JFileColumn) o;
return position == that.position;
}

@Override
public int hashCode() {
return Objects.hash(position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ public BigDecimalTypeRule(int position, DecimalFormat decimalFormat) {

@Override
public boolean isValid(JFileColumn fileColumn) {
try {
return fileColumn.getText().isEmpty() || fileColumn.getBigDecimal(decimalFormat) != null;
} catch (NumberFormatException e) {
return false;
}
return fileColumn.getText().isEmpty() || fileColumn.getBigDecimal(decimalFormat) != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ public BigIntegerTypeRule(int position) {

@Override
public boolean isValid(JFileColumn fileColumn) {
try {
return fileColumn.getText().isEmpty() || fileColumn.getBigInteger() != null;
} catch (NumberFormatException e) {
return false;
}
return fileColumn.getText().isEmpty() || fileColumn.getBigInteger() != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@

import com.jonpereiradev.jfile.reader.file.JFileColumn;

import java.util.Arrays;
import java.util.List;

public class CnpjRule extends AbstractColumnRule {

private static final List<String> INVALID_CNPJS = Arrays.asList(
"00000000000000",
"11111111111111",
"22222222222222",
"33333333333333",
"44444444444444",
"55555555555555",
"66666666666666",
"77777777777777",
"88888888888888",
"99999999999999"
);

public CnpjRule(int position) {
super(position);
}
Expand All @@ -12,7 +28,7 @@ public CnpjRule(int position) {
public boolean isValid(JFileColumn fileColumn) {
String cnpj = fileColumn.getText();

if (cnpj.equals("00000000000000") || cnpj.length() != 14) {
if (cnpj.length() != 14 || INVALID_CNPJS.contains(cnpj)) {
return false;
}

Expand Down
Loading

0 comments on commit 97a4f3d

Please sign in to comment.