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

com.bbn.bue.common.collections.TableUtils Maven / Gradle / Ivy

There is a newer version: 4.1.2
Show newest version
package com.bbn.bue.common.collections;

import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Table;

import java.util.Map;

/**
 * Created by jdeyoung on 5/27/15.
 */
@Beta
public class TableUtils {


  public static  ImmutableTable tableFromIndexFunctions(final Iterable values,
      final Function rowIndexFunction, final Function columnIndexFunction) {
    final ImmutableTable.Builder builder = ImmutableTable.builder();
    // this must be done in an imperative style since the column or row (or both) index functions
    // are not necessarily unique
    for (V v : values) {
      builder.put(rowIndexFunction.apply(v), columnIndexFunction.apply(v), v);
    }
    return builder.build();
  }

  public static  ImmutableTable columnTransformerByKeyOnly(
      final Table table,
      final Function columnTransformer) {
    return columnTransformerByCell(table, new Function, C2>() {
      @Override
      public C2 apply(final Table.Cell input) {
        return columnTransformer.apply(input.getColumnKey());
      }
    });
  }

  /**
   * columnTransformer has access to Key, Value information in each Table.Cell
   * @param table
   * @param columnTransformer
   * @param 
   * @param 
   * @param 
   * @param 
   * @return
   */
  public static  ImmutableTable columnTransformerByCell(
      final Table table,
      final Function, C2> columnTransformer) {
    final ImmutableTable.Builder newTable = ImmutableTable.builder();
    for(Table.Cell cell : table.cellSet()) {
      C2 col = columnTransformer.apply(cell);
      newTable.put(cell.getRowKey(), col, cell.getValue());
    }
    return newTable.build();
  }

  public static  ImmutableTable rowTransformerByKeyOnly(
      final Table table,
      final Function rowTransformer) {
    return rowTransformerByCell(table, new Function, R2>() {
      @Override
      public R2 apply(final Table.Cell input) {
        return rowTransformer.apply(input.getRowKey());
      }
    });
  }

  public static  ImmutableTable rowTransformerByCell(
      final Table table, final Function, R2> rowTransformer) {
    final ImmutableTable.Builder newTable = ImmutableTable.builder();
    for(Table.Cell cell : table.cellSet()) {
      R2 row = rowTransformer.apply(cell);
      newTable.put(row, cell.getColumnKey(), cell.getValue());
    }
    return newTable.build();
  }

  public static  void addColumnToBuilder(ImmutableTable.Builder builder, R row, Map column) {
    for(Map.Entry e: column.entrySet()) {
      builder.put(row, e.getKey(), e.getValue());
    }
  }

  /**
   * Creates a new {@link ImmutableTable} from the provided {@link com.google.common.collect.Table.Cell}s.
   * The iteration order of the resulting table will respect the iteration order of the input cells.
   * Null keys and values are forbidden.
   */
  public static  ImmutableTable copyOf(Iterable> cells) {
    final ImmutableTable.Builder ret = ImmutableTable.builder();

    for (final Table.Cell cell : cells) {
      ret.put(cell.getRowKey(), cell.getColumnKey(), cell.getValue());
    }

    return ret.build();
  }

  /**
   * Guava {@link Function} to transform a cell to its row key.
   */
  public static  Function, R> toRowKeyFunction() {
    return new Function, R>() {
      @Override
      public R apply(final Table.Cell input) {
        return input.getRowKey();
      }
    };
  }

  /**
   * Guava {@link Function} to transform a cell to its column key.
   */
  public static  Function, C> toColumnKeyFunction() {
    return new Function, C>() {
      @Override
      public C apply(final Table.Cell input) {
        return input.getColumnKey();
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy