Package br.com.objectos.way.io

Source Code of br.com.objectos.way.io.FixedLineKey$Builder

/*
* 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 marcio.endo@objectos.com.br (Marcio Endo)
*/
public class FixedLineKey<T> extends RecordKey<T> {

  private final int pos0;

  private final int pos1;

  public FixedLineKey(String id, ColumnKey<T> 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 <V> FixedLineKey<V> get(Class<V> clazz) {
      ColumnKey<V> type = ColumnKey.of(clazz);
      return get(type);
    }

    public <V> FixedLineKey<V> get(ColumnKey<V> type) {
      return new FixedLineKey<V>(id, type, pos0, pos1);
    }

  }

}
TOP

Related Classes of br.com.objectos.way.io.FixedLineKey$Builder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.