org.supercsv.cellprocessor.Optional Java Examples
The following examples show how to use
org.supercsv.cellprocessor.Optional.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Writing.java From super-csv with Apache License 2.0 | 6 votes |
/** * Sets up the processors used for the examples. There are 10 CSV columns, so 10 processors are defined. All values * are converted to Strings before writing (there's no need to convert them), and null values will be written as * empty columns (no need to convert them to ""). * * @return the cell processors */ private static CellProcessor[] getProcessors() { final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), // customerNo (must be unique) new NotNull(), // firstName new NotNull(), // lastName new FmtDate("dd/MM/yyyy"), // birthDate new NotNull(), // mailingAddress new Optional(new FmtBool("Y", "N")), // married new Optional(), // numberOfKids new NotNull(), // favouriteQuote new NotNull(), // email new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints }; return processors; }
Example #2
Source File: ReadWriteCSV.java From AIDR with GNU Affero General Public License v3.0 | 6 votes |
private static CellProcessor[] getProcessors4ClassifiedTweetIDSCCSV() { final CellProcessor[] processors = new CellProcessor[]{ //new UniqueHashCode(), // tweetID (must be unique) new NotNull(), // tweetID (must be unique): sometimes CAN be null! new NotNull(), // crisis name new Optional(), // attribute name new Optional(), // attribute code new Optional(), // label name new Optional(), // label description new Optional(), // label code new Optional(), // confidence new Optional(), // humanLabeled }; return processors; }
Example #3
Source File: ReadWriteCSV.java From AIDR with GNU Affero General Public License v3.0 | 6 votes |
private static CellProcessor[] getProcessors4ClassifiedCCSV() { final CellProcessor[] processors = new CellProcessor[]{ //new UniqueHashCode(), // tweetID (must be unique) //new NotNull(), // tweetID (must be unique) new Optional(), // tweetID - data shows that sometimes tweetID CAN be null! new Optional(), // message new Optional(), // userID new Optional(), // userName new Optional(), // userURL new Optional(), // createdAt new NotNull(), // crisis name new Optional(), // attribute name new Optional(), // attribute code new Optional(), // label name new Optional(), // label description new Optional(), // label code new Optional(), // confidence new Optional() // humanLabeled }; return processors; }
Example #4
Source File: ReadWriteCSV.java From AIDR with GNU Affero General Public License v3.0 | 6 votes |
private static CellProcessor[] getCollectorTweetsProcessors() { final CellProcessor[] processors = new CellProcessor[]{ //new UniqueHashCode(), // tweetID (must be unique) //new NotNull(), // tweetID (must be unique) new Optional(), // data shows that sometimes tweetID CAN be null! new Optional(), // message //new FmtDate("dd/MM/yyyy"), // birthDate new Optional(), // userID new Optional(), // userName //new Optional(new FmtBool("Y", "N")), // isRT new Optional(), // userURL new Optional(), // createdAt new Optional() // tweet permanent URL }; return processors; }
Example #5
Source File: SuperCsvBOMTest.java From super-csv with Apache License 2.0 | 6 votes |
public void ReadTestCSVFile(Reader reader) throws IOException { ICsvBeanReader beanReader = new CsvBeanReader(reader, CsvPreference.STANDARD_PREFERENCE); final String[] header = beanReader.getHeader(true); assertEquals("customerNo", header[0]); CustomerBean customer = null; final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+"; // just an example, not very robust! StrRegEx.registerMessage(emailRegex, "must be a valid email address"); final CellProcessor[] processors = new CellProcessor[]{new UniqueHashCode(), // customerNo (must be unique) new NotNull(), // firstName new NotNull(), // lastName new ParseDate("dd/MM/yyyy"), // birthDate new ParseSqlTime("HH:mm:ss"), // birthTime new NotNull(), // mailingAddress new Optional(new ParseBool()), // married new Optional(new ParseInt()), // numberOfKids new NotNull(), // favouriteQuote new StrRegEx(emailRegex), // email new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints }; customer = beanReader.read(CustomerBean.class, header, processors); assertEquals("1", customer.getCustomerNo()); assertEquals("John", customer.getFirstName()); assertEquals("[email protected]", customer.getEmail()); assertEquals(0, customer.getLoyaltyPoints()); beanReader.close(); }
Example #6
Source File: Reading.java From super-csv with Apache License 2.0 | 6 votes |
/** * Sets up the processors used for the examples. There are 10 CSV columns, so 10 processors are defined. Empty * columns are read as null (hence the NotNull() for mandatory columns). * * @return the cell processors */ private static CellProcessor[] getProcessors() { final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+"; // just an example, not very robust! StrRegEx.registerMessage(emailRegex, "must be a valid email address"); final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), // customerNo (must be unique) new NotNull(), // firstName new NotNull(), // lastName new ParseDate("dd/MM/yyyy"), // birthDate new ParseSqlTime("HH:mm:ss"), new NotNull(), // mailingAddress new Optional(new ParseBool()), // married new Optional(new ParseInt()), // numberOfKids new NotNull(), // favouriteQuote new StrRegEx(emailRegex), // email new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints }; return processors; }
Example #7
Source File: CsvFormatter.java From attic-apex-malhar with Apache License 2.0 | 6 votes |
/** * Returns array of cellprocessors, one for each field */ private CellProcessor[] getProcessor(List<Field> fields) { CellProcessor[] processor = new CellProcessor[fields.size()]; int fieldCount = 0; for (Field field : fields) { if (field.getType() == FieldType.DATE) { String format = field.getConstraints().get(DelimitedSchema.DATE_FORMAT) == null ? null : (String)field.getConstraints().get(DelimitedSchema.DATE_FORMAT); processor[fieldCount++] = new Optional(new FmtDate(format == null ? "dd/MM/yyyy" : format)); } else { processor[fieldCount++] = new Optional(); } } return processor; }
Example #8
Source File: MarketLogReader.java From jeveassets with GNU General Public License v2.0 | 6 votes |
private static CellProcessor[] getProcessors() { return new CellProcessor[]{ new ParseDouble(), // price new ParseDouble(), // volRemaining new ParseInt(), // typeID new ParseInt(), // range new ParseLong(), // orderID new ParseInt(), // volEntered new ParseInt(), // minVolume new ParseBool(), // bid new ParseDate(), // issueDate new ParseInt(), // duration new ParseLong(), // stationID new ParseLong(), // regionID new ParseLong(), // solarSystemID new ParseInt(), // jumps new Optional() }; }
Example #9
Source File: AirportCsvDeserializer.java From Showcase with Apache License 2.0 | 6 votes |
private CellProcessor[] getCellProcessors() { return new CellProcessor[]{ //@formatter:off null, // airport ID new Optional(), // name new Optional(), // city new Optional(), // country new Optional(new ParseAirportCode()), // iataCode new Optional(new ParseAirportCode()), // icaoCode new Optional(new ParseDouble()), // latitude new Optional(new ParseDouble()), // longitude null, // altitude null, // Timezone null, // DST null, // Tz database time zone null, // Type null, // Source //@formatter:on }; }
Example #10
Source File: CellProcessorBuilder.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
/** * Get Optional cellprocessor which means field is not mandatory. * * @param cellProcessor * next processor in the chain. * @return CellProcessor */ private static CellProcessor addOptional(CellProcessor cellProcessor) { if (cellProcessor == null) { return new Optional(); } return new Optional(cellProcessor); }
Example #11
Source File: ParseDateTest.java From robe with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void constructorsTest() throws Exception { new ParseDate("yyyyMMddHHmm"); new ParseDate("yyyyMMddHHmm", true); new ParseDate("yyyyMMddHHmm", true, Locale.getDefault()); new ParseDate("yyyyMMddHHmm", new Optional()); new ParseDate("yyyyMMddHHmm", true, new Optional()); new ParseDate("yyyyMMddHHmm", true, Locale.getDefault(), new Optional()); //No exception means success. }
Example #12
Source File: ParseBigDecimalTest.java From robe with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void constructorTests() throws Exception { new ParseBigDecimal(DecimalFormatSymbols.getInstance()); new ParseBigDecimal(new Optional()); new ParseBigDecimal(DecimalFormatSymbols.getInstance(), new Optional()); //No exception means success. }
Example #13
Source File: AbstractCsvParser.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
public void initialise(String[] properties, CellProcessor[] processors) { for (int i = 0; i < getFields().size(); i++) { FIELD_TYPE type = getFields().get(i).type; properties[i] = getFields().get(i).name; if (type == FIELD_TYPE.DOUBLE) { processors[i] = new Optional(new ParseDouble()); } else if (type == FIELD_TYPE.INTEGER) { processors[i] = new Optional(new ParseInt()); } else if (type == FIELD_TYPE.FLOAT) { processors[i] = new Optional(new ParseDouble()); } else if (type == FIELD_TYPE.LONG) { processors[i] = new Optional(new ParseLong()); } else if (type == FIELD_TYPE.SHORT) { processors[i] = new Optional(new ParseInt()); } else if (type == FIELD_TYPE.STRING) { processors[i] = new Optional(); } else if (type == FIELD_TYPE.CHARACTER) { processors[i] = new Optional(new ParseChar()); } else if (type == FIELD_TYPE.BOOLEAN) { processors[i] = new Optional(new ParseChar()); } else if (type == FIELD_TYPE.DATE) { processors[i] = new Optional(new ParseDate("dd/MM/yyyy")); } } }
Example #14
Source File: AirlineCsvDeserializer.java From Showcase with Apache License 2.0 | 5 votes |
private CellProcessor[] getCellProcessors() { return new CellProcessor[]{ //@formatter:off null, //airline ID new Optional(), //name new Optional(new ParseAlias()), //alias new Optional(), //iataCode new Optional(), //icaoCode new Optional(), //callsign new Optional(), //country null, //active //@formatter:on }; }
Example #15
Source File: Reading.java From super-csv with Apache License 2.0 | 5 votes |
/** * An example of reading using CsvDozerBeanReader. */ private static void readWithCsvDozerBeanReader() throws Exception { final CellProcessor[] processors = new CellProcessor[] { new Optional(new ParseInt()), // age new ParseBool(), // consent new ParseInt(), // questionNo 1 new Optional(), // answer 1 new ParseInt(), // questionNo 2 new Optional(), // answer 2 new ParseInt(), // questionNo 3 new Optional() // answer 3 }; ICsvDozerBeanReader beanReader = null; try { beanReader = new CsvDozerBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE); beanReader.getHeader(true); // ignore the header beanReader.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING); SurveyResponse surveyResponse; while( (surveyResponse = beanReader.read(SurveyResponse.class, processors)) != null ) { System.out.println(String.format("lineNo=%s, rowNo=%s, surveyResponse=%s", beanReader.getLineNumber(), beanReader.getRowNumber(), surveyResponse)); } } finally { if( beanReader != null ) { beanReader.close(); } } }
Example #16
Source File: Reading.java From super-csv with Apache License 2.0 | 5 votes |
/** * An example of partial reading using CsvDozerBeanReader. */ private static void partialReadWithCsvDozerBeanReader() throws Exception { // ignore age, and question/answer 3 final String[] partialFieldMapping = new String[] { null, "consentGiven", "answers[0].questionNo", "answers[0].answer", "answers[1].questionNo", "answers[1].answer", null, null }; // set processors for ignored columns to null for efficiency (could have used full array if we wanted them to execute anyway) final CellProcessor[] processors = new CellProcessor[] { null, new ParseBool(), new ParseInt(), new Optional(), new ParseInt(), new Optional(), null, null }; ICsvDozerBeanReader beanReader = null; try { beanReader = new CsvDozerBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE); beanReader.getHeader(true); // ignore the header beanReader.configureBeanMapping(SurveyResponse.class, partialFieldMapping); SurveyResponse surveyResponse; while( (surveyResponse = beanReader.read(SurveyResponse.class, processors)) != null ) { System.out.println(String.format("lineNo=%s, rowNo=%s, surveyResponse=%s", beanReader.getLineNumber(), beanReader.getRowNumber(), surveyResponse)); } } finally { if( beanReader != null ) { beanReader.close(); } } }
Example #17
Source File: SuperCSVParserExample.java From journaldev with MIT License | 5 votes |
private static CellProcessor[] getProcessors() { final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), // ID (must be unique) new NotNull(), // Name new Optional(), // Role new NotNull() // Salary }; return processors; }
Example #18
Source File: ReadWriteCSV.java From AIDR with GNU Affero General Public License v3.0 | 5 votes |
private static CellProcessor[] getClassifiedTweetVariableProcessors(final int count) { CellProcessor[] processors = new CellProcessor[count]; for (int i = 0;i < count;i++) { processors[i] = new Optional(); } return processors; }
Example #19
Source File: ConnectionCsvDeserializer.java From Showcase with Apache License 2.0 | 5 votes |
private CellProcessor[] getCellProcessors() { return new CellProcessor[]{ //@formatter:off new Optional(new ParseAirlinesIataCode()), //airline null, //airline ID new Optional(new ParseAirportIataCode()), //source airport null, //source airport ID new Optional(new ParseAirportIataCode()), //destination airport null, //destination airport ID new Optional(new ParseCodeShare()), //codeShare new Optional(new ParseInt()), //stops null, //equipment //@formatter:on }; }
Example #20
Source File: Reading.java From super-csv with Apache License 2.0 | 4 votes |
/** * An example of reading using CsvDozerBeanReader that uses indexed mapping and a cell processor * to read into a List of Answer beans (this requires a hint). */ private static void readWithCsvDozerBeanReaderUsingIndexMappingAndHints() throws Exception { // simple cell processor that creates an Answer with a value final CellProcessor parseAnswer = new CellProcessorAdaptor() { public Object execute(Object value, CsvContext context) { return new Answer(null, (String) value); } }; final CellProcessor[] processors = new CellProcessor[] { new Optional(new ParseInt()), // age null, // consent null, // questionNo 1 new Optional(parseAnswer), // answer 1 null, // questionNo 2 new Optional(parseAnswer), // answer 2 null, // questionNo 3 new Optional(parseAnswer) // answer 3 }; // no deep mapping (answers[0].answer) required as we're using a cell processor to create the bean final String[] fieldMapping = {"age", null, null, "answers[0]", null, "answers[1]", null, "answers[2]"}; // the indexed mappings need a hint for Dozer to work final Class<?>[] hintTypes = {null, null, null, Answer.class, null, Answer.class, null, Answer.class}; ICsvDozerBeanReader beanReader = null; try { beanReader = new CsvDozerBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE); beanReader.getHeader(true); // ignore the header beanReader.configureBeanMapping(SurveyResponse.class, fieldMapping, hintTypes); SurveyResponse surveyResponse; while( (surveyResponse = beanReader.read(SurveyResponse.class, processors)) != null ) { System.out.println(String.format("lineNo=%s, rowNo=%s, surveyResponse=%s", beanReader.getLineNumber(), beanReader.getRowNumber(), surveyResponse)); } } finally { if( beanReader != null ) { beanReader.close(); } } }
Example #21
Source File: Writing.java From super-csv with Apache License 2.0 | 4 votes |
/** * An example of writing using CsvDozerBeanWriter. */ private static void writeWithDozerCsvBeanWriter() throws Exception { final CellProcessor[] processors = new CellProcessor[] { new Token(0, null), // age new FmtBool("Y", "N"), // consent new NotNull(), // questionNo 1 new Optional(), // answer 1 new NotNull(), // questionNo 2 new Optional(), // answer 2 new NotNull(), // questionNo 3 new Optional() }; // answer 3 // create the survey responses to write SurveyResponse response1 = new SurveyResponse(18, true, Arrays.asList(new Answer(1, "Twelve"), new Answer(2, "Albert Einstein"), new Answer(3, "Big Bang Theory"))); SurveyResponse response2 = new SurveyResponse(0, true, Arrays.asList(new Answer(1, "Thirteen"), new Answer(2, "Nikola Tesla"), new Answer(3, "Stargate"))); SurveyResponse response3 = new SurveyResponse(42, false, Arrays.asList(new Answer(1, null), new Answer(2, "Carl Sagan"), new Answer(3, "Star Wars"))); final List<SurveyResponse> surveyResponses = Arrays.asList(response1, response2, response3); ICsvDozerBeanWriter beanWriter = null; try { beanWriter = new CsvDozerBeanWriter(new FileWriter("target/writeWithCsvDozerBeanWriter.csv"), CsvPreference.STANDARD_PREFERENCE); // configure the mapping from the fields to the CSV columns beanWriter.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING); // write the header beanWriter.writeHeader("age", "consentGiven", "questionNo1", "answer1", "questionNo2", "answer2", "questionNo3", "answer3"); // write the beans for( final SurveyResponse surveyResponse : surveyResponses ) { beanWriter.write(surveyResponse, processors); } } finally { if( beanWriter != null ) { beanWriter.close(); } } }
Example #22
Source File: Writing.java From super-csv with Apache License 2.0 | 4 votes |
/** * An example of appending writing using CsvDozerBeanWriter */ public static void appendingWriteWithCsvDozerBeanWriter() throws Exception { final CellProcessor[] processors = new CellProcessor[] { new Token(0, null), // age new FmtBool("Y", "N"), // consent new NotNull(), // questionNo 1 new Optional(), // answer 1 new NotNull(), // questionNo 2 new Optional(), // answer 2 new NotNull(), // questionNo 3 new Optional() }; // answer 3 // create the survey responses to write SurveyResponse response1 = new SurveyResponse(18, true, Arrays.asList(new Answer(1, "Twelve"), new Answer(2, "Albert Einstein"), new Answer(3, "Big Bang Theory"))); SurveyResponse response2 = new SurveyResponse(0, true, Arrays.asList(new Answer(1, "Thirteen"), new Answer(2, "Nikola Tesla"), new Answer(3, "Stargate"))); SurveyResponse response3 = new SurveyResponse(42, false, Arrays.asList(new Answer(1, null), new Answer(2, "Carl Sagan"), new Answer(3, "Star Wars"))); ICsvDozerBeanWriter beanWriter = null; try { beanWriter = new CsvDozerBeanWriter(new FileWriter("target/appendingWriteWithCsvDozerBeanWriter.csv"), CsvPreference.STANDARD_PREFERENCE); // configure the mapping from the fields to the CSV columns beanWriter.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING); // write the header beanWriter.writeHeader("age", "consentGiven", "questionNo1", "answer1", "questionNo2", "answer2", "questionNo3", "answer3"); // write the beans beanWriter.write(response1, processors); } finally { if( beanWriter != null ) { beanWriter.close(); } } // appending write try { beanWriter = new CsvDozerBeanWriter( new FileWriter("target/appendingWriteWithCsvDozerBeanWriter.csv", true), CsvPreference.STANDARD_PREFERENCE); beanWriter.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING); beanWriter.write(response2, processors); beanWriter.write(response3, processors); } finally { if( beanWriter != null ){ beanWriter.close(); } } }
Example #23
Source File: CsvDozerBeanReaderTest.java From super-csv with Apache License 2.0 | 4 votes |
/** * Tests mapping columns to indexed list elements (with no deep mapping). Dozer requires a hint in this situation. */ @Test public void testReadToListElement() throws IOException { final String csv = "age,answer1,answer2,answer3\n" + "23,Nikola Tesla,\"\"\"A brief history of time\"\" by Steven Hawking\",Theoretical physicist\n" + ",Genghis Kahn,\"\"\"Monsoon\"\" by Wilbur Smith\",\n" + "44,,,\"I hate surveys, thanks for wasting my time!\""; reader = new StringReader(csv); beanReader = new CsvDozerBeanReader(reader, PREFS); beanReader.getHeader(true); // skip header final String[] fieldMapping = new String[] { "age", "answers[0]", "answers[1]", "answers[2]" }; final Class<?>[] hintTypes = new Class<?>[] { null, Answer.class, Answer.class, Answer.class }; beanReader.configureBeanMapping(SurveyResponse.class, fieldMapping, hintTypes); final CellProcessor parseAnswer = new CellProcessor() { public Object execute(Object value, CsvContext context) { return value == null ? null : new Answer(0, (String) value); } }; final CellProcessor[] processors = new CellProcessor[] { new Optional(new ParseInt()), parseAnswer, parseAnswer, parseAnswer }; SurveyResponse response1 = beanReader.read(SurveyResponse.class, processors); assertEquals(23, response1.getAge()); assertEquals(3, response1.getAnswers().size()); assertEquals("Nikola Tesla", response1.getAnswers().get(0).getAnswer()); assertEquals("\"A brief history of time\" by Steven Hawking", response1.getAnswers().get(1).getAnswer()); assertEquals("Theoretical physicist", response1.getAnswers().get(2).getAnswer()); SurveyResponse response2 = beanReader.read(SurveyResponse.class, processors); assertEquals(0, response2.getAge()); assertEquals(3, response2.getAnswers().size()); assertEquals("Genghis Kahn", response2.getAnswers().get(0).getAnswer()); assertEquals("\"Monsoon\" by Wilbur Smith", response2.getAnswers().get(1).getAnswer()); assertNull(response2.getAnswers().get(2)); SurveyResponse response3 = beanReader.read(SurveyResponse.class, processors); assertEquals(44, response3.getAge()); assertEquals(3, response3.getAnswers().size()); assertNull(response3.getAnswers().get(0)); assertNull(response3.getAnswers().get(1)); assertEquals("I hate surveys, thanks for wasting my time!", response3.getAnswers().get(2).getAnswer()); assertNull(beanReader.read(SurveyResponse.class, processors)); }