Skip to content

Commit

Permalink
add javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
g0ddest committed Oct 13, 2024
1 parent 5d79ce5 commit 33c9b59
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ repositories {
}

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.3'
testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.2'
}

test {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/name/velikodniy/vitaliy/fixedlength/Align.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.Arrays;

/**
* Alignment of value in a field.
*/
public enum Align {
RIGHT {
public String make(String data, int length, char paddingChar) {
Expand Down Expand Up @@ -105,7 +108,7 @@ private static String leftPad(final String str, final int size, String padStr) {
if (str == null) {
return null;
}
if (padStr == null || padStr.length() == 0) {
if (padStr == null || padStr.isEmpty()) {
padStr = " ";
}
final int padLen = padStr.length();
Expand Down Expand Up @@ -150,7 +153,7 @@ public static String rightPad(final String str, final int size, String padStr) {
if (str == null) {
return null;
}
if (padStr == null || padStr.length() == 0) {
if (padStr == null || padStr.isEmpty()) {
padStr = " ";
}
final int padLen = padStr.length();
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@

import static java.util.Objects.requireNonNull;

/**
* Class for fixed line processing and registering classes to process.
* @param <T>
*/
public class FixedLength<T> {

private static final Logger LOGGER = Logger.getLogger(FixedLength.class.getName());
Expand Down Expand Up @@ -89,6 +93,12 @@ List<Field> getAllFields(final Class<?> clazz) {
return result;
}

/**
* Register here type that will be processed in serialization, or deserialization process.
* Could be called more than once.
* @param lineClass class for entity to be registered
* @return instance of FixedLength
*/
public FixedLength<T> registerLineType(final Class<? extends T> lineClass) {
lineTypes.add(classToLineDesc(lineClass));
return this;
Expand All @@ -108,21 +118,39 @@ public FixedLength<T> registerFormatter(
return this;
}

/**
* In case of unknown line, the one will be skipped with no exception thrown
* @return instance of FixedLength
*/
public FixedLength<T> stopSkipUnknownLines() {
skipUnknownLines = false;
return this;
}

/**
* In case of error field in parsing the line, the one will be skipped with no exception thrown
* @return instance of FixedLength
*/
public FixedLength<T> skipErroneousFields() {
skipErroneousFields = true;
return this;
}

/**
* In case of error line while parsing, the one will be skipped with no exception thrown
* @return instance of FixedLength
*/
public FixedLength<T> skipErroneousLines() {
skipErroneousLines = true;
return this;
}

/**
* In case you have a mixed fixed length file with different types in it,
* you could register more than one line type in array instead of calling registerLineType multiple times.
* @param lineClasses class for entity to be registered
* @return instance of FixedLength
*/
public FixedLength<T> registerLineTypes(final List<Class<T>> lineClasses) {
lineTypes.addAll(
lineClasses.stream()
Expand All @@ -132,21 +160,43 @@ public FixedLength<T> registerLineTypes(final List<Class<T>> lineClasses) {
return this;
}

/**
* In case you have a mixed fixed length file with different types in it,
* you could register more than one line type in array instead of calling registerLineType multiple times.
* @param lineClasses class for entity to be registered
* @return instance of FixedLength
*/
public FixedLength<T> registerLineTypes(final Class<T>[] lineClasses) {
registerLineTypes(Arrays.asList(lineClasses));
return this;
}

/**
* Specifies charset of a file, in case of no provided Charset.defaultCharset() will be used
* @param charset Charset of current file
* @return instance of FixedLength
*/
public FixedLength<T> usingCharset(Charset charset) {
this.charset = requireNonNull(charset, "Charset can't be null");
return this;
}

/**
* Delimiter between fixed line entity records could be specified as a regexp pattern.
* By default, it is linefeed LF \n
* @param pattern regexp pattern how to break lines
* @return instance of FixedLength
*/
public FixedLength<T> usingLineDelimiter(Pattern pattern) {
this.delimiter = requireNonNull(pattern, "Line delimiter pattern can't be null");
return this;
}

/**
* Delimiter between fixed line entity records. By default, it is linefeed LF \n (\u000a)
* @param delimiterString string that points the end of a line
* @return instance of FixedLength
*/
public FixedLength<T> usingLineDelimiter(String delimiterString) {
this.delimiterString = requireNonNull(
delimiterString,
Expand Down Expand Up @@ -305,14 +355,32 @@ private List<T> lineToObjects(FixedFormatRecord fixedFormatRecord) {
}
}

/**
* Parses a fixed length file into a List
* @param stream InputStream of a fixed length file
* @return List of parsed objects
* @throws FixedLengthException in case of parsing errors
*/
public List<T> parse(InputStream stream) throws FixedLengthException {
return this.parseAsStream(stream).collect(Collectors.toList());
}

/**
* Parses a fixed length file into a List
* @param reader Reader of a fixed length file
* @return List of parsed objects
* @throws FixedLengthException in case of parsing errors
*/
public List<T> parse(Reader reader) throws FixedLengthException {
return parseAsStream(reader).collect(Collectors.toList());
}

/**
* Parses a fixed length file into a stream
* @param inputStream InputStream of a fixed length file
* @return Stream of parsed objects
* @throws FixedLengthException in case of parsing errors
*/
public Stream<T> parseAsStream(InputStream inputStream)
throws FixedLengthException {
Stream<String> lines = StreamSupport.stream(
Expand All @@ -325,6 +393,12 @@ public Stream<T> parseAsStream(InputStream inputStream)
return parseAsStream(lines);
}

/**
* Parses a fixed length file into a stream
* @param reader Reader of a fixed length file
* @return Stream of parsed objects
* @throws FixedLengthException in case of parsing errors
*/
public Stream<T> parseAsStream(Reader reader) throws FixedLengthException {
return parseAsStream(new BufferedReader(reader).lines());
}
Expand All @@ -341,6 +415,11 @@ private Stream<T> parseAsStream(Stream<String> lines) throws FixedLengthExceptio
.flatMap(fixedFormatRecord -> lineToObjects(fixedFormatRecord).stream());
}

/**
* Builds a fixed length String
* @param lines lines to be serialized
* @return String of a fixed length format
*/
public String format(List<T> lines) {

final StringBuilder builder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
@Retention(RUNTIME)
@Target(ElementType.FIELD)
public @interface FixedField {
/**
* Offset of the field
* @return offset of the field
*/
int offset();

/**
Expand All @@ -21,14 +25,14 @@
int length();

/**
* Align of fixed format field
* Align of fixed format field, default is RIGHT
*
* @return align of fixed format field
*/
Align align() default Align.RIGHT;

/**
* Padding chars that will be trimmed. It depends on align.
* Padding chars that will be trimmed. It depends on align. By default, it is whitespace.
*
* @return padding chars that will be trimmed
*/
Expand Down

0 comments on commit 33c9b59

Please sign in to comment.