KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > parser > TypeInferencer


1 package prefuse.data.parser;
2
3 import java.util.ArrayList JavaDoc;
4
5 /**
6  * Infers the data types for a table of data by testing each value
7  * of the data against a bank of parsers and eliminating candidate
8  * parsers that do not successfully parse the data. This class leverages
9  * the mechanisms of {@link ParserFactory}, but while that class only
10  * supports one data field at a time, TypeInferencer maintains a collection
11  * of ParserFactory instances to infer type for multiple data columns
12  * simultaneously.
13  *
14  * @author <a HREF="http://jheer.org">jeffrey heer</a>
15  * @see ParserFactory
16  */

17 public class TypeInferencer {
18
19     public ParserFactory m_template;
20     public ArrayList JavaDoc m_factories = new ArrayList JavaDoc();
21     
22     /**
23      * Create a new TypeInferencer using the default ParserFactory
24      * settings, thus the default parsers and parser ordering will be used.
25      */

26     public TypeInferencer() {
27         this(new ParserFactory());
28     }
29
30     /**
31      * Create a new TypeInferenced using the input ParserFactory as a
32      * template for the desired parsers to check and parser ordering
33      * to use.
34      * @param template the template ParserFactory to use for included
35      * parsers and their precedence.
36      */

37     public TypeInferencer(ParserFactory template) {
38         m_template = template;
39     }
40     
41     // ------------------------------------------------------------------------
42

43     private void rangeCheck(int column, boolean grow) {
44         if ( column < 0 || (!grow && column >= m_factories.size()))
45             throw new IndexOutOfBoundsException JavaDoc(
46                 "Index out of bounds: "+column);
47         
48         if ( column < m_factories.size() )
49             return;
50         
51         for ( int i=m_factories.size(); i<=column; ++i )
52             m_factories.add(m_template.clone());
53     }
54     
55     /**
56      * Sample the given text string for the given data column index.
57      * @param column the data column index of the sample
58      * @param value the text string sample
59      */

60     public void sample(int column, String JavaDoc value) {
61         rangeCheck(column, true);
62         ((ParserFactory)m_factories.get(column)).sample(value);
63     }
64     
65     /**
66      * Get the data type for the highest ranking candidate parser
67      * still in the running for the given column index.
68      * @param column the data column index
69      * @return the currently inferred type of that column
70      */

71     public Class JavaDoc getType(int column) {
72         return getParser(column).getType();
73     }
74     
75     /**
76      * Get the top-ranking candidate data parser for the given column index.
77      * @param column the data column index
78      * @return the data parser to use for that column
79      */

80     public DataParser getParser(int column) {
81         rangeCheck(column, false);
82         return ((ParserFactory)m_factories.get(column)).getParser();
83     }
84     
85 } // end of class TypeInferencer
86
Popular Tags