br.com.objectos.io.flat.Record Maven / Gradle / Ivy
/*
* 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.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import com.google.common.collect.ImmutableList;
/**
* @author [email protected] (Marcio Endo)
*/
public class Record {
private final List invalidList;
private final List providerList;
private int index;
private Record(List invalidList, List providerList) {
this.invalidList = invalidList;
this.providerList = providerList;
}
static Record of(List tokenList) {
Map> validMap = tokenList.stream()
.collect(Collectors.groupingBy(Token::valid));
List invalidList = nullToEmpty(validMap.get(Boolean.FALSE));
List validList = nullToEmpty(validMap.get(Boolean.TRUE));
List providerList = validList.stream()
.filter(Token::keep)
.collect(Collectors.toList());
return new Record(invalidList, providerList);
}
public T get() {
return providerList.get(index++).get();
}
public boolean valid() {
return invalidList.isEmpty();
}
public double doubleValue() {
return providerList.get(index++).doubleValue();
}
public int intValue() {
return providerList.get(index++).intValue();
}
public RecordParseException parseException() {
List errorList = invalidList.stream()
.map(Token::parseError)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
return new RecordParseException(errorList);
}
private static List nullToEmpty(List list) {
return list != null ? list : ImmutableList.of();
}
}