/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package [Link];
/**
* This interface is responsible to give Person related operations.
* @author Hyderareefa
*/
public interface Customer {
public String getCustomerNo();
public void setCustomerNo(String customerNo);
public long getLoyaltyPoints();
public void setLoyaltyPoints(long loyaltyPoints);
public String getMailingAddress();
public void setMailingAddress(String mailingAddress);
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package [Link];
import [Link];
/**
* This class is responsible to to collect data from other Beans.
* @author Hyderareefa
*/
public class CustomerBean extends PersonBean implements Customer {
private String customerNo;
private long loyaltyPoints;
private String mailingAddress;
/**
* Constructor
*/
public CustomerBean() {
}
/**
* Constructor
* @param customerNo
* @param firstName
* @param lastName
* @param birthDate
* @param mailingAddress
* @param married
* @param numberOfKids
* @param favouriteQuote
* @param email
* @param loyaltyPoints
*/
public CustomerBean(final String customerNo, final String firstName, final S
tring lastName, final Date birthDate,
final String mailingAddress, final Boolean married, final Integer number
OfKids, final String favouriteQuote,
final String email, final long loyaltyPoints) {
super(firstName, lastName, birthDate, married, numberOfKids, favouriteQu
ote, email);
[Link] = customerNo;
[Link] = loyaltyPoints;
[Link] = mailingAddress;
}
@Override
public String getCustomerNo() {
return customerNo;
}
@Override
public long getLoyaltyPoints() {
return loyaltyPoints;
}
@Override
public String getMailingAddress() {
return mailingAddress;
}
@Override
public void setCustomerNo(String customerNo) {
[Link] = customerNo;
}
@Override
public void setLoyaltyPoints(long loyaltyPoints) {
[Link] = loyaltyPoints;
}
@Override
public void setMailingAddress(String mailingAddress) {
[Link] = mailingAddress;
}
@Override
public String toString() {
return String
.format(
"CustomerBean [customerNo=%s, firstName=%s, last
Name=%s, birthDate=%s, mailingAddress=%s, married=%s, numberOfKids=%s, favourite
Quote=%s, email=%s, loyaltyPoints=%s]",
customerNo, getFirstName(), getLastName(), getBi
rthDate(), mailingAddress, getMarried(),
getNumberOfKids(), getFavouriteQuote(), getEmail
(), loyaltyPoints);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/**
*
* @author Hyderareefa
*/
public class Reading {
private final static String CSV_FILE_NAME = "E:/supercsv/writeWithCSVBeanWri
[Link]";
public static void main(String[] args) throws Exception {
readWithCsvBeanReader();
}
/**
* This method set up the Cell Processor.
* @return processor
*/
private static CellProcessor[] getCellProcessors() {
/*
final String emailRegEX = "[a-z0-9\\\\._]+@[a-z0-9\\\\.]+";
[Link](emailRegEX, "Must be a valid email address.");
*/
final CellProcessor[] processor = new CellProcessor[] {
new UniqueHashCode(), //customerNo
new NotNull(), // firstName
new NotNull(), // lastName
new ParseDate("dd/MM/yyyy"), // birthDate
new NotNull(), // mailingAddress
new Optional(new ParseBool()), // married
new Optional(new ParseInt()), // numberOfKids
new NotNull(), // favouriteQuote
new NotNull(), // email
new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints
};
return processor;
}
/**
* This method is responsible to read data from CSV file using CSVBeanReader
.
* @throws Exception
*/
public static void readWithCsvBeanReader() throws Exception {
ICsvBeanReader beanReader = null;
try {
beanReader = new CsvBeanReader(new FileReader(CSV_FILE_NAME),CsvPref
erence.STANDARD_PREFERENCE);
final String[] header = [Link](true);
final CellProcessor[] processor = getCellProcessors();
CustomerBean customer;
while((customer = [Link]([Link], header, proces
sor)) != null) {
[Link]([Link]("Line no = %s, RowNo = %s, Cust
omer = %s",
[Link](), [Link](), c
ustomer));
}
} catch(IOException ie) {
[Link]([Link]());
} finally {
try {
if(beanReader != null) {
[Link]();
}
} catch(IOException ie) {
[Link]([Link]());
}
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package [Link];
/**
*
* @author Hyderareefa
*/
public class SampleTest {
public static void main(String[] args) {
new Test().getValue();
}
}
class Test {
boolean value;
public Test() {
value = true;
}
public void getValue() {
[Link](value);
initialize();
[Link](value);
}
public void initialize() {
value = false;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/**
*
* @author Hyderareefa
*/
public class Writing {
public static void main(String[] args) throws Exception {
writeWithCSVBeanWriter();
writeWithCSVListWriter();
writeWithCSVMapWriter();
}
/**
* This method is responsible to generate an CellProcessor
* @return processors
*/
private static CellProcessor[] getCellProcessor() {
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 Optional(), //favouriteQuote
new NotNull(), //email
new LMinMax(0L, LMinMax.MAX_LONG) //loyalityPoints
};
return processors;
}
/**
* This method is responsible to write data into a CSV file
* using CSVBeanWriter.
*
* @throws Exception
*/
public static void writeWithCSVBeanWriter() throws Exception {
final CustomerBean john = new CustomerBean("1", "John", "Dunbar", new Gr
egorianCalendar(1945, [Link], 13).getTime(),
"1600 Amphitheater Parkway\nMountain View, CA 94043\nUnited Stat
es", null, null,
"\"May the Force be with you.\" - Star Wars", "jdbundar@[Link]
m", 0L);
final CustomerBean bob = new CustomerBean("2", "Bob", "Down", new Gregor
ianCalendar(1919, [Link], 25).getTime(),
"1601 Willow Rd.\nMenlo Park, CA 94056\nUnited States", Boolean.
TRUE, 0,
"\"Frankly, my dear I don't give a damn.\" - Gone With the Wind
", "bobdown@hotmail@[Link]", 123456L);
final List<CustomerBean> customers = [Link](john, bob);
ICsvBeanWriter beanWriter = null;
try {
beanWriter = new CsvBeanWriter(new FileWriter("E:/supercsv/writeWith
[Link]"), CsvPreference.STANDARD_PREFERENCE);
final String[] header = new String[] {"customerNo", "firstName", "la
stName", "birthDate", "mailingAddress", "married", "numberOfKids", "favouriteQuo
te", "email", "loyaltyPoints"};
final CellProcessor[] processors = getCellProcessor();
[Link](header);
for(final CustomerBean customer :customers) {
[Link](customer, header, processors);
}
} catch(IOException io) {
[Link]([Link]());
} finally {
try {
if(beanWriter != null) {
[Link]();
}
} catch(IOException io) {
[Link]([Link]());
}
}
}
/**
* This method is responsible to write the data into CSV file
* using CSVListWriter.
* @throws Exception
*/
public static void writeWithCSVListWriter() throws Exception {
final List<Object> john = [Link](new Object[] {"1", "John", "Dunb
ar", new GregorianCalendar(1945, [Link], 13).getTime(),
"1600 Amphitheater Parkway\nMountain View, CA 94043\nUnited Stat
es", null, null,
"\"May the Force be with you.\" - Star Wars", "jdbundar@[Link]
m", 0L});
final List<Object> bob = [Link](new Object[] {"2", "Bob", "Down",
new GregorianCalendar(1919, [Link], 25).getTime(),
"1601 Willow Rd.\nMenlo Park, CA 94056\nUnited States", Boolean.
TRUE, 0,
"\"Frankly, my dear I don't give a damn.\" - Gone With the Wind
", "bobdown@hotmail@[Link]", 123456L});
ICsvListWriter beanWriter = null;
try {
beanWriter = new CsvListWriter(new FileWriter("E:/supercsv/writeWith
[Link]"), CsvPreference.STANDARD_PREFERENCE);
final CellProcessor[] processors = getCellProcessor();
final String[] header = new String[] {"customerNo", "firstName", "la
stName", "birthDate", "mailingAddress",
"married", "numberOfKids", "favouriteQuote", "email", "loyal
tyPoints"};
[Link](header);
[Link](john, processors);
[Link](bob, processors);
} catch(IOException io) {
[Link]([Link]());
} finally {
try {
if(beanWriter != null) {
[Link]();
}
} catch(IOException io) {
[Link]([Link]());
}
}
}
/**
* This method is responsible for writing data into CSV file using
* CSVMapWriter
* @throws Exception
*/
public static void writeWithCSVMapWriter() throws Exception {
final String[] header = new String[] {"customerNo", "firstName", "lastNa
me", "birthDate", "mailingAddress",
"married", "numberOfKids", "favouriteQuote", "email", "loyal
tyPoints"};
final Map<String, Object> john = new HashMap<>();
[Link](header[0], "1");
[Link](header[1], "John");
[Link](header[2], "Dunbar");
[Link](header[3], new GregorianCalendar(1945, [Link], 13).getTi
me());
[Link](header[4], "1600 Amphitheatre Parkway\\nMountain View, CA 94043
\\nUnited States");
[Link](header[5], null);
[Link](header[6], null);
[Link](header[7], "\"May the Force be with you.\" - Star Wars");
[Link](header[8], "jdunbar@@[Link]");
[Link](header[9], 0L);
final Map<String, Object> bob = new HashMap<>();
[Link](header[0], "2");
[Link](header[1], "Bob");
[Link](header[2], "Down");
[Link](header[3], new GregorianCalendar(1919, [Link], 25).ge
tTime());
[Link](header[4], "1601 Willow Rd.\nMenlo Park, CA 94025\nUnited States
");
[Link](header[5], true);
[Link](header[6], 0);
[Link](header[7], "\"Frankly, my dear, I don't give a damn.\" - Gone Wi
th The Wind");
[Link](header[8], "bobdown@[Link]");
[Link](header[9], 123456L);
ICsvMapWriter mapWriter = null;
try {
mapWriter = new CsvMapWriter(new FileWriter("E:/supercsv/writeWithMa
[Link]"), CsvPreference.STANDARD_PREFERENCE);
final CellProcessor[] cellProcessors = getCellProcessor();
[Link](header);
[Link](john, header, cellProcessors);
[Link](bob, header, cellProcessors);
} catch(IOException ex) {
[Link]([Link]());
} finally {
try {
if(mapWriter != null) {
[Link]();
}
} catch(IOException ex) {
[Link]([Link]());
}
}
}
}