
br.com.objectos.way.io.LineKey Maven / Gradle / Ivy
/*
* 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.Line;
/**
* @author [email protected] (Marcio Endo)
*/
public class LineKey extends RecordKey {
private final int index;
public LineKey(String id, ColumnKey type, int index) {
super(id, type);
this.index = index;
Preconditions.checkArgument(index >= 0, "index must be >= 0");
}
static Builder of() {
return new Builder();
}
@Override
Object apply(Line line) {
Object value = null;
try {
value = line.column(index).orNull(type);
} catch (ColumnConversionException e) {
throw new RecordKeyException(line, this, e);
}
return value;
}
public static class Builder {
private String id;
private int index;
Builder() {
}
public Builder id(String id, int index) {
this.id = id;
this.index = index;
return this;
}
public LineKey get(Class clazz) {
ColumnKey type = ColumnKey.of(clazz);
return get(type);
}
public LineKey get(ColumnKey type) {
return new LineKey(id, type, index);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy