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

br.com.objectos.way.io.ToStringXlsWriterHelper Maven / Gradle / Ivy

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 static com.google.common.collect.Lists.newArrayList;

import java.util.List;

import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.base.BaseLocal;
import org.joda.time.format.DateTimeFormat;

/**
 * @author [email protected] (Marcio Endo)
 */
public class ToStringXlsWriterHelper {

  private final List values = newArrayList();

  ToStringXlsWriterHelper() {
  }

  public ToStringXlsWriterHelper addValue(double value) {
    values.add(new DoubleToString(value));
    return this;
  }

  public ToStringXlsWriterHelper addValue(float value) {
    values.add(new DoubleToString(value));
    return this;
  }

  public ToStringXlsWriterHelper addValue(int value) {
    values.add(new DoubleToString(value));
    return this;
  }

  public ToStringXlsWriterHelper addValue(long value) {
    values.add(new DoubleToString(value));
    return this;
  }

  public ToStringXlsWriterHelper addValue(LocalDate localDate) {
    values.add(new JodaTimeToString(localDate));
    return this;
  }

  public ToStringXlsWriterHelper addValue(LocalDateTime localDateTime) {
    values.add(new JodaTimeToString(localDateTime));
    return this;
  }

  public ToStringXlsWriterHelper addValue(String value) {
    values.add(value);
    return this;
  }

  @Override
  public String toString() {
    return values.toString();
  }

  private static class DoubleToString {

    private final double value;

    public DoubleToString(double value) {
      this.value = value;
    }

    @Override
    public String toString() {
      return Double.toString(value);
    }

  }

  private static class JodaTimeToString {

    private static final String PATTERN = "dd-MMM-yyyy";

    private final T value;

    public JodaTimeToString(T value) {
      this.value = value;
    }

    @Override
    public String toString() {
      return DateTimeFormat.forPattern(PATTERN).print(value);
    }

  }

}