Skip to content

Parsers

jochenwierum edited this page Jul 9, 2012 · 1 revision

Parsers

A parser is a small version of a TypeAdapter. While TypeAdapters can parse strings into objects, compare them and convert them back to strings, parsers are only responsible for converting strings into objects.

Here is one example from FitGoodies which converts a String into a BigDecimal:

package de.cologneintelligence.fitgoodies.parsers;

import java.math.BigDecimal;

public class BigDecimalParser implements Parser<BigDecimal> {
        @Override
        public final BigDecimal parse(final String s, final String parameter) {
                return new BigDecimal(s);
        }

        @Override
        public final Class<BigDecimal> getType() {
                return BigDecimal.class;
        }

}

Parsers can be registered with:

de.cologneintelligence.fitgoodies.parsers.ParserHelper.instance().registerParser(new BigDecimalParser());

Parsers can also be registered via HTML using an appropriate SetupFixture. As soon as a parser is registered, it is automatically used if FitGoodies must convert a string into a corresponding object.

Clone this wiki locally