@Test public void testFromCollection() { CsvEngine csvEngine = new CsvEngine(); String generated = csvEngine.toString(Arrays.asList(Product.get())).replace("\r\n", "\n").trim(); assertEquals("Generated CSV is not the same", expected, generated); }
@Override public String toString(Object object) { if (object instanceof Csv) { return toCsv((Csv) object); } else if (object.getClass().isArray() && Csv.class.isAssignableFrom(object.getClass().getComponentType())) { return toCsv((Csv[]) object); } else if (Collection.class.isAssignableFrom(object.getClass())) { ArrayList<?> list = new ArrayList<>((Collection<?>) object); for (Object item : list) { if (!(item instanceof Csv)) { throw new RuntimeException("All objects in the supplied collection must implement " + Csv.class.getName()); } } return toCsv(list.toArray(new Csv[list.size()])); } throw new RuntimeException("Unexpected object type " + object.getClass().getName()); }
CsvEngine implements ContentTypeEngine { @Override public String toString(Object object) { if (object instanceof Csv) { return toCsv((Csv) object); } else if (object.getClass().isArray() && Csv.class.isAssignableFrom(object.getClass().getComponentType())) { return toCsv((Csv[]) object); } else if (Collection.class.isAssignableFrom(object.getClass())) { ArrayList<?> list = new ArrayList<>((Collection<?>) object); for (Object item : list) { if (!(item instanceof Csv)) { throw new RuntimeException("All objects in the supplied collection must implement " + Csv.class.getName()); } } return toCsv(list.toArray(new Csv[list.size()])); } throw new RuntimeException("Unexpected object type " + object.getClass().getName()); } }
CsvEngine implements ContentTypeEngine { @Override public String toString(Object object) { if (object instanceof Csv) { return toCsv((Csv) object); } else if (object.getClass().isArray() && Csv.class.isAssignableFrom(object.getClass().getComponentType())) { return toCsv((Csv[]) object); } else if (Collection.class.isAssignableFrom(object.getClass())) { ArrayList<?> list = new ArrayList<>((Collection<?>) object); for (Object item : list) { if (!(item instanceof Csv)) { throw new RuntimeException("All objects in the supplied collection must implement " + Csv.class.getName()); } } return toCsv(list.toArray(new Csv[list.size()])); } throw new RuntimeException("Unexpected object type " + object.getClass().getName()); }  }
CsvEngine implements ContentTypeEngine { @Override public String toString(Object object) { if (object instanceof Csv) { return toCsv((Csv) object); } else if (object.getClass().isArray() && Csv.class.isAssignableFrom(object.getClass().getComponentType())) { return toCsv((Csv[]) object); } else if (Collection.class.isAssignableFrom(object.getClass())) { ArrayList<?> list = new ArrayList<>((Collection<?>) object); for (Object item : list) { if (!(item instanceof Csv)) { throw new RuntimeException("All objects in the supplied collection must implement " + Csv.class.getName()); } } return toCsv(list.toArray(new Csv[list.size()])); } throw new RuntimeException("Unexpected object type " + object.getClass().getName()); }  void setCaseSensitiveFieldNames(boolean caseSensitiveFieldNames); void setDatePattern(String datePattern); void setDelimiter(char delimiter); void setEscapeCharacter(char escapeCharacter); void setQuoteCharacter(char quoteCharacter); void setQuoteMode(QuoteMode quoteMode); void setNullString(String nullString); void setRecordSeparator(String recordSeparator); CSVFormat getCSVFormat(); @Override void init(Application application); @Override String getContentType(); @Override String toString(Object object); String toCsv(Csv... records); @Override @SuppressWarnings("unchecked") T fromString(String content, Class<T> classOfT); String objectToString(Object object); Object objectFromString(String value, Class<?> objectClass); }
CsvEngine implements ContentTypeEngine { @Override public String toString(Object object) { if (object instanceof Csv) { return toCsv((Csv) object); } else if (object.getClass().isArray() && Csv.class.isAssignableFrom(object.getClass().getComponentType())) { return toCsv((Csv[]) object); } else if (Collection.class.isAssignableFrom(object.getClass())) { ArrayList<?> list = new ArrayList<>((Collection<?>) object); for (Object item : list) { if (!(item instanceof Csv)) { throw new RuntimeException("All objects in the supplied collection must implement " + Csv.class.getName()); } } return toCsv(list.toArray(new Csv[list.size()])); } throw new RuntimeException("Unexpected object type " + object.getClass().getName()); }  void setCaseSensitiveFieldNames(boolean caseSensitiveFieldNames); void setDatePattern(String datePattern); void setDelimiter(char delimiter); void setEscapeCharacter(char escapeCharacter); void setQuoteCharacter(char quoteCharacter); void setQuoteMode(QuoteMode quoteMode); void setNullString(String nullString); void setRecordSeparator(String recordSeparator); CSVFormat getCSVFormat(); @Override void init(Application application); @Override String getContentType(); @Override String toString(Object object); String toCsv(Csv... records); @Override @SuppressWarnings("unchecked") T fromString(String content, Class<T> classOfT); String objectToString(Object object); Object objectFromString(String value, Class<?> objectClass); final static String TEXT_CSV; }