alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (SparseImmutableTable.java)

This example Java source code file (SparseImmutableTable.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

cell, immutablelist, immutablemap, immutableset, integer, linkedhashmap, map, object, override, sparseimmutabletable, util

The SparseImmutableTable.java Java example source code

/*
 * Copyright (C) 2009 The Guava Authors
 *
 * 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 com.google.common.collect;

import com.google.common.annotations.GwtCompatible;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.concurrent.Immutable;

/**
 * A {@code RegularImmutableTable} optimized for sparse data.
 */
@GwtCompatible
@Immutable
final class SparseImmutableTable<R, C, V> extends RegularImmutableTable {
  static final ImmutableTable<Object, Object, Object> EMPTY =
      new SparseImmutableTable<Object, Object, Object>(
          ImmutableList.<Cellof(), ImmutableSet.of(), ImmutableSet.of());

  private final ImmutableMap<R, Map rowMap;
  private final ImmutableMap<C, Map columnMap;
  private final int[] iterationOrderRow;
  private final int[] iterationOrderColumn;

  SparseImmutableTable(
      ImmutableList<Cell cellList,
      ImmutableSet<R> rowSpace,
      ImmutableSet<C> columnSpace) {
    Map<R, Integer> rowIndex = Maps.indexMap(rowSpace);
    Map<R, Map rows = Maps.newLinkedHashMap();
    for (R row : rowSpace) {
      rows.put(row, new LinkedHashMap<C, V>());
    }
    Map<C, Map columns = Maps.newLinkedHashMap();
    for (C col : columnSpace) {
      columns.put(col, new LinkedHashMap<R, V>());
    }
    int[] iterationOrderRow = new int[cellList.size()];
    int[] iterationOrderColumn = new int[cellList.size()];
    for (int i = 0; i < cellList.size(); i++) {
      Cell<R, C, V> cell = cellList.get(i);
      R rowKey = cell.getRowKey();
      C columnKey = cell.getColumnKey();
      V value = cell.getValue();

      iterationOrderRow[i] = rowIndex.get(rowKey);
      Map<C, V> thisRow = rows.get(rowKey);
      iterationOrderColumn[i] = thisRow.size();
      V oldValue = thisRow.put(columnKey, value);
      if (oldValue != null) {
        throw new IllegalArgumentException(
            "Duplicate value for row="
                + rowKey
                + ", column="
                + columnKey
                + ": "
                + value
                + ", "
                + oldValue);
      }
      columns.get(columnKey).put(rowKey, value);
    }
    this.iterationOrderRow = iterationOrderRow;
    this.iterationOrderColumn = iterationOrderColumn;
    ImmutableMap.Builder<R, Map rowBuilder =
        new ImmutableMap.Builder<R, Map(rows.size());
    for (Map.Entry<R, Map row : rows.entrySet()) {
      rowBuilder.put(row.getKey(), ImmutableMap.copyOf(row.getValue()));
    }
    this.rowMap = rowBuilder.build();

    ImmutableMap.Builder<C, Map columnBuilder =
        new ImmutableMap.Builder<C, Map(columns.size());
    for (Map.Entry<C, Map col : columns.entrySet()) {
      columnBuilder.put(col.getKey(), ImmutableMap.copyOf(col.getValue()));
    }
    this.columnMap = columnBuilder.build();
  }

  @Override
  public ImmutableMap<C, Map columnMap() {
    return columnMap;
  }

  @Override
  public ImmutableMap<R, Map rowMap() {
    return rowMap;
  }

  @Override
  public int size() {
    return iterationOrderRow.length;
  }

  @Override
  Cell<R, C, V> getCell(int index) {
    int rowIndex = iterationOrderRow[index];
    Map.Entry<R, Map rowEntry = rowMap.entrySet().asList().get(rowIndex);
    ImmutableMap<C, V> row = (ImmutableMap) rowEntry.getValue();
    int columnIndex = iterationOrderColumn[index];
    Map.Entry<C, V> colEntry = row.entrySet().asList().get(columnIndex);
    return cellOf(rowEntry.getKey(), colEntry.getKey(), colEntry.getValue());
  }

  @Override
  V getValue(int index) {
    int rowIndex = iterationOrderRow[index];
    ImmutableMap<C, V> row = (ImmutableMap) rowMap.values().asList().get(rowIndex);
    int columnIndex = iterationOrderColumn[index];
    return row.values().asList().get(columnIndex);
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java SparseImmutableTable.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.