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

nl.topicus.jdbc.shaded.com.google.common.collect.RegularImmutableTable Maven / Gradle / Ivy

There is a newer version: 1.1.6
Show newest version
/*
 * Copyright (C) 2009 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in nl.topicus.jdbc.shaded.com.liance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.nl.topicus.jdbc.shaded.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 nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.collect;

import static nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.Preconditions.checkNotNull;

import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.annotations.GwtCompatible;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.collect.Table.Cell;
import nl.topicus.jdbc.shaded.com.google.j2objc.annotations.WeakOuter;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import nl.topicus.jdbc.shaded.javax.annotation.Nullable;

/**
 * An implementation of {@link ImmutableTable} holding an arbitrary number of
 * cells.
 *
 * @author Gregory Kick
 */
@GwtCompatible
abstract class RegularImmutableTable extends ImmutableTable {
  RegularImmutableTable() {}

  abstract Cell getCell(int iterationIndex);

  @Override
  final ImmutableSet> createCellSet() {
    return isEmpty() ? ImmutableSet.>of() : new CellSet();
  }

  @WeakOuter
  private final class CellSet extends ImmutableSet.Indexed> {
    @Override
    public int size() {
      return RegularImmutableTable.this.size();
    }

    @Override
    Cell get(int index) {
      return getCell(index);
    }

    @Override
    public boolean contains(@Nullable Object object) {
      if (object instanceof Cell) {
        Cell cell = (Cell) object;
        Object value = RegularImmutableTable.this.get(cell.getRowKey(), cell.getColumnKey());
        return value != null && value.equals(cell.getValue());
      }
      return false;
    }

    @Override
    boolean isPartialView() {
      return false;
    }
  }

  abstract V getValue(int iterationIndex);

  @Override
  final ImmutableCollection createValues() {
    return isEmpty() ? ImmutableList.of() : new Values();
  }

  @WeakOuter
  private final class Values extends ImmutableList {
    @Override
    public int size() {
      return RegularImmutableTable.this.size();
    }

    @Override
    public V get(int index) {
      return getValue(index);
    }

    @Override
    boolean isPartialView() {
      return true;
    }
  }

  static  RegularImmutableTable forCells(
      List> cells,
      @Nullable final Comparator rowComparator,
      @Nullable final Comparator columnComparator) {
    checkNotNull(cells);
    if (rowComparator != null || columnComparator != null) {
      /*
       * This sorting logic leads to a cellSet() ordering that may not be expected and that isn't
       * documented in the Javadoc. If a row Comparator is provided, cellSet() iterates across the
       * columns in the first row, the columns in the second row, etc. If a column Comparator is
       * provided but a row Comparator isn't, cellSet() iterates across the rows in the first
       * column, the rows in the second column, etc.
       */
      Comparator> nl.topicus.jdbc.shaded.com.arator =
          new Comparator>() {
            @Override
            public int nl.topicus.jdbc.shaded.com.are(Cell cell1, Cell cell2) {
              int rowCompare =
                  (rowComparator == null)
                      ? 0
                      : rowComparator.nl.topicus.jdbc.shaded.com.are(cell1.getRowKey(), cell2.getRowKey());
              if (rowCompare != 0) {
                return rowCompare;
              }
              return (columnComparator == null)
                  ? 0
                  : columnComparator.nl.topicus.jdbc.shaded.com.are(cell1.getColumnKey(), cell2.getColumnKey());
            }
          };
      Collections.sort(cells, nl.topicus.jdbc.shaded.com.arator);
    }
    return forCellsInternal(cells, rowComparator, columnComparator);
  }

  static  RegularImmutableTable forCells(Iterable> cells) {
    return forCellsInternal(cells, null, null);
  }

  private static final  RegularImmutableTable forCellsInternal(
      Iterable> cells,
      @Nullable Comparator rowComparator,
      @Nullable Comparator columnComparator) {
    Set rowSpaceBuilder = new LinkedHashSet();
    Set columnSpaceBuilder = new LinkedHashSet();
    ImmutableList> cellList = ImmutableList.copyOf(cells);
    for (Cell cell : cells) {
      rowSpaceBuilder.add(cell.getRowKey());
      columnSpaceBuilder.add(cell.getColumnKey());
    }

    ImmutableSet rowSpace =
        (rowComparator == null)
            ? ImmutableSet.copyOf(rowSpaceBuilder)
            : ImmutableSet.copyOf(
                Ordering.from(rowComparator).immutableSortedCopy(rowSpaceBuilder));
    ImmutableSet columnSpace =
        (columnComparator == null)
            ? ImmutableSet.copyOf(columnSpaceBuilder)
            : ImmutableSet.copyOf(
                Ordering.from(columnComparator).immutableSortedCopy(columnSpaceBuilder));

    return forOrderedComponents(cellList, rowSpace, columnSpace);
  }

  /** A factory that chooses the most space-efficient representation of the table. */
  static  RegularImmutableTable forOrderedComponents(
      ImmutableList> cellList,
      ImmutableSet rowSpace,
      ImmutableSet columnSpace) {
    // use a dense table if more than half of the cells have values
    // TODO(gak): tune this condition based on empirical evidence
    return (cellList.size() > (((long) rowSpace.size() * columnSpace.size()) / 2))
        ? new DenseImmutableTable(cellList, rowSpace, columnSpace)
        : new SparseImmutableTable(cellList, rowSpace, columnSpace);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy