### Question:
TableId extends AbstractId<TableId> { public static TableId of(final String canonical) { try { return cache.get(canonical, () -> new TableId(canonical)); } catch (ExecutionException e) { throw new AssertionError( "This should never happen: ID constructor should never return null."); } } private  TableId(final String canonical); static TableId of(final String canonical);  }### Answer:
@Test public void testCacheNoDuplicates() { assertNotSame(RootTable.ID, MetadataTable.ID); assertNotSame(RootTable.ID, ReplicationTable.ID); String tableString = "table-" + name.getMethodName(); long initialSize = TableId.cache.asMap().entrySet().stream().count(); TableId table1 = TableId.of(tableString); assertEquals(initialSize + 1, TableId.cache.asMap().entrySet().stream().count()); assertEquals(tableString, table1.canonical()); TableId builtInTableId = TableId.of("!0"); assertSame(MetadataTable.ID, builtInTableId); builtInTableId = TableId.of("+r"); assertSame(RootTable.ID, builtInTableId); builtInTableId = TableId.of("+rep"); assertSame(ReplicationTable.ID, builtInTableId); table1 = TableId.of(tableString); assertEquals(initialSize + 1, TableId.cache.asMap().entrySet().stream().count()); assertEquals(tableString, table1.canonical()); TableId table2 = TableId.of(tableString); assertEquals(initialSize + 1, TableId.cache.asMap().entrySet().stream().count()); assertEquals(tableString, table2.canonical()); assertSame(table1, table2); }
@Test(timeout = 30_000) public void testCacheIncreasesAndDecreasesAfterGC() { long initialSize = TableId.cache.asMap().entrySet().stream().count(); assertTrue(initialSize < 20); LOG.info("Initial cache size: {}", initialSize); LOG.info(TableId.cache.asMap().toString()); String tableString = "table-" + name.getMethodName(); TableId table1 = TableId.of(tableString); assertEquals(initialSize + 1, TableId.cache.asMap().entrySet().stream().count()); assertEquals(tableString, table1.canonical()); for (int i = 0; i < 999; i++) { TableId.of(new String("table" + i)); } long preGCSize = TableId.cache.asMap().entrySet().stream().count(); LOG.info("Entries before System.gc(): {}", preGCSize); assertTrue(preGCSize > 500); long postGCSize = preGCSize; while (postGCSize >= preGCSize) { tryToGc(); postGCSize = TableId.cache.asMap().entrySet().stream().count(); LOG.info("Entries after System.gc(): {}", postGCSize); } }