br.com.objectos.io.flat.RecordReader Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2015 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.io.flat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import br.com.objectos.io.flat.annotation.IntegerOption;
import br.com.objectos.io.flat.annotation.LocalDatePattern;
/**
* @author [email protected] (Marcio Endo)
*/
public class RecordReader {
private final Line line;
private final List columnList = new ArrayList<>();
private Optional lastColumn = Optional.empty();
private RecordReader(Line line) {
this.line = line;
}
public static RecordReader get(Line line) {
return new RecordReader(line);
}
public RecordReader custom(int length, CustomFormatter> formatter) {
CustomColumn nextColumn = lastColumn
.map(c -> c.custom(length, formatter))
.orElse(CustomColumn.get(length, formatter));
return addToList(nextColumn);
}
public RecordReader decimal(int precision, int scale) {
DecimalColumn nextColumn = lastColumn
.map(c -> c.decimal(precision, scale))
.orElse(DecimalColumn.get(precision, scale));
return addToList(nextColumn);
}
public RecordReader decimal(int precision, int scale, DoubleValueCondition condition, String text) {
DecimalColumn nextColumn = lastColumn
.map(c -> c.decimal(precision, scale, condition, text))
.orElse(DecimalColumn.get(precision, scale, condition, text));
return addToList(nextColumn);
}
public RecordReader fixed(String text) {
FixedColumn nextColumn = lastColumn
.map(c -> c.fixed(text))
.orElse(FixedColumn.get(text));
return addToList(nextColumn);
}
public RecordReader flatEnum(int length, FlatEnumParser> parser) {
FlatEnumColumn nextColumn = lastColumn
.map(c -> c.flatEnum(length, parser))
.orElse(FlatEnumColumn.get(length, parser));
return addToList(nextColumn);
}
public RecordReader integer(int length, IntegerOption... options) {
IntegerColumn nextColumn = lastColumn
.map(c -> c.integer(length, options))
.orElse(IntegerColumn.get(length, options));
return addToList(nextColumn);
}
public RecordReader integer(int length, IntValueCondition condition, String text) {
IntegerColumn nextColumn = lastColumn
.map(c -> c.integer(length, condition, text))
.orElse(IntegerColumn.get(length, condition, text));
return addToList(nextColumn);
}
public RecordReader localDate(LocalDatePattern pattern) {
LocalDateColumn nextColumn = lastColumn
.map(c -> c.localDate(pattern))
.orElse(LocalDateColumn.get(pattern));
return addToList(nextColumn);
}
public RecordReader localDate(LocalDatePattern pattern, String whenAbsent) {
LocalDateColumn nextColumn = lastColumn
.map(c -> c.localDate(pattern, whenAbsent))
.orElse(LocalDateColumn.get(pattern, whenAbsent));
return addToList(nextColumn);
}
public Record read() {
List tokenList = columnList.stream()
.map(c -> c.parse(line))
.collect(Collectors.toList());
return Record.of(tokenList);
}
public RecordReader text(int length) {
TextColumn nextColumn = lastColumn
.map(c -> c.text(length))
.orElse(TextColumn.get(length));
return addToList(nextColumn);
}
private RecordReader addToList(Column nextColumn) {
columnList.add(nextColumn);
lastColumn = Optional.of(nextColumn);
return this;
}
}