@VisibleForTesting static List<String> getUniqueDimensions( List<TimelineObjectHolder<String, DataSegment>> timelineSegments, @Nullable Set<String> excludeDimensions ) { final BiMap<String, Integer> uniqueDims = HashBiMap.create(); // Here, we try to retain the order of dimensions as they were specified since the order of dimensions may be // optimized for performance. // Dimensions are extracted from the recent segments to olders because recent segments are likely to be queried more // frequently, and thus the performance should be optimized for recent ones rather than old ones. // timelineSegments are sorted in order of interval int index = 0; for (TimelineObjectHolder<String, DataSegment> timelineHolder : Lists.reverse(timelineSegments)) { for (PartitionChunk<DataSegment> chunk : timelineHolder.getObject()) { for (String dimension : chunk.getObject().getDimensions()) { if (!uniqueDims.containsKey(dimension) && (excludeDimensions == null || !excludeDimensions.contains(dimension))) { uniqueDims.put(dimension, index++); } } } } final BiMap<Integer, String> orderedDims = uniqueDims.inverse(); return IntStream.range(0, orderedDims.size()) .mapToObj(orderedDims::get) .collect(Collectors.toList()); }
public static String getBitmexCurrencyCode(Currency currency) { if (currency.getIso4217Currency() != null) { currency = currency.getIso4217Currency(); } String bitmexCode = assetsMap.inverse().get(currency); if (bitmexCode == null) { throw new ExchangeException("Bitmex does not support the currency code " + currency); } return bitmexCode; }
@Override public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { checkNotNull(function); BiEntry<K, V> oldFirst = firstInKeyInsertionOrder; clear(); for (BiEntry<K, V> entry = oldFirst; entry != null; entry = entry.nextInKeyInsertionOrder) { put(entry.key, function.apply(entry.key, entry.value)); } }
/** * Constructs a new, empty bimap with the specified expected size. * * @param expectedSize the expected number of entries * @throws IllegalArgumentException if the specified expected size is negative */ public static <K, V> HashBiMap<K, V> create(int expectedSize) { return new HashBiMap<>(expectedSize); }
/** * Returns the existing {@link NamedToroIndex named toro index} that has * the same associated info than the candidate given or null if there is * no index that fulfil that condition * <p> * @param candidate * @return */ @Nullable public NamedToroIndex getToroIndex(UnnamedToroIndex candidate) { return memory.toroNamedToUnnamed.inverse().get(candidate); }
public void testInverseInsertionOrderAfterInverseForcePut() { BiMap<String, Integer> map = HashBiMap.create(); map.put("foo", 1); map.put("bar", 2); map.put("quux", 3); map.inverse().forcePut(1, "quux"); assertThat(map.inverse().entrySet()) .containsExactly(Maps.immutableEntry(2, "bar"), Maps.immutableEntry(1, "quux")) .inOrder(); }
public void testUnmodifiableBiMap() { BiMap<Integer, String> mod = HashBiMap.create(); mod.put(1, "one"); mod.put(2, "two"); mod.put(3, "three"); BiMap<Number, String> unmod = Maps.<Number, String>unmodifiableBiMap(mod);
/** See {@link SynchronizedBiMapTest} for more tests. */ public void testSynchronizedBiMap() { BiMap<String, Integer> bimap = HashBiMap.create(); bimap.put("one", 1); BiMap<String, Integer> sync = Maps.synchronizedBiMap(bimap); bimap.put("two", 2); sync.put("three", 3); assertEquals(ImmutableSet.of(1, 2, 3), bimap.inverse().keySet()); assertEquals(ImmutableSet.of(1, 2, 3), sync.inverse().keySet()); }
public void testMapConstructor() { /* Test with non-empty Map. */ Map<String, String> map = ImmutableMap.of( "canada", "dollar", "chile", "peso", "switzerland", "franc"); HashBiMap<String, String> bimap = HashBiMap.create(map); assertEquals("dollar", bimap.get("canada")); assertEquals("canada", bimap.inverse().get("dollar")); }
public void testCreate() { EnumHashBiMap<Currency, String> bimap = EnumHashBiMap.create(Currency.class); assertTrue(bimap.isEmpty()); assertEquals("{}", bimap.toString()); assertEquals(HashBiMap.create(), bimap); bimap.put(Currency.DOLLAR, "dollar"); assertEquals("dollar", bimap.get(Currency.DOLLAR)); assertEquals(Currency.DOLLAR, bimap.inverse().get("dollar")); }
@Override protected BiMap<String, String> create(Entry<String, String>[] entries) { Object mutex = new Object(); BiMap<String, String> result = HashBiMap.create(); for (Entry<String, String> entry : entries) { checkArgument(!result.containsKey(entry.getKey())); result.put(entry.getKey(), entry.getValue()); } return Maps.synchronizedBiMap(result); } }
@Override protected BiMap<String, String> create(Entry<String, String>[] entries) { BiMap<String, String> bimap = HashBiMap.create(entries.length); for (Entry<String, String> entry : entries) { checkArgument(!bimap.containsKey(entry.getKey())); bimap.put(entry.getKey(), entry.getValue()); } return Maps.unmodifiableBiMap(bimap); } })
@Override protected BiMap<String, String> create(Entry<String, String>[] entries) { Object mutex = new Object(); BiMap<String, String> backing = new TestBiMap<>(HashBiMap.<String, String>create(), mutex); BiMap<String, String> result = Synchronized.biMap(backing, mutex); for (Entry<String, String> entry : entries) { checkArgument(!result.containsKey(entry.getKey())); result.put(entry.getKey(), entry.getValue()); } return result; } }
public void testAsConverter_withNullMapping() throws Exception { BiMap<String, Integer> biMap = HashBiMap.create(); biMap.put("one", 1); biMap.put("two", 2); biMap.put("three", null); try { Maps.asConverter(biMap).convert("three"); fail(); } catch (IllegalArgumentException expected) { } }
public GuidDatasetUrnStateStoreNameParser(FileSystem fs, Path jobStatestoreRootDir) throws IOException { this.fs = fs; this.sanitizedNameToDatasetURNMap = Maps.synchronizedBiMap(HashBiMap.<String, String>create()); this.versionIdentifier = new Path(jobStatestoreRootDir, StateStoreNameVersion.V1.getDatasetUrnNameMapFile()); if (this.fs.exists(versionIdentifier)) { this.version = StateStoreNameVersion.V1; try (InputStream in = this.fs.open(versionIdentifier)) { LineReader lineReader = new LineReader(new InputStreamReader(in, Charsets.UTF_8)); String shortenName = lineReader.readLine(); while (shortenName != null) { String datasetUrn = lineReader.readLine(); this.sanitizedNameToDatasetURNMap.put(shortenName, datasetUrn); shortenName = lineReader.readLine(); } } } else { this.version = StateStoreNameVersion.V0; } }
@Override protected BiMap<String, String> create(Entry<String, String>[] entries) { BiMap<String, String> map = HashBiMap.create(); putEntries(map, entries); map.putAll(ENTRIES_TO_FILTER); return Maps.filterKeys(map, FILTER_KEYS); } })