All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.io.flat.Column 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 br.com.objectos.core.testing.Testable;
import br.com.objectos.core.testing.Testables;
import br.com.objectos.io.flat.annotation.IntegerOption;
import br.com.objectos.io.flat.annotation.LocalDatePattern;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class Column implements Testable {

  private final int index;
  private final int length;

  Column(int index, int length) {
    this.index = index;
    this.length = length;
  }

  public CustomColumn custom(int length, CustomFormatter formatter) {
    return CustomColumn.get(nextIndex(), length, formatter);
  }

  public DecimalColumn decimal(int precision, int scale) {
    return DecimalColumn.get(nextIndex(), precision, scale);
  }

  public DecimalColumn decimal(int precision, int scale, DoubleValueCondition condition, String text) {
    return DecimalColumn.get(nextIndex(), precision, scale, condition, text);
  }

  public FixedColumn fixed(String text) {
    return FixedColumn.get(nextIndex(), text);
  }

  public FlatEnumColumn flatEnum(int length, FlatEnumParser parser) {
    return FlatEnumColumn.get(nextIndex(), length, parser);
  }

  public IntegerColumn integer(int length, IntegerOption... options) {
    return IntegerColumn.get(nextIndex(), length, options);
  }

  public IntegerColumn integer(int length, IntValueCondition condition, String text) {
    return IntegerColumn.get(nextIndex(), length, condition, text);
  }

  public LocalDateColumn localDate(LocalDatePattern pattern) {
    return LocalDateColumn.get(nextIndex(), pattern);
  }

  public LocalDateColumn localDate(LocalDatePattern pattern, String whenAbsent) {
    return LocalDateColumn.get(nextIndex(), pattern, whenAbsent);
  }

  @Override
  public boolean isEqual(Column o) {
    return Testables.isEqualHelper()
        .equal(index, o.index)
        .equal(length, o.length)
        .result();
  }

  public boolean keep() {
    return true;
  }

  public final Token parse(Line line) {
    int endIndex = nextIndex();
    String text = line.substring(index, endIndex);
    return parse(line, text);
  }

  public TextColumn text(int length) {
    return TextColumn.get(nextIndex(), length);
  }

  abstract Token parse(Line line, String text);

  private int nextIndex() {
    return index + length;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy