本文整理了Java中com.google.common.base.Converter.convert()
方法的一些代码示例,展示了Converter.convert()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Converter.convert()
方法的具体详情如下:
包路径:com.google.common.base.Converter
类名称:Converter
方法名:convert
[英]Returns a representation of a as an instance of type B.
[中]将a的表示形式作为类型B的实例返回。
代码示例来源:origin: google/guava
/**
* @deprecated Provided to satisfy the {@code Function} interface; use {@link #convert} instead.
*/
@Deprecated
@Override
@CanIgnoreReturnValue
public final @Nullable B apply(@Nullable A a) {
return convert(a);
}
代码示例来源:origin: google/guava
public void testStringConverter_convert() {
Converter<String, Long> converter = Longs.stringConverter();
assertEquals((Long) 1L, converter.convert("1"));
assertEquals((Long) 0L, converter.convert("0"));
assertEquals((Long) (-1L), converter.convert("-1"));
assertEquals((Long) 255L, converter.convert("0xff"));
assertEquals((Long) 255L, converter.convert("0xFF"));
assertEquals((Long) (-255L), converter.convert("-0xFF"));
assertEquals((Long) 255L, converter.convert("#0000FF"));
assertEquals((Long) 438L, converter.convert("0666"));
}
代码示例来源:origin: google/guava
public void testStringConverter_convert() {
Converter<String, Short> converter = Shorts.stringConverter();
assertEquals((Short) (short) 1, converter.convert("1"));
assertEquals((Short) (short) 0, converter.convert("0"));
assertEquals((Short) (short) (-1), converter.convert("-1"));
assertEquals((Short) (short) 255, converter.convert("0xff"));
assertEquals((Short) (short) 255, converter.convert("0xFF"));
assertEquals((Short) (short) (-255), converter.convert("-0xFF"));
assertEquals((Short) (short) 255, converter.convert("#0000FF"));
assertEquals((Short) (short) 438, converter.convert("0666"));
}
代码示例来源: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 testStringConverter_convert() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
assertEquals(TestEnum.CHEETO, converter.convert("CHEETO"));
assertEquals(TestEnum.HONDA, converter.convert("HONDA"));
assertEquals(TestEnum.POODLE, converter.convert("POODLE"));
assertNull(converter.convert(null));
assertNull(converter.reverse().convert(null));
}
代码示例来源:origin: google/guava
void check() {
runTester();
@SuppressWarnings("unchecked") // We are checking it anyway
Converter<String, Integer> defaultConverter =
(Converter<String, Integer>) getDefaultParameterValue(0);
assertEquals(Integer.valueOf(0), defaultConverter.convert("anything"));
assertEquals("", defaultConverter.reverse().convert(123));
assertNull(defaultConverter.convert(null));
assertNull(defaultConverter.reverse().convert(null));
}
}
代码示例来源:origin: google/guava
public void testNullIsNotPassedThrough() {
Converter<String, String> nullsAreHandled = sillyConverter(true);
assertEquals("forward", nullsAreHandled.convert("foo"));
assertEquals(null, nullsAreHandled.convert(null));
assertEquals("backward", nullsAreHandled.reverse().convert("foo"));
assertEquals(null, nullsAreHandled.reverse().convert(null));
}
代码示例来源:origin: google/guava
public void testStringConverter_convertError() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
try {
converter.convert("xxx");
fail();
} catch (IllegalArgumentException expected) {
}
}
代码示例来源:origin: google/guava
public void testStringConverter_convertError() {
try {
Floats.stringConverter().convert("notanumber");
fail();
} catch (NumberFormatException expected) {
}
}
代码示例来源:origin: google/guava
public void testStringConverter_convertError() {
try {
Ints.stringConverter().convert("notanumber");
fail();
} catch (NumberFormatException expected) {
}
}
代码示例来源:origin: google/guava
public void testStringConverter_nullConversions() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
assertNull(converter.convert(null));
assertNull(converter.reverse().convert(null));
}
代码示例来源:origin: google/guava
public void testStringConverter_convertError() {
try {
Doubles.stringConverter().convert("notanumber");
fail();
} catch (NumberFormatException expected) {
}
}
代码示例来源:origin: google/guava
public void testStringConverter_reverse() {
Converter<String, Integer> converter = Ints.stringConverter();
assertEquals("1", converter.reverse().convert(1));
assertEquals("0", converter.reverse().convert(0));
assertEquals("-1", converter.reverse().convert(-1));
assertEquals("255", converter.reverse().convert(0xff));
assertEquals("255", converter.reverse().convert(0xFF));
assertEquals("-255", converter.reverse().convert(-0xFF));
assertEquals("438", converter.reverse().convert(0666));
}
代码示例来源:origin: google/guava
public void testStringConverter_reverse() {
Converter<String, Long> converter = Longs.stringConverter();
assertEquals("1", converter.reverse().convert(1L));
assertEquals("0", converter.reverse().convert(0L));
assertEquals("-1", converter.reverse().convert(-1L));
assertEquals("255", converter.reverse().convert(0xffL));
assertEquals("255", converter.reverse().convert(0xFFL));
assertEquals("-255", converter.reverse().convert(-0xFFL));
assertEquals("438", converter.reverse().convert(0666L));
}
代码示例来源:origin: google/guava
public void testStringConverter_reverse() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
assertEquals("CHEETO", converter.reverse().convert(TestEnum.CHEETO));
assertEquals("HONDA", converter.reverse().convert(TestEnum.HONDA));
assertEquals("POODLE", converter.reverse().convert(TestEnum.POODLE));
}
代码示例来源:origin: google/guava
@GwtIncompatible // Double.toString returns different value in GWT.
public void testStringConverter_reverse() {
Converter<String, Double> converter = Doubles.stringConverter();
assertEquals("1.0", converter.reverse().convert(1.0));
assertEquals("0.0", converter.reverse().convert(0.0));
assertEquals("-1.0", converter.reverse().convert(-1.0));
assertEquals("1000000.0", converter.reverse().convert(1e6));
assertEquals("1.0E-6", converter.reverse().convert(1e-6));
}
代码示例来源: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: google/guava
public void testConverterToBackward() {
assertEquals("FOO_BAR", UPPER_UNDERSCORE.converterTo(UPPER_CAMEL).reverse().convert("FooBar"));
assertEquals("FOO_BAR", UPPER_UNDERSCORE.converterTo(LOWER_CAMEL).reverse().convert("fooBar"));
assertEquals("FooBar", UPPER_CAMEL.converterTo(UPPER_UNDERSCORE).reverse().convert("FOO_BAR"));
assertEquals("fooBar", LOWER_CAMEL.converterTo(UPPER_UNDERSCORE).reverse().convert("FOO_BAR"));
}
代码示例来源:origin: google/guava
public void testConverter_nullConversions() {
for (CaseFormat outer : CaseFormat.values()) {
for (CaseFormat inner : CaseFormat.values()) {
assertNull(outer.converterTo(inner).convert(null));
assertNull(outer.converterTo(inner).reverse().convert(null));
}
}
}
代码示例来源:origin: google/guava
public void testIdentityConverter() {
Converter<String, String> stringIdentityConverter = Converter.identity();
assertSame(stringIdentityConverter, stringIdentityConverter.reverse());
assertSame(STR_TO_LONG, stringIdentityConverter.andThen(STR_TO_LONG));
assertSame(STR_VAL, stringIdentityConverter.convert(STR_VAL));
assertSame(STR_VAL, stringIdentityConverter.reverse().convert(STR_VAL));
assertEquals("Converter.identity()", stringIdentityConverter.toString());
assertSame(Converter.identity(), Converter.identity());
}
内容来源于网络,如有侵权,请联系作者删除!