本文整理汇总了Java中com.google.common.base.Converter类的典型用法代码示例。如果您正苦于以下问题:Java Converter类的具体用法?Java Converter怎么用?Java Converter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Converter类属于com.google.common.base包,在下文中一共展示了Converter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAsConverter_isAView
import com.google.common.base.Converter; //导入依赖的package包/类
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"));
}
示例2: determine
import com.google.common.base.Converter; //导入依赖的package包/类
/**
* Determine the converter with the field type
* @param field the field
* @return the Converter
*/
public static Converter<String, ?> determine(Field field) {
Class clazz = field.getType();
// Primitive type
Converter<String, ?> converter = determinePrimitiveConverter(clazz);
if (converter != null){
return converter;
}
// List or Set or Map
if (List.class.equals(clazz) || Map.class.equals(clazz) || Set.class.equals(clazz)){
return determineCollectionConverter((ParameterizedType)field.getGenericType());
}
// Common Json Object
return new JsonConverter(clazz);
}
示例3: determinePrimitiveConverter
import com.google.common.base.Converter; //导入依赖的package包/类
/**
* Determine the primitive type's converter
* @param type the primitive type
* @return the primitive type's converter or null
*/
public static Converter<String, ?> determinePrimitiveConverter(Type type){
if (String.class.equals(type)){
return StringConverter.INSTANCE;
} else if (Boolean.class.equals(type) || boolean.class.equals(type)){
return BooleanConverter.INSTANCE;
} else if (Integer.class.equals(type) || int.class.equals(type)){
return Ints.stringConverter();
} else if (Long.class.equals(type) || long.class.equals(type)) {
return Longs.stringConverter();
} else if (Short.class.equals(type) || short.class.equals(type)){
return Shorts.stringConverter();
} else if (Float.class.equals(type) || float.class.equals(type)){
return Floats.stringConverter();
} else if (Double.class.equals(type) || double.class.equals(type)){
return Doubles.stringConverter();
}
return null;
}
示例4: get
import com.google.common.base.Converter; //导入依赖的package包/类
@Override
public <T> T get(final PropertyId<T> propertyId) {
Preconditions.checkNotNull(propertyId);
Pair<Converter<String, T>, Supplier<T>> information = getRegistrationInformation(propertyId);
T value = information.getValue0().convert(getAsStringUnsafe(propertyId));
if (value == null) {
T defaultValue = information.getValue1().get();
if (defaultValue != null) {
value = defaultValue;
LOGGER.debug(String.format("Property '%1s' has no value, fallback on default value.", propertyId));
} else {
LOGGER.info(String.format("Property '%1s' has no value and default value is undefined.", propertyId));
}
}
return value;
}
示例5: getAsString
import com.google.common.base.Converter; //导入依赖的package包/类
@Override
public <T> String getAsString(final PropertyId<T> propertyId) {
Pair<Converter<String, T>, Supplier<T>> information = getRegistrationInformation(propertyId);
String valueAsString = getAsStringUnsafe(propertyId);
if (valueAsString == null) {
T defaultValue = information.getValue1().get();
if (defaultValue != null) {
valueAsString = information.getValue0().reverse().convert(defaultValue);
LOGGER.debug(String.format("Property '%1s' has no value, fallback on default value.", propertyId));
} else {
LOGGER.info(String.format("Property '%1s' has no value and default value is undefined.", propertyId));
}
}
return valueAsString;
}
示例6: buildReflecter
import com.google.common.base.Converter; //导入依赖的package包/类
private <T> Reflecter<T> buildReflecter(final Reflecter<T> ref) {
addJsonExchangeFunc(ref).autoExchange();
if (jsoner.isPresent() && !jsoner.get().typeConverts.isEmpty()) {
ref.fieldLoop(new Decisional<Field>() {
@Override protected void decision(Field input) {
Class<?> type = input.getType();
Converter<?, ?> converter = jsoner.get().getTypeConverter(type);
if (null != converter) {
ref.exchange(converter, input.getName());
}
}
});
}
if (!this.mappingFuncs.isEmpty()) {
ref.setExchangeFuncs(this.mappingFuncs);
}
return ref;
}
示例7: assignFieldValue
import com.google.common.base.Converter; //导入依赖的package包/类
private void assignFieldValue(Object instance, Field field, String cellValue) throws Exception {
Object fieldValue = null;
if (field.getType().equals(String.class)) {
fieldValue = cellValue;
} else {
Converter<String, ?> converter = getConverter(field);
if (converter == null) {
throw new Exception(String.format("No converter found for field : s%' with type :'s%'",
field.getName(), field.getType().getSimpleName()));
}
fieldValue = converter.convert(cellValue);
}
field.set(instance, fieldValue);
}
示例8: defaultConverter
import com.google.common.base.Converter; //导入依赖的package包/类
private <F, T> Converter<F, T> defaultConverter(
final TypeToken<F> convertFromType, final TypeToken<T> convertToType) {
return new Converter<F, T>() {
@Override
protected T doForward(F a) {
return doConvert(convertToType);
}
@Override
protected F doBackward(T b) {
return doConvert(convertFromType);
}
private /*static*/ <S> S doConvert(TypeToken<S> type) {
return checkNotNull(getDefaultValue(type));
}
};
}
示例9: stringConverter
import com.google.common.base.Converter; //导入依赖的package包/类
/**
* Method stringConverter.
* @param enumClass Class<E>
* @param or E
* @return Converter<String,E>
*/
public static <E extends Enum<E>> Converter<String,E> stringConverter(
final Class<E> enumClass,
final E or) {
return new Converter<String,E>() {
@Override
protected String doBackward(E e) {
return checkNotNull(e).name();
}
@Override
protected E doForward(String s) {
return getIfPresent(enumClass, s).or(or);
}
};
}
示例10: toLowerConverter
import com.google.common.base.Converter; //导入依赖的package包/类
/**
* Method toLowerConverter.
* @param locale Locale
* @return Converter<String,String>
*/
public static Converter<String,String> toLowerConverter(final Locale locale) {
return new Converter<String,String>() {
@Override
protected String doForward(String a) {
return a.toLowerCase(locale);
}
@Override
protected String doBackward(String b) {
return b.toUpperCase(locale);
}
};
}
示例11: maps_converter
import com.google.common.base.Converter; //导入依赖的package包/类
@Test
public void maps_converter () {
BiMap<String, String> stateCapitals =
HashBiMap.create();
stateCapitals.put("Wisconsin", "Madison");
stateCapitals.put("Iowa", "Des Moines");
stateCapitals.put("Minnesota", "Saint Paul");
stateCapitals.put("Illinois", "Springfield");
stateCapitals.put("Michigan", "Lansing");
Converter<String, String> converter = Maps.asConverter(stateCapitals);
String state = converter.reverse().convert("Madison");
assertEquals("Wisconsin", state);
}
示例12: create
import com.google.common.base.Converter; //导入依赖的package包/类
/**
* 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());
}
示例13: create
import com.google.common.base.Converter; //导入依赖的package包/类
static JaxenXPath create(final Converter<String, QNameModule> converter, final SchemaPath schemaPath,
final String xpath) throws JaxenException {
final BaseXPath compiled = new BaseXPath(xpath) {
private static final long serialVersionUID = 1L;
@Override
protected ContextSupport getContextSupport() {
throw new UnsupportedOperationException(xpath);
}
};
final Expr expr = compiled.getRootExpr();
LOG.debug("Compiled {} to expression {}", xpath, expr);
new ExprWalker(new ExprListener() {
// FIXME: perform expression introspection to understand things like apex, etc.
}).walk(expr);
return new JaxenXPath(converter, schemaPath, compiled);
}
示例14: testIsMemberOfRelationToExternalResource
import com.google.common.base.Converter; //导入依赖的package包/类
@Test
public void testIsMemberOfRelationToExternalResource() throws RepositoryException {
when(mockContainer.hasType(LDP_DIRECT_CONTAINER)).thenReturn(true);
when(mockContainer.hasProperty(LDP_IS_MEMBER_OF_RELATION)).thenReturn(true);
when(mockContainer.hasProperty(LDP_MEMBER_RESOURCE)).thenReturn(true);
when(mockContainerNode.getProperty(LDP_IS_MEMBER_OF_RELATION)).thenReturn(mockRelationProperty);
when(mockContainerNode.getProperty(LDP_MEMBER_RESOURCE)).thenReturn(mockMembershipProperty);
when(mockMembershipProperty.getType()).thenReturn(URI);
when(mockMembershipProperty.getString()).thenReturn("some:resource");
final String property = "some:uri";
when(mockRelationProperty.getString()).thenReturn(property);
testObj = new LdpIsMemberOfRdfContext(mockResource, subjects);
final Model model = testObj.collect(toModel());
final Converter<FedoraResource, Resource> nodeSubjects = subjects.reverse();
assertTrue("Expected stream to contain triple",
model.contains(nodeSubjects.convert(mockResource),
createProperty(property),
createResource("some:resource")));
}
示例15: testIsMemberOfRelationForBinary
import com.google.common.base.Converter; //导入依赖的package包/类
@Test
public void testIsMemberOfRelationForBinary() throws RepositoryException {
when(mockContainer.hasType(LDP_DIRECT_CONTAINER)).thenReturn(true);
when(mockContainer.hasProperty(LDP_IS_MEMBER_OF_RELATION)).thenReturn(true);
when(mockContainer.hasProperty(LDP_MEMBER_RESOURCE)).thenReturn(true);
when(mockContainerNode.getProperty(LDP_IS_MEMBER_OF_RELATION)).thenReturn(mockRelationProperty);
when(mockContainerNode.getProperty(LDP_MEMBER_RESOURCE)).thenReturn(mockMembershipProperty);
when(mockMembershipProperty.getType()).thenReturn(REFERENCE);
when(mockMembershipProperty.getNode()).thenReturn(mockNode);
final String property = "some:uri";
when(mockRelationProperty.getString()).thenReturn(property);
testObj = new LdpIsMemberOfRdfContext(mockBinary, subjects);
final Model model = testObj.collect(toModel());
final Converter<FedoraResource, Resource> nodeSubjects = subjects.reverse();
assertTrue("Expected stream to contain triple",
model.contains(nodeSubjects.convert(mockBinary),
createProperty(property),
nodeToResource(subjects).convert(mockNode)));
}