br.com.objectos.way.io.IsXlsWritableLineBuilder.java Source code

Java tutorial

Introduction

Here is the source code for br.com.objectos.way.io.IsXlsWritableLineBuilder.java

Source

/*
 * Copyright 2014 Objectos, Fbrica 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 com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

/**
 * @author Marcio Endo
 */
public class IsXlsWritableLineBuilder {

    private final List<Row> rows = newArrayList();

    IsXlsWritableLineBuilder() {
    }

    public StringRow addRow() {
        StringRow row = new StringRow();
        rows.add(row);
        return row;
    }

    public <T extends IsXlsWritable> List<String> transform(List<T> writables) {
        List<Row> moreRows;
        moreRows = Lists.transform(writables, new IsXlsWritableToRow());

        rows.addAll(moreRows);

        List<String> strings;
        strings = Lists.transform(rows, Functions.toStringFunction());

        return ImmutableList.copyOf(strings);
    }

    private class IsXlsWritableToRow implements Function<IsXlsWritable, Row> {

        private int index = rows.size() + 1;

        @Override
        public Row apply(IsXlsWritable input) {
            return new IsXlsWritableRow(index++, input);
        }

    }

    public class StringRow extends Row {

        private final List<String> cols = newArrayList();

        public StringRow() {
            super(rows.size() + 1);
        }

        public StringRow write(String col) {
            cols.add(col);
            return this;
        }

        public IsXlsWritableLineBuilder end() {
            return IsXlsWritableLineBuilder.this;
        }

        @Override
        String getText() {
            return cols.toString();
        }

    }

    private class IsXlsWritableRow extends Row {

        private final IsXlsWritable writable;

        public IsXlsWritableRow(int index, IsXlsWritable writable) {
            super(index);
            this.writable = writable;
        }

        @Override
        String getText() {
            return writable.toStringXlsWriter();
        }

    }

    abstract class Row {

        private final int index;

        public Row(int index) {
            this.index = index;
        }

        @Override
        public String toString() {
            return String.format("%05d: %s", index, getText());
        }

        abstract String getText();

    }

}