org.supercsv.cellprocessor.ParseInt Java Examples

The following examples show how to use org.supercsv.cellprocessor.ParseInt. 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: CacheLoader.java    From geode-demo-application with Apache License 2.0 6 votes vote down vote up
public void loadData() {
	System.out.println("Loading the Data");
	// load the current transactions
	String[] nameMapping = new String[] { "customerId", "productId",
			"quantity", "retailPrice", "id", "markUp", "orderStatus" };
	CellProcessor[] processors = new CellProcessor[] { new NotNull(),// customerId
			new NotNull(),// productId
			new ParseInt(),// quantity
			new ParseDouble(),// retailsPrice
			new NotNull(),// transactionId
			new ParseDouble(),// markUp
			new NotNull() // order status
	};
	loadCurrentTransactions("current_transactions.csv", nameMapping,
			processors);
	System.out
			.println("*************************************************************");
	System.out
			.println("********************PLAYING TRANSACTIONS*********************");
	System.out
			.println("*************************************************************");
	start();
}
 
Example #2
Source File: MarketLogReader.java    From jeveassets with GNU General Public License v2.0 6 votes vote down vote up
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 #3
Source File: Reading.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #4
Source File: SuperCsvBOMTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
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 #5
Source File: CsvTest.java    From haxademic with MIT License 6 votes vote down vote up
/**
 * Sets up the CSV processors for loading and saving data
 * 
 * @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(), 	// score id (must be unique)
			new NotNull(), 			// initials
			new NotNull(), 			// emails
			new ParseInt(), 		// completeTime
			new ParseInt() 			// regionId
	};

	return processors;
}
 
Example #6
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
@Test
public void testConvertsToBasicObjects() throws Exception {
	String csv = "Connor|John|16|1999-07-12|6" + decimalFormatSymbols.getDecimalSeparator() + "65\r\n";
	String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
	CellProcessor[] processors = { new NotNull(), new NotNull(), new ParseInt(), new ParseDate("yyyy-MM-dd"),
		new ParseBigDecimal(decimalFormatSymbols) };
	
	CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
	CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), customPreference);
	FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
	
	Assert.assertNotNull(character);
	Assert.assertEquals("John", character.getFirstName());
	Assert.assertEquals("Connor", character.getLastName());
	Assert.assertEquals(16, character.getAge());
	Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd").parse("1999-07-12"), character.getBirthDate());
	Assert.assertEquals(new BigDecimal(6.65, new MathContext(3)), character.getSavings());
}
 
Example #7
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
@Test
public void testConverterSupport() throws Exception {
	String csv = "Connor|John|16|1999-07-12|6" + decimalFormatSymbols.getDecimalSeparator() + "65\r\n";
	String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
	CellProcessor[] processors = { new NotNull(), new NotNull(), new ParseInt(), new ParseDate("yyyy-MM-dd"),
		new ParseBigDecimal(decimalFormatSymbols) };
	
	CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
	CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), customPreference);
	FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
	
	Assert.assertNotNull(character);
	Assert.assertEquals("John", character.getFirstName());
	Assert.assertEquals("Connor", character.getLastName());
	Assert.assertEquals(16, character.getAge());
	Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd").parse("1999-07-12"), character.getBirthDate());
	Assert.assertEquals(new BigDecimal(6.65, new MathContext(3)), character.getSavings());
}
 
Example #8
Source File: ProcesssorBuilderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
 * CellProcessor名で別途定義する
 */
@Test
public void testErrorMessage_custom() {
    
    // メッセージの入れ替え
    exceptionConverter.setMessageResolver(testMessageResolver);
    
    BeanMapping<TestCsv> beanMapping = beanMappingFactory.create(TestCsv.class, groupEmpty);
    ColumnMapping columnMapping = beanMapping.getColumnMapping("col_default").get();
    
    CellProcessor processor = columnMapping.getCellProcessorForReading();
    
    printCellProcessorChain(processor, name.getMethodName());
    
    // wrong input - wrong format
    String input = "abc";
    try {
        processor.execute(input, testCsvContext(columnMapping, input));
        fail();
        
    } catch(Exception e) {
        assertThat(e).isInstanceOf(SuperCsvCellProcessorException.class);
        
        SuperCsvCellProcessorException processorException = (SuperCsvCellProcessorException)e;
        assertThat(processorException.getProcessor()).isInstanceOf(ParseInt.class);
        
        List<String> messages = exceptionConverter.convertAndFormat((SuperCsvCellProcessorException)e, beanMapping);
        assertThat(messages).hasSize(1)
            .contains("[2行, 1列] : 項目「col_default」の値(abc)は、整数として不正です。");
    }
    
}
 
Example #9
Source File: ConnectionCsvDeserializer.java    From Showcase with Apache License 2.0 5 votes vote down vote up
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 #10
Source File: ProcesssorBuilderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorMessage_default() {
    
    BeanMapping<TestCsv> beanMapping = beanMappingFactory.create(TestCsv.class, groupEmpty);
    ColumnMapping columnMapping = beanMapping.getColumnMapping("col_default").get();
    
    CellProcessor processor = columnMapping.getCellProcessorForReading();
    
    printCellProcessorChain(processor, name.getMethodName());
    
    // wrong input - wrong format
    String input = "abc";
    try {
        processor.execute(input, testCsvContext(columnMapping, input));
        fail();
        
    } catch(Exception e) {
        assertThat(e).isInstanceOf(SuperCsvCellProcessorException.class);
        
        SuperCsvCellProcessorException processorException = (SuperCsvCellProcessorException)e;
        assertThat(processorException.getProcessor()).isInstanceOf(ParseInt.class);
        
        List<String> messages = exceptionConverter.convertAndFormat((SuperCsvCellProcessorException)e, beanMapping);
        assertThat(messages).hasSize(1)
            .contains("'abc' could not be parsed as an Integer");
    }
    
}
 
Example #11
Source File: ProcesssorBuilderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<CellProcessor> buildForReading(final Class<Integer> type, final FieldAccessor field,
        final Configuration config, final Class<?>[] groups) {
    
    CellProcessor processor = new NotNull(new Trim(new ParseInt()));
    return Optional.of(processor);
}
 
Example #12
Source File: Reading.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #13
Source File: Reading.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #14
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertsToPrimitives() throws IOException {
	String csv = "Connor,John,16\r\n";
	String[] mapping = { "lastName", "firstName", "age" };
	CellProcessor[] processors = { new NotNull(), new NotNull(), new ParseInt() };
	
	CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), STANDARD_PREFERENCE);
	FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
	
	Assert.assertNotNull(character);
	Assert.assertEquals("John", character.getFirstName());
	Assert.assertEquals("Connor", character.getLastName());
	Assert.assertEquals(16, character.getAge());
}
 
Example #15
Source File: CellProcessorBuilder.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
/**
 * Get cellprocessor to parse String as Integer.
 *
 * @param cellProcessor
 *          next processor in the chain.
 * @return CellProcessor
 */
private static CellProcessor addParseInt(CellProcessor cellProcessor)
{
  if (cellProcessor == null) {
    return new ParseInt();
  }
  return new ParseInt((LongCellProcessor)cellProcessor);
}
 
Example #16
Source File: AbstractCsvParser.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
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 #17
Source File: Reading.java    From super-csv with Apache License 2.0 4 votes vote down vote up
/**
 * 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 #18
Source File: CsvDozerBeanReaderTest.java    From super-csv with Apache License 2.0 4 votes vote down vote up
/**
 * 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));
	
}
 
Example #19
Source File: CacheLoader.java    From geode-demo-application with Apache License 2.0 4 votes vote down vote up
/**
 * Call this to load the Data, it uses the CSV files in its classpath
 */
public void loadData() {
	
	startTime = new Date().getTime();
	
	//load the customers
	String[] nameMapping = new String[]{"city","birthday","id","name", "emailAddress"};
	CellProcessor[] processors = new CellProcessor[] { 
       		new NotNull(), //city
       		new ParseDate("dd-MM-yyyy"), //birthday
               new NotNull(), //id
               new NotNull(), //name
               new NotNull() //email
       };
       loadCustomers("customer.csv",nameMapping,processors);
       
       //load the products
       nameMapping = new String[]{"stockOnHand","wholeSalePrice","brand","type", "color", "size", "gender", "id"};
       processors = new CellProcessor[] { 
       		new ParseInt(),//stockOnHand
       		new ParseDouble(),//wholeSalePrice
       		new NotNull(),//brand
       		new NotNull(),//type
       		new NotNull(),//color
       		new ParseDouble(),//size
       		new NotNull(),//gender
               new NotNull()//productId
       };
       loadProducts("products.csv",nameMapping,processors);
       
       //load the historic transactions - these are just randomly generated and do not respect stock quantity
       nameMapping = new String[]{"customerId","transactionDate","productId","quantity", "retailPrice", "id", "markUp", "orderStatus"};
       processors = new CellProcessor[] { 
       		new NotNull(),//customerId
       		new ParseDate("dd-MM-yyyy"),//transactionDate
       		new NotNull(),//productId
       		new ParseInt(),//quantity
       		new ParseDouble(),//retailsPrice
       		new NotNull(),//transactionId
       		new ParseDouble(),//markUp
       		new NotNull()//order status
       };
       loadTransactions("transactions.csv",nameMapping,processors);
       
       //load the mark ups
       nameMapping = new String[]{"id", "rate","levelName","qualifyingTransactionCountFloor","qualifyingTransactionCountCeiling"};
       processors = new CellProcessor[] {
       		new NotNull(),//id
       		new ParseDouble(),//rate
       		new NotNull(),//levelName
       		new ParseInt(),//qualifyingTransactionCountFloor
       		new ParseInt()//qualifyingTransactionCountCeiling
       };
       loadMarkUps("markUps.csv",nameMapping,processors);
       
       //clean out the alerts
       for (Alert alert : alertRepository.findAll()) {
       	alertRepository.delete(alert);
       }
       long endTime = new Date().getTime();
	long timeToLoad = endTime - startTime;
	activityLog.add("Total Loading Time: " + timeToLoad/1000 + " seconds");
       closeBeanReader();
	writeOutLogs();
}