Skip to content

Instantly share code, notes, and snippets.

@obrodinho
Created June 3, 2016 18:40
Show Gist options
  • Save obrodinho/3e8a082865d8b8354dccbeb887eeec8f to your computer and use it in GitHub Desktop.
Save obrodinho/3e8a082865d8b8354dccbeb887eeec8f to your computer and use it in GitHub Desktop.
Guava's Table traversal comparison
package br.com.jusbrasil.diarios.parser.producao;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Table;
import org.apache.commons.lang3.StringUtils;
public class GuavaMapOrTable {
/**
* Normally I would have three or more maps.
*/
private static final String BOOK_LABEL_1 = "ONE";
private static final String BOOK_LABEL_2 = "TWO";
private static final String BOOK_LABEL_3 = "THREE";
private static final String BOOK_LABEL_4 = "FOUR";
private static final String BOOK_LABEL_5 = "FIVE";
private static final String BOOK_LABLE_6 = "SIX";
private static final String BOOK_LABEL_10 = "TEN";
private static final String BOOK_LABEL_1_SUP = "ONE_SUPPLEMENT";
private static final String BOOK_LABEL_2_SUP = "TWO_SUPPLEMENT";
private static final String BOOK_LABEL_3_SUP = "THREE_SUPPLEMENT";
private static final String BOOK_LABEL_4_SUP = "FOUR_SUPPLEMENT";
private static final String BOOK_LABEL_5_SUP = "FIVE_SUPPLEMENT";
private static final String BOOK_LABEL_6_SUP = "SIX_SUPPLEMENT";
private static final String BOOK_LABEL_10_SUP = "TEN_SUPPLEMENT";
private static final Map<String, String> BOOK_LABELS = ImmutableMap.<String, String>builder()
.put(BOOK_LABEL_1, "One")
.put(BOOK_LABEL_2, "Two")
.put(BOOK_LABEL_3, "Three")
.put(BOOK_LABEL_4, "Four")
.put(BOOK_LABEL_5, "Five")
.put(BOOK_LABLE_6, "Six")
.put(BOOK_LABEL_10, "Ten")
.put(BOOK_LABEL_1_SUP, "One - Supplement")
.put(BOOK_LABEL_2_SUP, "Two - Supplement")
.put(BOOK_LABEL_3_SUP, "Three - Supplement")
.put(BOOK_LABEL_4_SUP, "Four - Supplement")
.put(BOOK_LABEL_5_SUP, "Five - Supplement")
.put(BOOK_LABEL_6_SUP, "Six - Supplement")
.put(BOOK_LABEL_10_SUP, "Ten - Supplement")
.build();
private static final Map<String, Integer> BOOK_LABEL_SUPPLEMENT_FLAG = ImmutableMap.<String, Integer>builder()
.put(BOOK_LABEL_1, 0)
.put(BOOK_LABEL_2, 0)
.put(BOOK_LABEL_3, 0)
.put(BOOK_LABEL_4, 0)
.put(BOOK_LABEL_5, 0)
.put(BOOK_LABLE_6, 0)
.put(BOOK_LABEL_10, 0)
.put(BOOK_LABEL_1_SUP, 1)
.put(BOOK_LABEL_2_SUP, 1)
.put(BOOK_LABEL_3_SUP, 1)
.put(BOOK_LABEL_4_SUP, 1)
.put(BOOK_LABEL_5_SUP, 1)
.put(BOOK_LABEL_6_SUP, 1)
.put(BOOK_LABEL_10_SUP, 1)
.build();
private static final Map<String, Integer> BOOK_LABEL_EDITION_TYPE_ID = ImmutableMap.<String, Integer>builder()
.put(BOOK_LABEL_1, 1)
.put(BOOK_LABEL_2, 2)
.put(BOOK_LABEL_3, 3)
.put(BOOK_LABEL_4, 4)
.put(BOOK_LABEL_5, 5)
.put(BOOK_LABLE_6, 6)
.put(BOOK_LABEL_10, 10)
.put(BOOK_LABEL_1_SUP, 1)
.put(BOOK_LABEL_2_SUP, 2)
.put(BOOK_LABEL_3_SUP, 3)
.put(BOOK_LABEL_4_SUP, 4)
.put(BOOK_LABEL_5_SUP, 5)
.put(BOOK_LABEL_6_SUP, 6)
.put(BOOK_LABEL_10_SUP, 10)
.build();
private static final String EDITION_ID_COLUMN_KEY = "EditionId";
private static final String SUPLLEMENT_FLAG_COLUMN_KEY = "SupplementFlag";
/**
* Or could we get a Table? to remove at least two of these maps above.
*/
private static final Table<String, String, Integer> BOOK_LABEL_EDITION_IDS_AND_FLAGS =
ImmutableTable.<String, String, Integer>builder()
.put(BOOK_LABEL_1, EDITION_ID_COLUMN_KEY, 1)
.put(BOOK_LABEL_2, EDITION_ID_COLUMN_KEY, 2)
.put(BOOK_LABEL_3, EDITION_ID_COLUMN_KEY, 3)
.put(BOOK_LABEL_4, EDITION_ID_COLUMN_KEY, 4)
.put(BOOK_LABEL_5, EDITION_ID_COLUMN_KEY, 5)
.put(BOOK_LABLE_6, EDITION_ID_COLUMN_KEY, 6)
.put(BOOK_LABEL_10, EDITION_ID_COLUMN_KEY, 10)
.put(BOOK_LABEL_1_SUP, EDITION_ID_COLUMN_KEY, 1)
.put(BOOK_LABEL_2_SUP, EDITION_ID_COLUMN_KEY, 2)
.put(BOOK_LABEL_3_SUP, EDITION_ID_COLUMN_KEY, 3)
.put(BOOK_LABEL_4_SUP, EDITION_ID_COLUMN_KEY, 4)
.put(BOOK_LABEL_5_SUP, EDITION_ID_COLUMN_KEY, 5)
.put(BOOK_LABEL_6_SUP, EDITION_ID_COLUMN_KEY, 6)
.put(BOOK_LABEL_10_SUP, EDITION_ID_COLUMN_KEY, 10)
.put(BOOK_LABEL_1, SUPLLEMENT_FLAG_COLUMN_KEY, 0)
.put(BOOK_LABEL_2, SUPLLEMENT_FLAG_COLUMN_KEY, 0)
.put(BOOK_LABEL_3, SUPLLEMENT_FLAG_COLUMN_KEY, 0)
.put(BOOK_LABEL_4, SUPLLEMENT_FLAG_COLUMN_KEY, 0)
.put(BOOK_LABEL_5, SUPLLEMENT_FLAG_COLUMN_KEY, 0)
.put(BOOK_LABLE_6, SUPLLEMENT_FLAG_COLUMN_KEY, 0)
.put(BOOK_LABEL_10, SUPLLEMENT_FLAG_COLUMN_KEY, 0)
.put(BOOK_LABEL_1_SUP, SUPLLEMENT_FLAG_COLUMN_KEY, 1)
.put(BOOK_LABEL_2_SUP, SUPLLEMENT_FLAG_COLUMN_KEY, 1)
.put(BOOK_LABEL_3_SUP, SUPLLEMENT_FLAG_COLUMN_KEY, 1)
.put(BOOK_LABEL_4_SUP, SUPLLEMENT_FLAG_COLUMN_KEY, 1)
.put(BOOK_LABEL_5_SUP, SUPLLEMENT_FLAG_COLUMN_KEY, 1)
.put(BOOK_LABEL_6_SUP, SUPLLEMENT_FLAG_COLUMN_KEY, 1)
.put(BOOK_LABEL_10_SUP, SUPLLEMENT_FLAG_COLUMN_KEY, 1)
.build();
/**
* What about an inverted table?
*/
private static final Table<Integer, Integer, String> EDITION_ID_FLAG_BOOK_LABELS =
ImmutableTable.<Integer, Integer, String>builder()
.put(0, 1, BOOK_LABEL_1)
.put(0, 2, BOOK_LABEL_2)
.put(0, 3, BOOK_LABEL_3)
.put(0, 4, BOOK_LABEL_4)
.put(0, 5, BOOK_LABEL_5)
.put(0, 6, BOOK_LABLE_6)
.put(0, 10, BOOK_LABEL_10)
.put(1, 1, BOOK_LABEL_1_SUP)
.put(1, 2, BOOK_LABEL_2_SUP)
.put(1, 3, BOOK_LABEL_3_SUP)
.put(1, 4, BOOK_LABEL_4_SUP)
.put(1, 5, BOOK_LABEL_5_SUP)
.put(1, 6, BOOK_LABEL_6_SUP)
.put(1, 10, BOOK_LABEL_10_SUP)
.build();
public static void main(String[] args) {
/**
* For both situations we need to retrieve one of the labels from other descriptive info.
* If these info is only descriptive to you, think on creating a reverse map or reverse table, to ease the traversal.
*/
final int regularEdition = 0;
final int supplementEdition = 1;
final int searchedEditionId = 6;
final Map<String, Map<String, Integer>> columns = BOOK_LABEL_EDITION_IDS_AND_FLAGS.columnMap();
final Map<String, Map<String, Integer>> rows = BOOK_LABEL_EDITION_IDS_AND_FLAGS.rowMap();
System.out.println("Iterating... Getting SIX book label for EditionId=6 and SupplementEditionFlag=0.");
for (final Map.Entry<String, Map<String, Integer>> row : rows.entrySet()) {
final Map<String, Integer> rowValue = row.getValue();
if (rowValue.containsKey(EDITION_ID_COLUMN_KEY) && rowValue.containsValue(searchedEditionId) &&
rowValue.containsKey(SUPLLEMENT_FLAG_COLUMN_KEY) && rowValue.containsValue(regularEdition)) {
System.out.println(row.getKey());
}
}
System.out.println("Using inverted table. EditionId=6 and SupplementEditionFlag=1.");
System.out.println(EDITION_ID_FLAG_BOOK_LABELS);
// It could be something like:
// final String bookLabel = EDITION_ID_FLAG_BOOK_LABELS.row(supplementEdition).get(searchedEditionId);
// Note that I'm using the keys to traverse the table. Firstly .row() will return all rows with that key
// And then I get the value from the second key.
// row(X rowKey) -> Map<Y columnKey, Z columnValue>
// row(x).get(y) -> value!
// The following code is to fail fast and avoid NPE.
final Map<Integer, String> row = EDITION_ID_FLAG_BOOK_LABELS.row(supplementEdition);
if (row == null) {
System.err.println("Null row. Invalid Supplement Edition Flag. [0,1]");
}
final String bookLabel = row.get(searchedEditionId);
if (StringUtils.isBlank(bookLabel)) {
System.err.println("Null bookLabel. Invalid Edition Id. [1,2,3,4,5,6,10]");
System.exit(1);
}
System.out.println(bookLabel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment