br.com.objectos.way.io.FixedLineKey Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of way-io Show documentation
Show all versions of way-io Show documentation
CSV, XLS and fixed parsers
The newest version!
/*
* Copyright 2013 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.way.io;
import br.com.objectos.comuns.io.ColumnConversionException;
import br.com.objectos.comuns.io.ColumnKey;
import br.com.objectos.comuns.io.FixedLine;
import br.com.objectos.comuns.io.Line;
import com.google.common.base.Preconditions;
/**
* @author [email protected] (Marcio Endo)
*/
public class FixedLineKey extends RecordKey {
private final int pos0;
private final int pos1;
public FixedLineKey(String id, ColumnKey type, int pos0, int pos1) {
super(id, type);
this.pos0 = pos0;
this.pos1 = pos1;
Preconditions.checkArgument(pos0 >= 0, "pos0 must be >= 0");
Preconditions.checkArgument(pos1 > 0, "pos1 must be > 0");
}
static Builder of() {
return new Builder();
}
@Override
Object apply(Line line) {
Object value = null;
try {
FixedLine fixed = FixedLine.class.cast(line);
value = fixed.column(pos0 - 1, pos1).get(type);
} catch (ColumnConversionException e) {
throw new RecordKeyException(line, this, e);
}
return value;
}
public static class Builder {
private String id;
private int pos0;
private int pos1;
Builder() {
}
public Builder id(String id) {
this.id = id;
return this;
}
public Builder at(int pos0, int pos1) {
this.pos0 = pos0;
this.pos1 = pos1;
return this;
}
public FixedLineKey get(Class clazz) {
ColumnKey type = ColumnKey.of(clazz);
return get(type);
}
public FixedLineKey get(ColumnKey type) {
return new FixedLineKey(id, type, pos0, pos1);
}
}
}