Example usage for com.google.common.base Converter convert

List of usage examples for com.google.common.base Converter convert

Introduction

In this page you can find the example usage for com.google.common.base Converter convert.

Prototype

@Nullable
public final B convert(@Nullable A a) 

Source Link

Document

Returns a representation of a as an instance of type B .

Usage

From source file:com.googlecode.blaisemath.sketch.SketchIO.java

/**
 * Converts given composite graphics to SVG-compatible object.
 * @param comp graphic to convert/*from w  ww  .  j a v a 2s. c o  m*/
 * @return SVG representation
 */
public static SVGRoot toSVG(GraphicComposite<Graphics2D> comp) {
    Converter<Graphic<Graphics2D>, SVGElement> conv = SVGElementGraphicConverter.getInstance().reverse();
    SVGRoot r = new SVGRoot();
    for (Graphic<Graphics2D> g : comp.getGraphics()) {
        r.addElement(conv.convert(g));
    }
    return r;
}

From source file:com.googlecode.blaisemath.sketch.SketchIO.java

/**
 * Create composite graphics from SVG representation.
 * @param svg the SVG representation//w  ww.j  a v a 2  s.  c  om
 * @return set of top-level graphics
 */
public static Iterable<Graphic<Graphics2D>> fromSVG(SVGRoot svg) {
    Converter<SVGElement, Graphic<Graphics2D>> conv = SVGElementGraphicConverter.getInstance();
    List<Graphic<Graphics2D>> res = Lists.newArrayList();
    for (SVGElement el : svg.getElements()) {
        res.add(conv.convert(el));
    }
    return res;
}

From source file:be.nbb.demetra.dotstat.DotStatAccessor.java

private static DbSeries getSeriesWithData(SdmxConnection conn, FlowRef flowRef, DbSetId ref)
        throws IOException {
    Converter<DbSetId, Key> converter = getConverter(conn.getDataStructure(flowRef), ref);

    Key seriesKey = converter.convert(ref);
    return new DbSeries(ref, DotStatUtil.getSeriesWithData(conn, flowRef, seriesKey));
}

From source file:be.nbb.demetra.dotstat.DotStatAccessor.java

private static List<DbSetId> getAllSeries(SdmxConnection conn, FlowRef flowRef, DbSetId ref)
        throws IOException {
    Converter<DbSetId, Key> converter = getConverter(conn.getDataStructure(flowRef), ref);

    Key colKey = converter.convert(ref);
    try (TsCursor<Key, IOException> cursor = DotStatUtil.getAllSeries(conn, flowRef, colKey)) {
        ImmutableList.Builder<DbSetId> result = ImmutableList.builder();
        while (cursor.nextSeries()) {
            result.add(converter.reverse().convert(cursor.getKey()));
        }//from ww  w  . j a  va2s .  c o  m
        return result.build();
    }
}

From source file:be.nbb.demetra.dotstat.DotStatAccessor.java

private static List<DbSeries> getAllSeriesWithData(SdmxConnection conn, FlowRef flowRef, DbSetId ref)
        throws IOException {
    Converter<DbSetId, Key> converter = getConverter(conn.getDataStructure(flowRef), ref);

    Key colKey = converter.convert(ref);
    try (TsCursor<Key, IOException> cursor = DotStatUtil.getAllSeriesWithData(conn, flowRef, colKey)) {
        ImmutableList.Builder<DbSeries> result = ImmutableList.builder();
        while (cursor.nextSeries()) {
            result.add(new DbSeries(converter.reverse().convert(cursor.getKey()), cursor.getData()));
        }//w  w w .  j a v a  2s.c  o  m
        return result.build();
    }
}

From source file:us.eharning.atomun.mnemonic.spi.electrum.v2.MnemonicDecoderSpiImpl.java

/**
 * Verify that the dictionary contains all of the words in the given mnemonic sequence.
 *
 * @param dictionary//from  w  w w .  jav a  2 s  .c om
 *         instance to check for the presence of all words.
 * @param mnemonicWordList
 *         sequence of mnemonic words to match up against a dictionary.
 *
 * @return true if dictionary contains all words in mnemonicWordList.
 */
private static boolean verifyDictionary(@Nonnull BidirectionalDictionary dictionary,
        @Nonnull List<String> mnemonicWordList) {
    Converter<String, Integer> reverseDictionary = dictionary.reverse();
    /* Due to inability for converters to return null as a valid response, need to catch thrown exception */
    try {
        for (String word : mnemonicWordList) {
            reverseDictionary.convert(word);
        }
    } catch (IllegalArgumentException ignored) {
        return false;
    }
    return true;
}

From source file:us.eharning.atomun.mnemonic.spi.electrum.v2.MnemonicUtility.java

/**
 * Utility method to determine if a given seed is of the "old" format.
 *
 * @param seed//from  w w  w.j a v  a2 s  .  co  m
 *         space-separated list of words to validate.
 *
 * @return true if the seed can be interpreted as a legacy format.
 */
private static boolean isOldSeed(CharSequence seed) {
    List<String> words = Splitter.on(WHITESPACE_MATCH).splitToList(seed);
    if (words.size() % 3 != 0) {
        /* Not a multiple of 3 words, not an old seed */
        return false;
    }
    Converter<String, Integer> reverse = LEGACY_DICTIONARY.reverse();
    try {
        for (String word : words) {
            reverse.convert(word);
        }
    } catch (IllegalArgumentException ignored) {
        return false;
    }
    /* All words were found and there were a multiple of 3 */
    return true;
}

From source file:be.nbb.demetra.dotstat.DotStatAccessor.java

private static List<String> getChildren(SdmxConnection conn, FlowRef flowRef, DbSetId ref) throws IOException {
    Converter<DbSetId, Key> converter = getConverter(conn.getDataStructure(flowRef), ref);
    int dimensionPosition = dimensionById(conn.getDataStructure(flowRef)).get(ref.getColumn(ref.getLevel()))
            .getPosition();/*w w w  . j ava 2 s .  c o m*/
    return DotStatUtil.getChildren(conn, flowRef, converter.convert(ref), dimensionPosition);
}

From source file:us.eharning.atomun.mnemonic.spi.bip0039.BIP0039MnemonicUnitSpi.java

/**
 * Convert a sequence of mnemonic word into a bit array for validation and usage.
 *
 * @param dictionary/*from  w ww . jav  a 2 s  .  c om*/
 *         instance to check for the presence of all words.
 * @param mnemonicWordList
 *         sequence of mnemonic words to map against a dictionary for bit values.
 *
 * @return sequence of bytes based on word list.
 */
@Nonnull
private static byte[] mnemonicToBytes(@Nonnull BidirectionalDictionary dictionary,
        @Nonnull List<String> mnemonicWordList) {
    /* Each word represents 11 bits of entropy (2^11 => 2048 words) */
    int mnemonicSentenceBitCount = mnemonicWordList.size() * 11;
    int mnemonicSentenceByteCount = (mnemonicSentenceBitCount + 7) / 8;

    byte[] mnemonicSentenceBytes = new byte[mnemonicSentenceByteCount];
    BitWriter bitWriter = new ByteArrayBitWriter(mnemonicSentenceBytes);
    Converter<String, Integer> reverseConverter = dictionary.reverse();
    for (String word : mnemonicWordList) {
        /* Find the word index in the wordList. */
        /* Warning suppressed due to word guaranteed non-null */
        //noinspection ConstantConditions
        int index = reverseConverter.convert(word);

        bitWriter.write(index, 11);
    }
    bitWriter.flush();
    return mnemonicSentenceBytes;
}

From source file:us.eharning.atomun.mnemonic.spi.electrum.legacy.LegacyElectrumMnemonicUtility.java

/**
 * Decode a space-delimited sequence of mnemonic words.
 *
 * @param mnemonicSequence/*from   w w  w. java  2s .c o  m*/
 *         space-delimited sequence of mnemonic words to toEntropy.
 *
 * @return encoded value.
 */
@Nonnull
static byte[] toEntropy(@Nonnull CharSequence mnemonicSequence) {
    String[] mnemonicWords = Iterables.toArray(WORD_SPLITTER.split(mnemonicSequence), String.class);
    byte[] entropy = new byte[mnemonicWords.length * 4 / 3];
    int entropyIndex = 0;
    Converter<String, Integer> reverseDictionary = DICTIONARY.reverse();
    if (mnemonicWords.length % 3 != 0) {
        throw new IllegalArgumentException("Mnemonic sequence is not a multiple of 3");
    }
    for (int i = 0; i < mnemonicWords.length; i += 3) {
        String word1 = mnemonicWords[i].toLowerCase();
        String word2 = mnemonicWords[i + 1].toLowerCase();
        String word3 = mnemonicWords[i + 2].toLowerCase();
        Integer w1 = reverseDictionary.convert(word1);
        Integer w2 = reverseDictionary.convert(word2);
        Integer w3 = reverseDictionary.convert(word3);
        if (null == w1 || null == w2 || null == w3) {
            throw new IllegalArgumentException("Unknown mnemonic word used");
        }
        int subValue = w1 + N * mn_mod(w2 - w1, N) + N * N * mn_mod(w3 - w2, N);
        /* Convert to 4 bytes */
        putInteger(entropy, entropyIndex, subValue);
        entropyIndex += 4;
    }
    return entropy;
}