package prefuse.data.parser; import java.util.Arrays; /** * Factory class that maintains a collection of parser instances and returns * the appropriate parser based on a history of samples presented to the * factory. The {@link #sample(String)} method takes a text string and tests * it against all available parsers, updating whether or not the parsers can * successfully parse the value. This method is used in a more automated * fashion by the {@link TypeInferencer} class. * * @author jeffrey heer * @see TypeInferencer */ public class ParserFactory implements Cloneable { private static final DataParser[] DEFAULT_PARSERS = new DataParser[] { new IntParser(), new LongParser(), new DoubleParser(), new FloatParser(), new BooleanParser(), new DateParser(), new TimeParser(), new DateTimeParser(), new IntArrayParser(), new StringParser() }; private static final ParserFactory DEFAULT_FACTORY = new ParserFactory(DEFAULT_PARSERS); private DataParser[] m_parsers; private boolean[] m_isCandidate; /** * Returns the default parser factory. The default factory tests for the * following data types (in the provided order of precedence): * int, long, double, float, boolean, Date, Time, DateTime, String. */ public static ParserFactory getDefaultFactory() { return DEFAULT_FACTORY; } /** * Constructor. Uses a default collection of parsers, testing for the * following data type in the followinf order of precedence: * int, long, double, float, boolean, Date, Time, DateTime, String. */ public ParserFactory() { this(DEFAULT_PARSERS); } /** * @see java.lang.Object#clone() */ public Object clone() { return new ParserFactory(m_parsers); } /** *

Constructor. Takes an array of parsers to test. After creating this * instance, sample data values can be passed in using the * sample() method, and this class will check the sample * against the parsers, computing which parsers can successfully parse the * sample. This process of elimination disregards inappropriate parsers. * After a series of samples, the getParser() * method can be used to retrieve the highest ranking candidate parser. *

* *

* If no parser can parse all samples, a null value will be returned by * getParser(). For this reason, it is recommended to always use a * StringParser as the last element of the input array, as it is guaranteed * to always parse successfully (by simply returning its input String). *

* *

* The ordering of parsers in the array is taken to be the desired order * of precendence of the parsers. For example, if both parser[0] and * parser[2] can parse all the available samples, parser[0] will be * returned. *

* @param parsers the input DataParsers to use. */ public ParserFactory(DataParser[] parsers) { // check integrity of input for ( int i=0; i