Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure a generic CSV reader would be this simple to use (and to create, by the way).</p> <p>The first question that comes to my mind is: What if the CSV contains three columns: first an integer, then a string and finally a date? How would you use your generic CSV reader?</p> <p>Anyway, lets suppose you want to create a CSV reader where all columns are of the same type. As you said, you can't parametrize a class on a type "that accepts a <code>String</code> as constructor". Java just doesn't allow that. The solution using reflection is a good start. But what if your class doesn't take a <code>String</code> as parameter in one of its constructors?</p> <p>Here you can come with an alternative: a parser that would take your String and return an object of the correct type. Create a generic interface, and make some implementations for the type you want to crawl:</p> <pre><code>public interface Parser&lt;T&gt; { T parse(String value); } </code></pre> <p>And then, implement:</p> <pre><code>public class StringParser implements Parser&lt;String&gt; { public String parse(String value) { return value; } } </code></pre> <p>Then, you CSV reader can take a <code>Parser</code> as one of its parameters. Then, it can use this parser to convert each <code>String</code> into a Java object.</p> <p>With this solution, you get rid of the not-so-pretty reflection your where using. And you can convert to any type, you just have to implement a <code>Parser</code>.</p> <p>Your reader will look like this:</p> <pre><code>public CSVReader&lt;T&gt; { Parser&lt;T&gt; parser; List&lt;T&gt; getValues() { // ... } } </code></pre> <p>Now, back at the problem where a CSV file can have multiple types, just improve your reader a little. All you need is a list of parsers (one per column) instead of one that parse all columns.</p> <p>Hope that helps :-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload