Guava Converter - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Guava Converter class.

This code example shows how to use the following methods:convert
    new NullPointerTester().testAllPublicStaticMethods(Longs.class);
  } 
 
  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"));
  } 
 
  public void testStringConverter_convertError() { 
    try { 
      Longs.stringConverter().convert("notanumber");
      fail();
    } catch (NumberFormatException expected) {
5
CodeRank
This code example shows how to use the following methods:convert
    new NullPointerTester().testAllPublicStaticMethods(Ints.class);
  } 
 
  public void testStringConverter_convert() { 
    Converter<String, Integer> converter = Ints.stringConverter();
    assertEquals((Integer) 1, converter.convert("1"));
    assertEquals((Integer) 0, converter.convert("0"));
    assertEquals((Integer) (-1), converter.convert("-1"));
    assertEquals((Integer) 255, converter.convert("0xff"));
    assertEquals((Integer) 255, converter.convert("0xFF"));
    assertEquals((Integer) (-255), converter.convert("-0xFF"));
    assertEquals((Integer) 255, converter.convert("#0000FF"));
    assertEquals((Integer) 438, converter.convert("0666"));
  } 
 
  public void testStringConverter_convertError() { 
    try { 
      Ints.stringConverter().convert("notanumber");
      fail();
    } catch (NumberFormatException expected) {
5
CodeRank
This code example shows how to use the following methods:convert
    new NullPointerTester().testAllPublicStaticMethods(Shorts.class);
  } 
 
  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"));
  } 
 
  public void testStringConverter_convertError() { 
    try { 
      Shorts.stringConverter().convert("notanumber");
      fail();
    } catch (NumberFormatException expected) {
5
CodeRank
This code example shows how to use the following methods:convert
    new NullPointerTester().testAllPublicStaticMethods(Doubles.class);
  } 
 
  public void testStringConverter_convert() { 
    Converter<String, Double> converter = Doubles.stringConverter();
    assertEquals((Double) 1.0, converter.convert("1.0"));
    assertEquals((Double) 0.0, converter.convert("0.0"));
    assertEquals((Double) (-1.0), converter.convert("-1.0"));
    assertEquals((Double) 1.0, converter.convert("1"));
    assertEquals((Double) 0.0, converter.convert("0"));
    assertEquals((Double) (-1.0), converter.convert("-1"));
    assertEquals((Double) 1e6, converter.convert("1e6"));
    assertEquals((Double) 1e-6, converter.convert("1e-6"));
  } 
 
  public void testStringConverter_convertError() { 
    try { 
      Doubles.stringConverter().convert("notanumber");
      fail();
    } catch (NumberFormatException expected) {
4
CodeRank
This code example shows how to use the following methods:convert
  } 
 
  @GwtIncompatible("Float.toString returns different value in GWT.") 
  public void testStringConverter_convert() { 
    Converter<String, Float> converter = Floats.stringConverter();
    assertEquals((Float) 1.0f, converter.convert("1.0"));
    assertEquals((Float) 0.0f, converter.convert("0.0"));
    assertEquals((Float) (-1.0f), converter.convert("-1.0"));
    assertEquals((Float) 1.0f, converter.convert("1"));
    assertEquals((Float) 0.0f, converter.convert("0"));
    assertEquals((Float) (-1.0f), converter.convert("-1"));
    assertEquals((Float) 1e6f, converter.convert("1e6"));
    assertEquals((Float) 1e-6f, converter.convert("1e-6"));
  } 
 
  public void testStringConverter_convertError() { 
    try { 
      Floats.stringConverter().convert("notanumber");
      fail();
    } catch (NumberFormatException expected) {
5
CodeRank
This code example shows how to use the following methods:andThen, reverse
  public EnumAdapter( 
    Class<E> _enumClass, 
    Converter<String,E> c) {
    super(); 
    this.des = toUpper.andThen(c);
    this.ser = c.reverse().andThen(toLower);
  } 
     
  /** 
   * Method serialize. 
   * @param src E 
   * @param typeOfSrc Type 
   * @param context JsonSerializationContext 
   * @return JsonElement 
   */ 
  public JsonElement serialize(
    E src, 
    Type typeOfSrc,
    JsonSerializationContext context) {
    return context.serialize(ser.convert(src));
4
CodeRank
This code example shows how to use the following methods:reverse
     * Get a converter that can transform a Node to a Resource 
     * @param c 
     * @return 
     */ 
    public static Converter<Node, Resource> nodeToResource(final Converter<Resource, FedoraResource> c) { 
        return nodeConverter.andThen(c.reverse()); 
    } 
 
    @Override 
    protected FedoraResource doForward(final Node node) { 
 
        final FedoraResource fedoraResource; 
 
        if (NonRdfSourceDescriptionImpl.hasMixin(node)) { 
            fedoraResource = new NonRdfSourceDescriptionImpl(node); 
        } else if (FedoraBinaryImpl.hasMixin(node)) { 
            fedoraResource = new FedoraBinaryImpl(node); 
        } else if (TombstoneImpl.hasMixin(node)) { 
            fedoraResource = new TombstoneImpl(node); 
        } else { 
4
CodeRank
This code example shows how to use the following methods:convert
        if (absPath.startsWith("/")) {
            relativePath = absPath.substring(1);
        } else { 
            relativePath = absPath;
        } 
        return createResource(RESOURCE_NAMESPACE + reverse.convert(relativePath));
    } 
 
    @Override 
    public String asString(final Resource subject) {
        if (!inDomain(subject)) {
            return null; 
        } 
 
        final String path = subject.getURI().substring(RESOURCE_NAMESPACE.length() - 1);
 
        final String absPath = forward.convert(path);
 
        if (absPath.isEmpty()) {
            return "/"; 
3
CodeRank
This code example shows how to use the following methods:convert
 
            if (metadata) {
                path = replaceOnce(path, "/" + FCR_METADATA, EMPTY);
            } 
 
            path = forward.convert(path);
 
            if (path == null) {
                return null; 
            } 
 
            try { 
                path = URLDecoder.decode(path, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                LOGGER.debug("Unable to URL-decode path " + e + " as UTF-8", e);
            } 
 
            if (path.isEmpty()) {
                return "/"; 
            } 
2
CodeRank
See Code Examples for Guava Converter Methods: