本文整理了Java中com.google.common.base.Converter
类的一些代码示例,展示了Converter
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Converter
类的具体详情如下:
包路径:com.google.common.base.Converter
类名称:Converter
[英]A function from A to B with an associated reverse function from Bto A; used for converting back and forth between different representations of the same information.
The reverse operation may be a strict inverse (meaning that converter.reverse().convert(converter.convert(a)).equals(a) is always true). However, it is very common (perhaps more common) for round-trip conversion to be lossy. Consider an example round-trip using com.google.common.primitives.Doubles#stringConverter:
Note that it should still be the case that the round-tripped and original objects are similar.
A converter always converts null to null and non-null references to non-null references. It would not make sense to consider null and a non-null reference to be "different representations of the same information", since one is distinguishable from missing information and the other is not. The #convert method handles this null behavior for all converters; implementations of #doForward and #doBackward are guaranteed to never be passed null, and must never return null.
Getting a converter:
Using a converter:
return new Converter<Integer, String>() {
protected String doForward(Integer i) {
return Integer.toHexString(i);
}
protected Integer doBackward(String s) {
return parseUnsignedInt(s, 16);
}
};
An alternative using Java 8:
return Converter.from(
[中]从A到B的功能,以及从B到A的相关反向功能;用于在相同信息的不同表示之间来回转换。
####可逆性
反向操作可能是严格的反向操作(即converter.reverse()。转换(convert.convert(a))。等于(a)总是正确的)。然而,往返转换有损是非常常见的(可能更常见)。考虑使用COM的往返示例。谷歌。常见的原语。双列#stringConverter:
1.stringConverter()。convert(“1.00”)返回双精度值1.0
1.stringConverter()。反向()。convert(1.0)返回字符串“1.0”--与我们开始使用的字符串(“1.00”)不同
请注意,往返对象和原始对象应该是相似的。
####无效性
转换器总是将空引用转换为空引用,将非空引用转换为非空引用。考虑NULL和非空引用是“相同信息的不同表示”,这是没有意义的,因为一个可以区别于丢失的信息,而另一个不是。#convert方法处理所有转换器的这种空行为;#doForward和#doBackward的实现保证永远不会传递null,并且永远不能返回null。
####常用的使用方法
获取转换器:
*使用提供的转换器实现,例如Enums#stringConverter、com。谷歌。常见的原语。Ints#stringConverter或其#反向视图。
*使用com在特定预设值之间转换。谷歌。常见的收集地图是一个转换器。例如,使用它为单元测试创建一个“假”转换器。使用模拟框架模拟转换器类型是不必要的(而且容易混淆)。
*扩展此类并实现其#doForward和#doBackward方法。
*Java8用户:您可能更愿意将两个lambda表达式或方法引用传递给#from factory方法。
使用转换器:
*使用转换器在“正向”方向转换一个实例。转换(a)。
*使用转换器“正向”转换多个实例。convertAll(as)。
*使用转换器在“向后”方向进行转换。反向()。转换(b)或转换器。反向()。康沃尔特(bs)。
*使用转换器或转换器。reverse()在java中的任何位置。util。作用函数被接受(例如java.util.stream.stream#map)。
*不要直接呼叫#doForward或#doBackward;这些存在只是为了被覆盖。
####范例
return new Converter<Integer, String>() {
protected String doForward(Integer i) {
return Integer.toHexString(i);
}
protected Integer doBackward(String s) {
return parseUnsignedInt(s, 16);
}
};
使用Java 8的替代方案:
return Converter.from(
代码示例来源: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
@Override
public boolean equals(@Nullable Object object) {
if (object instanceof ConverterComposition) {
ConverterComposition<?, ?, ?> that = (ConverterComposition<?, ?, ?>) object;
return this.first.equals(that.first) && this.second.equals(that.second);
}
return false;
}
代码示例来源:origin: google/guava
@Override
@Nullable
A correctedDoBackward(@Nullable C c) {
return first.correctedDoBackward(second.correctedDoBackward(c));
}
代码示例来源: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());
}
代码示例来源:origin: google/guava
public void testConverter() {
assertEquals(LONG_VAL, STR_TO_LONG.convert(STR_VAL));
assertEquals(STR_VAL, STR_TO_LONG.reverse().convert(LONG_VAL));
Iterable<Long> convertedValues = STR_TO_LONG.convertAll(STRINGS);
assertEquals(LONGS, ImmutableList.copyOf(convertedValues));
}
代码示例来源:origin: com.google.guava/guava-jdk5
@Override
@Nullable
C correctedDoForward(@Nullable A a) {
return second.correctedDoForward(first.correctedDoForward(a));
}
代码示例来源:origin: com.google.guava/guava-jdk5
@Override
@Nullable
A correctedDoBackward(@Nullable C c) {
return first.correctedDoBackward(second.correctedDoBackward(c));
}
代码示例来源:origin: com.google.guava/guava-jdk5
@Override
public boolean equals(@Nullable Object object) {
if (object instanceof ConverterComposition) {
ConverterComposition<?, ?, ?> that = (ConverterComposition<?, ?, ?>) object;
return this.first.equals(that.first)
&& this.second.equals(that.second);
}
return false;
}
代码示例来源:origin: com.google.guava/guava-jdk5
/**
* @deprecated Provided to satisfy the {@code Function} interface; use {@link #convert} instead.
*/
@Deprecated
@Override
@Nullable
public final B apply(@Nullable A a) {
return convert(a);
}
代码示例来源:origin: org.fcrepo/fcrepo-kernel-modeshape
/**
* Get the membership resource relation asserted by the container
* @param parent resource with relations
* @return Node of member
* @throws RepositoryException on error
*/
private Node getMemberResource(final FedoraResource parent) throws RepositoryException {
final Node membershipResource;
if (parent.hasProperty(LDP_MEMBER_RESOURCE)) {
final Property memberResource = getJcrNode(parent).getProperty(LDP_MEMBER_RESOURCE);
if (REFERENCE_TYPES.contains(memberResource.getType())) {
membershipResource = nodeConverter().convert(memberResource.getNode()).asNode();
} else {
membershipResource = createURI(memberResource.getString());
}
} else {
membershipResource = uriFor(parent);
}
return membershipResource;
}
}
代码示例来源:origin: org.fcrepo/fcrepo-http-api
/**
* This is a helper method for using the idTranslator to convert this resource into an associated Jena Node.
*
* @param resource to be converted into a Jena Node
* @return the Jena node
*/
protected Node asNode(final FedoraResource resource) {
return translator().reverse().convert(resource).asNode();
}
代码示例来源:origin: org.fcrepo/fcrepo-kernel-modeshape
@Override
public String asString(final Resource subject) {
if (!inDomain(subject)) {
return null;
}
final String path = subject.getURI().substring(resourceNamespace.length() - 1);
final String absPath = forward.convert(path);
if (absPath.isEmpty()) {
return "/";
}
return absPath;
}
代码示例来源:origin: org.fcrepo/fcrepo-http-api
private static void addContentStatements(final IdentifierConverter<Resource,FedoraResource> idTranslator,
final FedoraBinary resource,
final Model model) {
// fcr:fixity
final Resource subject = idTranslator.reverse().convert(resource);
model.add(subject, HAS_FIXITY_SERVICE, createResource(subject.getURI() +
"/fcr:fixity"));
}
代码示例来源:origin: org.fcrepo/fcrepo-kernel-modeshape
@Override
public RdfStream getTriples(final IdentifierConverter<Resource, FedoraResource> idTranslator,
final Set<? extends TripleCategory> contexts) {
final FedoraResource described = getOriginalResource().getDescribedResource();
final org.apache.jena.graph.Node describedNode = idTranslator.reverse().convert(described).asNode();
final String resourceUri = idTranslator.reverse().convert(this).getURI();
Stream<Triple> triples = contexts.stream()
.filter(contextMap::containsKey)
.map(x -> contextMap.get(x).apply(this).apply(idTranslator).apply(contexts.contains(MINIMAL)))
.reduce(empty(), Stream::concat)
.map(t -> mapSubject(t, resourceUri, describedNode));
// if a memento, convert subjects to original resource and object references from referential integrity
// ignoring internal URL back the original external URL.
if (isMemento()) {
final IdentifierConverter<Resource, FedoraResource> internalIdTranslator =
new InternalIdentifierTranslator(getSession());
triples = triples.map(convertMementoReferences(idTranslator, internalIdTranslator));
}
return new DefaultRdfStream(idTranslator.reverse().convert(described).asNode(), triples);
}
代码示例来源: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: org.fcrepo/fcrepo-http-api
@Override
protected void addResourceHttpHeaders(final FedoraResource resource) {
super.addResourceHttpHeaders(resource);
if (session.isBatchSession()) {
final String canonical = translator().reverse()
.convert(resource)
.toString()
.replaceFirst("/tx:[^/]+", "");
servletResponse.addHeader(LINK, "<" + canonical + ">;rel=\"canonical\"");
}
addExternalContentHeaders(resource);
}
代码示例来源:origin: SmartDataAnalytics/jena-sparql-api
@Override
protected R doBackward(J b) {
Node node = converter.reverse().convert(b);//nodeMapper.toNode(b);
RDFNode tmp = model.asRDFNode(node);
R result = tmp.as(rdfViewClass);
return result;
}
}
代码示例来源:origin: google/guava
public void testSerialization_andThen() {
Converter<String, Long> converterA = Longs.stringConverter();
Converter<Long, String> reverseConverter = Longs.stringConverter().reverse();
Converter<String, String> composedConverter = converterA.andThen(reverseConverter);
SerializableTester.reserializeAndAssert(composedConverter);
}
代码示例来源:origin: google/guava
@Override
@Nullable
C correctedDoForward(@Nullable A a) {
return second.correctedDoForward(first.correctedDoForward(a));
}
代码示例来源:origin: Nextdoor/bender
@Override
@Nullable
C correctedDoForward(@Nullable A a) {
return second.correctedDoForward(first.correctedDoForward(a));
}
内容来源于网络,如有侵权,请联系作者删除!