public void testBuilderPutAll() { Map<String, Integer> toPut = new LinkedHashMap<String, Integer>(); toPut.put("one", 1); toPut.put("two", 2); toPut.put("three", 3); Map<String, Integer> moreToPut = new LinkedHashMap<String, Integer>(); moreToPut.put("four", 4); moreToPut.put("five", 5); ImmutableBiMap<String, Integer> map = new Builder<String, Integer>() .putAll(toPut) .putAll(moreToPut) .build(); assertMapEquals(map, "one", 1, "two", 2, "three", 3, "four", 4, "five", 5); assertMapEquals(map.inverse(), 1, "one", 2, "two", 3, "three", 4, "four", 5, "five"); }
public void testBuilderReuse() { Builder<String, Integer> builder = new Builder<String, Integer>(); ImmutableBiMap<String, Integer> mapOne = builder .put("one", 1) .put("two", 2) .build(); ImmutableBiMap<String, Integer> mapTwo = builder .put("three", 3) .put("four", 4) .build(); assertMapEquals(mapOne, "one", 1, "two", 2); assertMapEquals(mapOne.inverse(), 1, "one", 2, "two"); assertMapEquals(mapTwo, "one", 1, "two", 2, "three", 3, "four", 4); assertMapEquals(mapTwo.inverse(), 1, "one", 2, "two", 3, "three", 4, "four"); }
public void testDuplicateValues() { ImmutableMap<String, Integer> map = new ImmutableMap.Builder<String, Integer>() .put("one", 1) .put("two", 2) .put("uno", 1) .put("dos", 2) .build(); try { ImmutableBiMap.copyOf(map); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage()).contains("1"); } }
Long resolveShardForPath(final YangInstanceIdentifier path) { final String shardName = actorContext.getShardStrategyFactory().getStrategy(path).findShard(path); Long cookie = shards.get(shardName); if (cookie == null) { synchronized (this) { cookie = shards.get(shardName); if (cookie == null) { cookie = nextShard++; Builder<String, Long> builder = ImmutableBiMap.builder(); builder.putAll(shards); builder.put(shardName, cookie); shards = builder.build(); } } } return cookie; }
public void testDuplicateValues() { ImmutableMap<String, Integer> map = new ImmutableMap.Builder<String, Integer>() .put("one", 1) .put("two", 2) .put("uno", 1) .put("dos", 2) .build(); try { ImmutableBiMap.copyOf(map); fail(); } catch (IllegalArgumentException expected) { assertTrue(expected.getMessage().contains("1")); } }
/** * Create a prefix {@link Converter} for {@link XPathExpressionException} defined in a particular YANG * {@link Module} .Instantiation requires establishing how a module's imports are mapped to actual modules * and their namespaces. This information is cached and used for improved lookups. * * @param ctx A SchemaContext * @param module Module in which the XPath is defined * @return A new Converter */ public static @Nonnull Converter<String, QNameModule> create(final SchemaContext ctx, final Module module) { // Always check for null ctx requireNonNull(ctx, "Schema context may not be null"); // Use immutable map builder for detection of duplicates (which should never occur) final Builder<String, QNameModule> b = ImmutableBiMap.builder(); b.put(module.getPrefix(), module.getQNameModule()); for (ModuleImport i : module.getImports()) { final Optional<Module> mod = ctx.findModule(i.getModuleName(), i.getRevision()); checkArgument(mod.isPresent(), "Unsatisfied import of %s by module %s", i, module); b.put(i.getPrefix(), mod.get().getQNameModule()); } return Maps.asConverter(b.build()); }
public void testEmptyBuilder() { ImmutableBiMap<String, Integer> map = new Builder<String, Integer>().build(); assertEquals(Collections.<String, Integer>emptyMap(), map); assertEquals(Collections.<Integer, String>emptyMap(), map.inverse()); assertSame(ImmutableBiMap.of(), map); }
public void testSingletonBuilder() { ImmutableBiMap<String, Integer> map = new Builder<String, Integer>() .put("one", 1) .build(); assertMapEquals(map, "one", 1); assertMapEquals(map.inverse(), 1, "one"); }
public void testBuilder_orderEntriesByValueAfterExactSizeBuild() { ImmutableBiMap.Builder<String, Integer> builder = new ImmutableBiMap.Builder<String, Integer>(2).put("four", 4).put("one", 1); ImmutableMap<String, Integer> keyOrdered = builder.build(); ImmutableMap<String, Integer> valueOrdered = builder.orderEntriesByValue(Ordering.natural()).build(); assertMapEquals(keyOrdered, "four", 4, "one", 1); assertMapEquals(valueOrdered, "one", 1, "four", 4); }
public void testBuilder_orderEntriesByValue_usedTwiceFails() { ImmutableBiMap.Builder<String, Integer> builder = new Builder<String, Integer>() .orderEntriesByValue(Ordering.natural()); try { builder.orderEntriesByValue(Ordering.natural()); fail("Expected IllegalStateException"); } catch (IllegalStateException expected) {} }
public void testBuilderPutNullKey() { Builder<String, Integer> builder = new Builder<String, Integer>(); try { builder.put(null, 1); fail(); } catch (NullPointerException expected) { } }
public void testBuilderPutNullValue() { Builder<String, Integer> builder = new Builder<String, Integer>(); try { builder.put("one", null); fail(); } catch (NullPointerException expected) { } }
public void testBuilderPutNullKeyViaPutAll() { Builder<String, Integer> builder = new Builder<String, Integer>(); try { builder.putAll(Collections.<String, Integer>singletonMap(null, 1)); fail(); } catch (NullPointerException expected) { } }
public void testBuilderPutNullValueViaPutAll() { Builder<String, Integer> builder = new Builder<String, Integer>(); try { builder.putAll(Collections.<String, Integer>singletonMap("one", null)); fail(); } catch (NullPointerException expected) { } }
public void testPuttingTheSameKeyTwiceThrowsOnBuild() { Builder<String, Integer> builder = new Builder<String, Integer>() .put("one", 1) .put("one", 1); // throwing on this line would be even better try { builder.build(); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage()).contains("one"); } }
public void testFromImmutableMap() { ImmutableBiMap<String, Integer> bimap = ImmutableBiMap.copyOf( new ImmutableMap.Builder<String, Integer>() .put("one", 1) .put("two", 2) .put("three", 3) .put("four", 4) .put("five", 5) .build()); assertMapEquals(bimap, "one", 1, "two", 2, "three", 3, "four", 4, "five", 5); assertMapEquals(bimap.inverse(), 1, "one", 2, "two", 3, "three", 4, "four", 5, "five"); }
public void setupLoadOnly(String deobfFileName, boolean loadAll) { try { File mapData = new File(deobfFileName); LZMAInputSupplier zis = new LZMAInputSupplier(new FileInputStream(mapData)); CharSource srgSource = zis.asCharSource(Charsets.UTF_8); List<String> srgList = srgSource.readLines(); rawMethodMaps = Maps.newHashMap(); rawFieldMaps = Maps.newHashMap(); Builder<String, String> builder = ImmutableBiMap.builder(); Splitter splitter = Splitter.on(CharMatcher.anyOf(": ")).omitEmptyStrings().trimResults(); for (String line : srgList) { String[] parts = Iterables.toArray(splitter.split(line),String.class); String typ = parts[0]; if ("CL".equals(typ)) { parseClass(builder, parts); } else if ("MD".equals(typ) && loadAll) { parseMethod(parts); } else if ("FD".equals(typ) && loadAll) { parseField(parts); } } classNameBiMap = builder.build(); } catch (IOException ioe) { FMLRelaunchLog.log(Level.ERROR, "An error occurred loading the deobfuscation map data", ioe); } methodNameMaps = Maps.newHashMapWithExpectedSize(rawMethodMaps.size()); fieldNameMaps = Maps.newHashMapWithExpectedSize(rawFieldMaps.size()); }
public void setupLoadOnly(String deobfFileName, boolean loadAll) { try { File mapData = new File(deobfFileName); LZMAInputSupplier zis = new LZMAInputSupplier(new FileInputStream(mapData)); CharSource srgSource = zis.asCharSource(Charsets.UTF_8); List<String> srgList = srgSource.readLines(); rawMethodMaps = Maps.newHashMap(); rawFieldMaps = Maps.newHashMap(); Builder<String, String> builder = ImmutableBiMap.<String,String>builder(); Splitter splitter = Splitter.on(CharMatcher.anyOf(": ")).omitEmptyStrings().trimResults(); for (String line : srgList) { String[] parts = Iterables.toArray(splitter.split(line),String.class); String typ = parts[0]; if ("CL".equals(typ)) { parseClass(builder, parts); } else if ("MD".equals(typ) && loadAll) { parseMethod(parts); } else if ("FD".equals(typ) && loadAll) { parseField(parts); } } classNameBiMap = builder.build(); } catch (IOException ioe) { FMLRelaunchLog.log(Level.ERROR, "An error occurred loading the deobfuscation map data", ioe); } methodNameMaps = Maps.newHashMapWithExpectedSize(rawMethodMaps.size()); fieldNameMaps = Maps.newHashMapWithExpectedSize(rawFieldMaps.size()); }
public void setup(File mcDir, LaunchClassLoader classLoader, String deobfFileName) { this.classLoader = classLoader; try { InputStream classData = getClass().getResourceAsStream(deobfFileName); LZMAInputSupplier zis = new LZMAInputSupplier(classData); CharSource srgSource = zis.asCharSource(Charsets.UTF_8); List<String> srgList = srgSource.readLines(); rawMethodMaps = Maps.newHashMap(); rawFieldMaps = Maps.newHashMap(); Builder<String, String> builder = ImmutableBiMap.<String,String>builder(); Splitter splitter = Splitter.on(CharMatcher.anyOf(": ")).omitEmptyStrings().trimResults(); for (String line : srgList) { String[] parts = Iterables.toArray(splitter.split(line),String.class); String typ = parts[0]; if ("CL".equals(typ)) { parseClass(builder, parts); } else if ("MD".equals(typ)) { parseMethod(parts); } else if ("FD".equals(typ)) { parseField(parts); } } classNameBiMap = builder.build(); } catch (IOException ioe) { FMLRelaunchLog.log(Level.ERROR, ioe, "An error occurred loading the deobfuscation map data"); } methodNameMaps = Maps.newHashMapWithExpectedSize(rawMethodMaps.size()); fieldNameMaps = Maps.newHashMapWithExpectedSize(rawFieldMaps.size()); }
/** * Restricted constructor */ private SecurityTypesDescriptionProvider() { Builder<String, String> builder = ImmutableBiMap.builder(); AnnotationReflector reflector = AnnotationReflector.getDefaultReflector(); Set<Class<?>> securityClasses = reflector.getReflector().getTypesAnnotatedWith(SecurityDescription.class); for (Class<?> securityClass : securityClasses) { SecurityDescription securityDescriptionAnnotation = securityClass.getAnnotation(SecurityDescription.class); if (securityDescriptionAnnotation != null) { // extract type String type = StringUtils.trimToNull(securityDescriptionAnnotation.type()); if (type == null) { if (ManageableSecurity.class.isAssignableFrom(securityClass)) { MetaBean metaBean = JodaBeanUtils.metaBean(securityClass); ManageableSecurity bareSecurity = (ManageableSecurity) metaBean.builder().build(); type = bareSecurity.getSecurityType(); } else { s_logger.warn("{} anotated with {}, but not subtype of {}", securityClass, SecurityDescription.class, ManageableSecurity.class); } } // extract description String description = StringUtils.trimToNull(securityDescriptionAnnotation.description()); if (description == null) { description = securityClass.getSimpleName(); } builder.put(type, description); } } _type2Description = builder.build(); }