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

org.tools4j.spockito.table.Table Maven / Gradle / Ivy

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2017-2021 tools4j.org (Marco Terzer)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.tools4j.spockito.table;

import java.lang.reflect.Type;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static java.util.Objects.requireNonNull;

/**
 * Represents a table with a header row and zero to many data or separator rows; all table elements are strings.
 */
public interface Table extends Iterable {

    int getColumnCount();
    int getRowCount();

    List getColumnNames();
    String getColumnName(int index);
    boolean hasColumn(String columnName);
    int getColumnIndexByName(String columnName);

    TableRow getRow(int rowIndex);
    int getRowIndex(TableRow row);

    String getValue(int rowIndex, int columnIndex);
    String getValue(int rowIndex, String columnName);

    @Override
    Iterator iterator();
    default Stream stream() {
        return StreamSupport.stream(spliterator(), false);
    }

    Table filter(Predicate filter);
    Table sort(Comparator comparator);
    TableJoiner join(TableRow row);

    default  T to(final Class type) {
        return to(type, type);
    }

    default  T to(final Class type, final Type genericType) {
        return to(type, genericType, SpockitoValueConverter.DEFAULT_INSTANCE);
    }

    default  T to(final Class type, final Type genericType, final ValueConverter valueConverter) {
        final TableConverter tableConverter = new SpockitoTableConverter(type, genericType, valueConverter);
        return type.cast(tableConverter.convert(this));
    }

    default List toList() {
        return stream().map(TableRow::toArray).collect(Collectors.toList());
    }

    default  List toList(final Class rowType) {
        return toList(rowType, SpockitoValueConverter.DEFAULT_INSTANCE);
    }

    default  List toList(final Class rowType, final ValueConverter valueConverter) {
        requireNonNull(rowType);
        requireNonNull(valueConverter);
        return stream().map(row -> row.to(rowType, valueConverter)).collect(Collectors.toList());
    }
    default  List toList(final Class rowType, final Type genericType, final ValueConverter valueConverter) {
        requireNonNull(rowType);
        requireNonNull(genericType);
        requireNonNull(valueConverter);
        return stream().map(row -> row.to(rowType, genericType, valueConverter)).collect(Collectors.toList());
    }

    static Table parse(final String[] headerAndRows) {
        return SpockitoTable.parse(headerAndRows);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy