com.google.common.collect.Maps.asConverter()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(184)

本文整理了Java中com.google.common.collect.Maps.asConverter()方法的一些代码示例,展示了Maps.asConverter()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Maps.asConverter()方法的具体详情如下:
包路径:com.google.common.collect.Maps
类名称:Maps
方法名:asConverter

Maps.asConverter介绍

[英]Returns a Converter that converts values using BiMap#get, and whose inverse view converts values using BiMap#inverse .get()
[中]返回使用BiMap#get转换值的转换器,以及使用BiMap#inverse转换值的反转视图。得到()

代码示例

代码示例来源:origin: google/guava

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) {
 }
}

代码示例来源:origin: google/guava

public void testAsConverter_toString() {
 ImmutableBiMap<String, Integer> biMap =
   ImmutableBiMap.of(
     "one", 1,
     "two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 assertEquals("Maps.asConverter({one=1, two=2})", converter.toString());
}

代码示例来源:origin: google/guava

public void testAsConverter_serialization() {
 ImmutableBiMap<String, Integer> biMap =
   ImmutableBiMap.of(
     "one", 1,
     "two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 SerializableTester.reserializeAndAssert(converter);
}

代码示例来源:origin: google/guava

public void testAsConverter_nominal() throws Exception {
 ImmutableBiMap<String, Integer> biMap =
   ImmutableBiMap.of(
     "one", 1,
     "two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 for (Entry<String, Integer> entry : biMap.entrySet()) {
  assertSame(entry.getValue(), converter.convert(entry.getKey()));
 }
}

代码示例来源:origin: google/guava

public void testAsConverter_inverse() throws Exception {
 ImmutableBiMap<String, Integer> biMap =
   ImmutableBiMap.of(
     "one", 1,
     "two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 for (Entry<String, Integer> entry : biMap.entrySet()) {
  assertSame(entry.getKey(), converter.reverse().convert(entry.getValue()));
 }
}

代码示例来源:origin: google/guava

public void testAsConverter_noMapping() throws Exception {
 ImmutableBiMap<String, Integer> biMap =
   ImmutableBiMap.of(
     "one", 1,
     "two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 try {
  converter.convert("three");
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

代码示例来源:origin: google/guava

public void testAsConverter_isAView() throws Exception {
 BiMap<String, Integer> biMap = HashBiMap.create();
 biMap.put("one", 1);
 biMap.put("two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 assertSame(1, converter.convert("one"));
 assertSame(2, converter.convert("two"));
 try {
  converter.convert("three");
  fail();
 } catch (IllegalArgumentException expected) {
 }
 biMap.put("three", 3);
 assertSame(1, converter.convert("one"));
 assertSame(2, converter.convert("two"));
 assertSame(3, converter.convert("three"));
}

代码示例来源:origin: google/guava

public void testAsConverter_nullConversions() throws Exception {
 ImmutableBiMap<String, Integer> biMap =
   ImmutableBiMap.of(
     "one", 1,
     "two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 assertNull(converter.convert(null));
 assertNull(converter.reverse().convert(null));
}

代码示例来源:origin: com.google.guava/guava-tests

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) {
 }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testAsConverter_toString() {
 ImmutableBiMap<String, Integer> biMap = ImmutableBiMap.of(
   "one", 1,
   "two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 assertEquals("Maps.asConverter({one=1, two=2})", converter.toString());
}

代码示例来源:origin: com.google.guava/guava-tests

public void testAsConverter_serialization() {
 ImmutableBiMap<String, Integer> biMap = ImmutableBiMap.of(
   "one", 1,
   "two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 SerializableTester.reserializeAndAssert(converter);
}

代码示例来源:origin: org.opendaylight.yangtools/yang-data-api

/**
   * 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());
  }
}

代码示例来源:origin: opendaylight/yangtools

/**
   * 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());
  }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testAsConverter_nominal() throws Exception {
 ImmutableBiMap<String, Integer> biMap = ImmutableBiMap.of(
   "one", 1,
   "two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 for (Entry<String, Integer> entry : biMap.entrySet()) {
  assertSame(entry.getValue(), converter.convert(entry.getKey()));
 }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testAsConverter_inverse() throws Exception {
 ImmutableBiMap<String, Integer> biMap = ImmutableBiMap.of(
   "one", 1,
   "two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 for (Entry<String, Integer> entry : biMap.entrySet()) {
  assertSame(entry.getKey(), converter.reverse().convert(entry.getValue()));
 }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testAsConverter_noMapping() throws Exception {
 ImmutableBiMap<String, Integer> biMap = ImmutableBiMap.of(
   "one", 1,
   "two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 try {
  converter.convert("three");
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testAsConverter_isAView() throws Exception {
 BiMap<String, Integer> biMap = HashBiMap.create();
 biMap.put("one", 1);
 biMap.put("two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 assertSame(1, converter.convert("one"));
 assertSame(2, converter.convert("two"));
 try {
  converter.convert("three");
  fail();
 } catch (IllegalArgumentException expected) {
 }
 biMap.put("three", 3);
 assertSame(1, converter.convert("one"));
 assertSame(2, converter.convert("two"));
 assertSame(3, converter.convert("three"));
}

代码示例来源:origin: com.google.guava/guava-tests

public void testAsConverter_nullConversions() throws Exception {
 ImmutableBiMap<String, Integer> biMap = ImmutableBiMap.of(
   "one", 1,
   "two", 2);
 Converter<String, Integer> converter = Maps.asConverter(biMap);
 assertNull(converter.convert(null));
 assertNull(converter.reverse().convert(null));
}

相关文章