[BAEL-10840] - Create a libraries-apache-commons module
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.commons.beanutils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Course {
|
||||
private String name;
|
||||
private List<String> codes;
|
||||
private Map<String, Student> enrolledStudent = new HashMap<String, Student>();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<String> getCodes() {
|
||||
return codes;
|
||||
}
|
||||
|
||||
public void setCodes(List<String> codes) {
|
||||
this.codes = codes;
|
||||
}
|
||||
|
||||
public void setEnrolledStudent(String enrolledId, Student student) {
|
||||
enrolledStudent.put(enrolledId, student);
|
||||
}
|
||||
|
||||
public Student getEnrolledStudent(String enrolledId) {
|
||||
return enrolledStudent.get(enrolledId);
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.commons.beanutils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class CourseEntity {
|
||||
private String name;
|
||||
private List<String> codes;
|
||||
private Map<String, Student> students = new HashMap<String, Student>();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<String> getCodes() {
|
||||
return codes;
|
||||
}
|
||||
|
||||
public void setCodes(List<String> codes) {
|
||||
this.codes = codes;
|
||||
}
|
||||
|
||||
public void setStudent(String id, Student student) {
|
||||
students.put(id, student);
|
||||
}
|
||||
|
||||
public Student getStudent(String enrolledId) {
|
||||
return students.get(enrolledId);
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.commons.beanutils;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.beanutils.BeanUtils;
|
||||
import org.apache.commons.beanutils.PropertyUtils;
|
||||
|
||||
public class CourseService {
|
||||
|
||||
public static void setValues(Course course, String name, List<String> codes) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
// Setting the simple properties
|
||||
PropertyUtils.setSimpleProperty(course, "name", name);
|
||||
PropertyUtils.setSimpleProperty(course, "codes", codes);
|
||||
}
|
||||
|
||||
public static void setIndexedValue(Course course, int codeIndex, String code) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
// Setting the indexed properties
|
||||
PropertyUtils.setIndexedProperty(course, "codes[" + codeIndex + "]", code);
|
||||
}
|
||||
|
||||
public static void setMappedValue(Course course, String enrollId, Student student) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
// Setting the mapped properties
|
||||
PropertyUtils.setMappedProperty(course, "enrolledStudent(" + enrollId + ")", student);
|
||||
}
|
||||
|
||||
public static String getNestedValue(Course course, String enrollId, String nestedPropertyName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
return (String) PropertyUtils.getNestedProperty(course, "enrolledStudent(" + enrollId + ")." + nestedPropertyName);
|
||||
}
|
||||
|
||||
public static void copyProperties(Course course, CourseEntity courseEntity) throws IllegalAccessException, InvocationTargetException {
|
||||
BeanUtils.copyProperties(course, courseEntity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.commons.beanutils;
|
||||
|
||||
public class Student {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.commons.chain;
|
||||
|
||||
import org.apache.commons.chain.Command;
|
||||
import org.apache.commons.chain.Context;
|
||||
|
||||
import static com.baeldung.commons.chain.AtmConstants.AMOUNT_LEFT_TO_BE_WITHDRAWN;
|
||||
|
||||
public abstract class AbstractDenominationDispenser implements Command {
|
||||
|
||||
@Override
|
||||
public boolean execute(Context context) throws Exception {
|
||||
int amountLeftToBeWithdrawn = (int) context.get(AMOUNT_LEFT_TO_BE_WITHDRAWN);
|
||||
if (amountLeftToBeWithdrawn >= getDenominationValue()) {
|
||||
context.put(getDenominationString(), amountLeftToBeWithdrawn / getDenominationValue());
|
||||
context.put(AMOUNT_LEFT_TO_BE_WITHDRAWN, amountLeftToBeWithdrawn % getDenominationValue());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected abstract String getDenominationString();
|
||||
|
||||
protected abstract int getDenominationValue();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.commons.chain;
|
||||
|
||||
import org.apache.commons.chain.impl.CatalogBase;
|
||||
|
||||
import static com.baeldung.commons.chain.AtmConstants.ATM_WITHDRAWAL_CHAIN;
|
||||
|
||||
public class AtmCatalog extends CatalogBase {
|
||||
|
||||
public AtmCatalog() {
|
||||
super();
|
||||
addCommand(ATM_WITHDRAWAL_CHAIN, new AtmWithdrawalChain());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.commons.chain;
|
||||
|
||||
public class AtmConstants {
|
||||
public static final String TOTAL_AMOUNT_TO_BE_WITHDRAWN = "totalAmountToBeWithdrawn";
|
||||
public static final String AMOUNT_LEFT_TO_BE_WITHDRAWN = "amountLeftToBeWithdrawn";
|
||||
public static final String NO_OF_HUNDREDS_DISPENSED = "noOfHundredsDispensed";
|
||||
public static final String NO_OF_FIFTIES_DISPENSED = "noOfFiftiesDispensed";
|
||||
public static final String NO_OF_TENS_DISPENSED = "noOfTensDispensed";
|
||||
public static final String ATM_WITHDRAWAL_CHAIN = "atmWithdrawalChain";
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.commons.chain;
|
||||
|
||||
import org.apache.commons.chain.impl.ContextBase;
|
||||
|
||||
public class AtmRequestContext extends ContextBase {
|
||||
|
||||
int totalAmountToBeWithdrawn;
|
||||
int noOfHundredsDispensed;
|
||||
int noOfFiftiesDispensed;
|
||||
int noOfTensDispensed;
|
||||
int amountLeftToBeWithdrawn;
|
||||
|
||||
public int getTotalAmountToBeWithdrawn() {
|
||||
return totalAmountToBeWithdrawn;
|
||||
}
|
||||
|
||||
public void setTotalAmountToBeWithdrawn(int totalAmountToBeWithdrawn) {
|
||||
this.totalAmountToBeWithdrawn = totalAmountToBeWithdrawn;
|
||||
}
|
||||
|
||||
public int getNoOfHundredsDispensed() {
|
||||
return noOfHundredsDispensed;
|
||||
}
|
||||
|
||||
public void setNoOfHundredsDispensed(int noOfHundredsDispensed) {
|
||||
this.noOfHundredsDispensed = noOfHundredsDispensed;
|
||||
}
|
||||
|
||||
public int getNoOfFiftiesDispensed() {
|
||||
return noOfFiftiesDispensed;
|
||||
}
|
||||
|
||||
public void setNoOfFiftiesDispensed(int noOfFiftiesDispensed) {
|
||||
this.noOfFiftiesDispensed = noOfFiftiesDispensed;
|
||||
}
|
||||
|
||||
public int getNoOfTensDispensed() {
|
||||
return noOfTensDispensed;
|
||||
}
|
||||
|
||||
public void setNoOfTensDispensed(int noOfTensDispensed) {
|
||||
this.noOfTensDispensed = noOfTensDispensed;
|
||||
}
|
||||
|
||||
public int getAmountLeftToBeWithdrawn() {
|
||||
return amountLeftToBeWithdrawn;
|
||||
}
|
||||
|
||||
public void setAmountLeftToBeWithdrawn(int amountLeftToBeWithdrawn) {
|
||||
this.amountLeftToBeWithdrawn = amountLeftToBeWithdrawn;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.commons.chain;
|
||||
|
||||
import org.apache.commons.chain.impl.ChainBase;
|
||||
|
||||
public class AtmWithdrawalChain extends ChainBase {
|
||||
|
||||
public AtmWithdrawalChain() {
|
||||
super();
|
||||
addCommand(new HundredDenominationDispenser());
|
||||
addCommand(new FiftyDenominationDispenser());
|
||||
addCommand(new TenDenominationDispenser());
|
||||
addCommand(new AuditFilter());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.commons.chain;
|
||||
|
||||
import org.apache.commons.chain.Context;
|
||||
import org.apache.commons.chain.Filter;
|
||||
|
||||
public class AuditFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public boolean postprocess(Context context, Exception exception) {
|
||||
// Send notification to customer & bank.
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(Context context) throws Exception {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.commons.chain;
|
||||
|
||||
import static com.baeldung.commons.chain.AtmConstants.NO_OF_FIFTIES_DISPENSED;
|
||||
|
||||
public class FiftyDenominationDispenser extends AbstractDenominationDispenser {
|
||||
@Override
|
||||
protected String getDenominationString() {
|
||||
return NO_OF_FIFTIES_DISPENSED;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getDenominationValue() {
|
||||
return 50;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.commons.chain;
|
||||
|
||||
import static com.baeldung.commons.chain.AtmConstants.NO_OF_HUNDREDS_DISPENSED;
|
||||
|
||||
public class HundredDenominationDispenser extends AbstractDenominationDispenser {
|
||||
@Override
|
||||
protected String getDenominationString() {
|
||||
return NO_OF_HUNDREDS_DISPENSED;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getDenominationValue() {
|
||||
return 100;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.commons.chain;
|
||||
|
||||
import static com.baeldung.commons.chain.AtmConstants.NO_OF_TENS_DISPENSED;
|
||||
|
||||
public class TenDenominationDispenser extends AbstractDenominationDispenser {
|
||||
@Override
|
||||
protected String getDenominationString() {
|
||||
return NO_OF_TENS_DISPENSED;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getDenominationValue() {
|
||||
return 10;
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.commons.collectionutil;
|
||||
|
||||
public class Address {
|
||||
|
||||
private String locality;
|
||||
private String city;
|
||||
private String zip;
|
||||
|
||||
public String getLocality() {
|
||||
return locality;
|
||||
}
|
||||
|
||||
public void setLocality(String locality) {
|
||||
this.locality = locality;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getZip() {
|
||||
return zip;
|
||||
}
|
||||
|
||||
public void setZip(String zip) {
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("Address [locality=").append(locality).append(", city=").append(city).append(", zip=").append(zip).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public Address(String locality, String city, String zip) {
|
||||
super();
|
||||
this.locality = locality;
|
||||
this.city = city;
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package com.baeldung.commons.collectionutil;
|
||||
|
||||
public class Customer implements Comparable<Customer> {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
private Long phone;
|
||||
private String locality;
|
||||
private String city;
|
||||
private String zip;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(Long phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public String getLocality() {
|
||||
return locality;
|
||||
}
|
||||
|
||||
public void setLocality(String locality) {
|
||||
this.locality = locality;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getZip() {
|
||||
return zip;
|
||||
}
|
||||
|
||||
public void setZip(String zip) {
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
public Customer(Integer id, String name, Long phone, String locality, String city, String zip) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.phone = phone;
|
||||
this.locality = locality;
|
||||
this.city = city;
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
public Customer(Integer id, String name, Long phone) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public Customer(String name) {
|
||||
super();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Customer other = (Customer) obj;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public int compareTo(Customer o) {
|
||||
return this.name.compareTo(o.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("Customer [id=").append(id).append(", name=").append(name).append(", phone=").append(phone).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.commons.dbutils;
|
||||
|
||||
public class Email {
|
||||
private Integer id;
|
||||
private Integer employeeId;
|
||||
private String address;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
public void setEmployeeId(Integer employeeId) {
|
||||
this.employeeId = employeeId;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Email{" + "id=" + id + ", address=" + address + '}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.commons.dbutils;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class Employee {
|
||||
private Integer id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private Double salary;
|
||||
private Date hiredDate;
|
||||
private List<Email> emails;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(Double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public Date getHiredDate() {
|
||||
return hiredDate;
|
||||
}
|
||||
|
||||
public void setHiredDate(Date hiredDate) {
|
||||
this.hiredDate = hiredDate;
|
||||
}
|
||||
|
||||
public List<Email> getEmails() {
|
||||
return emails;
|
||||
}
|
||||
|
||||
public void setEmails(List<Email> emails) {
|
||||
this.emails = emails;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Employee{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", salary=" + salary + ", hiredDate=" + hiredDate + '}';
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.commons.dbutils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.commons.dbutils.BasicRowProcessor;
|
||||
import org.apache.commons.dbutils.BeanProcessor;
|
||||
|
||||
import org.apache.commons.dbutils.QueryRunner;
|
||||
import org.apache.commons.dbutils.handlers.BeanListHandler;
|
||||
|
||||
public class EmployeeHandler extends BeanListHandler<Employee> {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
public EmployeeHandler(Connection con) {
|
||||
super(Employee.class, new BasicRowProcessor(new BeanProcessor(getColumnsToFieldsMap())));
|
||||
this.connection = con;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Employee> handle(ResultSet rs) throws SQLException {
|
||||
List<Employee> employees = super.handle(rs);
|
||||
|
||||
QueryRunner runner = new QueryRunner();
|
||||
BeanListHandler<Email> handler = new BeanListHandler<>(Email.class);
|
||||
String query = "SELECT * FROM email WHERE employeeid = ?";
|
||||
for (Employee employee : employees) {
|
||||
List<Email> emails = runner.query(connection, query, handler, employee.getId());
|
||||
employee.setEmails(emails);
|
||||
}
|
||||
return employees;
|
||||
}
|
||||
|
||||
public static Map<String, String> getColumnsToFieldsMap() {
|
||||
Map<String, String> columnsToFieldsMap = new HashMap<>();
|
||||
columnsToFieldsMap.put("FIRST_NAME", "firstName");
|
||||
columnsToFieldsMap.put("LAST_NAME", "lastName");
|
||||
columnsToFieldsMap.put("HIRED_DATE", "hiredDate");
|
||||
return columnsToFieldsMap;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.commons.io;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.monitor.FileAlterationListener;
|
||||
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
|
||||
import org.apache.commons.io.monitor.FileAlterationMonitor;
|
||||
import org.apache.commons.io.monitor.FileAlterationObserver;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class FileMonitor {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
File folder = FileUtils.getTempDirectory();
|
||||
startFileMonitor(folder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param folder
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void startFileMonitor(File folder) throws Exception {
|
||||
FileAlterationObserver observer = new FileAlterationObserver(folder);
|
||||
FileAlterationMonitor monitor = new FileAlterationMonitor(5000);
|
||||
|
||||
FileAlterationListener fal = new FileAlterationListenerAdaptor() {
|
||||
|
||||
@Override
|
||||
public void onFileCreate(File file) {
|
||||
// on create action
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFileDelete(File file) {
|
||||
// on delete action
|
||||
}
|
||||
};
|
||||
|
||||
observer.addListener(fal);
|
||||
monitor.addObserver(observer);
|
||||
monitor.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.baeldung.commons.lang3;
|
||||
|
||||
import org.apache.commons.lang3.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang3.builder.HashCodeBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.concurrent.ConcurrentException;
|
||||
import org.apache.commons.lang3.concurrent.BackgroundInitializer;
|
||||
|
||||
public class BuilderMethods {
|
||||
|
||||
private final int intValue;
|
||||
private final String strSample;
|
||||
|
||||
public BuilderMethods(final int newId, final String newName) {
|
||||
this.intValue = newId;
|
||||
this.strSample = newName;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.intValue;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.strSample;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return new HashCodeBuilder().append(this.intValue).append(this.strSample).toHashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof BuilderMethods == false) {
|
||||
return false;
|
||||
}
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
final BuilderMethods otherObject = (BuilderMethods) obj;
|
||||
|
||||
return new EqualsBuilder().append(this.intValue, otherObject.intValue).append(this.strSample, otherObject.strSample).isEquals();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this).append("INTVALUE", this.intValue).append("STRINGVALUE", this.strSample).toString();
|
||||
}
|
||||
|
||||
public static void main(final String[] arguments) {
|
||||
final BuilderMethods simple1 = new BuilderMethods(1, "The First One");
|
||||
System.out.println(simple1.getName());
|
||||
System.out.println(simple1.hashCode());
|
||||
System.out.println(simple1.toString());
|
||||
|
||||
SampleLazyInitializer sampleLazyInitializer = new SampleLazyInitializer();
|
||||
|
||||
try {
|
||||
sampleLazyInitializer.get();
|
||||
} catch (ConcurrentException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
SampleBackgroundInitializer sampleBackgroundInitializer = new SampleBackgroundInitializer();
|
||||
sampleBackgroundInitializer.start();
|
||||
|
||||
// Proceed with other tasks instead of waiting for the SampleBackgroundInitializer task to finish.
|
||||
|
||||
try {
|
||||
Object result = sampleBackgroundInitializer.get();
|
||||
} catch (ConcurrentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SampleBackgroundInitializer extends BackgroundInitializer<String> {
|
||||
|
||||
@Override
|
||||
protected String initialize() throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Any complex task that takes some time
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.commons.lang3;
|
||||
|
||||
import org.apache.commons.lang3.concurrent.LazyInitializer;
|
||||
|
||||
public class SampleLazyInitializer extends LazyInitializer<SampleObject> {
|
||||
|
||||
@Override
|
||||
protected SampleObject initialize() {
|
||||
return new SampleObject();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.commons.lang3;
|
||||
|
||||
public class SampleObject {
|
||||
|
||||
// Ignored
|
||||
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.commons.lang3.application;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.commons.lang3.beans;
|
||||
|
||||
public class User {
|
||||
|
||||
private final String name;
|
||||
private final String email;
|
||||
|
||||
public User(String name, String email) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name=" + name + ", email=" + email + '}';
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.commons.lang3.beans;
|
||||
|
||||
import org.apache.commons.lang3.concurrent.LazyInitializer;
|
||||
|
||||
public class UserInitializer extends LazyInitializer<User> {
|
||||
|
||||
@Override
|
||||
protected User initialize() {
|
||||
return new User("John", "john@domain.com");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.baeldung.commons.math3;
|
||||
|
||||
import org.apache.commons.math3.stat.Frequency;
|
||||
import org.knowm.xchart.CategoryChart;
|
||||
import org.knowm.xchart.CategoryChartBuilder;
|
||||
import org.knowm.xchart.SwingWrapper;
|
||||
import org.knowm.xchart.style.Styler;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Histogram {
|
||||
|
||||
private Map distributionMap;
|
||||
private int classWidth;
|
||||
|
||||
public Histogram() {
|
||||
|
||||
distributionMap = new TreeMap();
|
||||
classWidth = 10;
|
||||
Map distributionMap = processRawData();
|
||||
List yData = new ArrayList();
|
||||
yData.addAll(distributionMap.values());
|
||||
List xData = Arrays.asList(distributionMap.keySet().toArray());
|
||||
|
||||
CategoryChart chart = buildChart(xData, yData);
|
||||
new SwingWrapper<>(chart).displayChart();
|
||||
|
||||
}
|
||||
|
||||
private CategoryChart buildChart(List xData, List yData) {
|
||||
|
||||
// Create Chart
|
||||
CategoryChart chart = new CategoryChartBuilder().width(800).height(600)
|
||||
.title("Age Distribution")
|
||||
.xAxisTitle("Age Group")
|
||||
.yAxisTitle("Frequency")
|
||||
.build();
|
||||
|
||||
chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideNW);
|
||||
chart.getStyler().setAvailableSpaceFill(0.99);
|
||||
chart.getStyler().setOverlapped(true);
|
||||
|
||||
chart.addSeries("age group", xData, yData);
|
||||
|
||||
return chart;
|
||||
}
|
||||
|
||||
private Map processRawData() {
|
||||
|
||||
List<Integer> datasetList = Arrays.asList(
|
||||
36, 25, 38, 46, 55, 68, 72,
|
||||
55, 36, 38, 67, 45, 22, 48,
|
||||
91, 46, 52, 61, 58, 55);
|
||||
Frequency frequency = new Frequency();
|
||||
datasetList.forEach(d -> frequency.addValue(Double.parseDouble(d.toString())));
|
||||
|
||||
datasetList.stream()
|
||||
.map(d -> Double.parseDouble(d.toString()))
|
||||
.distinct()
|
||||
.forEach(observation -> {
|
||||
long observationFrequency = frequency.getCount(observation);
|
||||
int upperBoundary = (observation > classWidth)
|
||||
? Math.multiplyExact( (int) Math.ceil(observation / classWidth), classWidth)
|
||||
: classWidth;
|
||||
int lowerBoundary = (upperBoundary > classWidth)
|
||||
? Math.subtractExact(upperBoundary, classWidth)
|
||||
: 0;
|
||||
String bin = lowerBoundary + "-" + upperBoundary;
|
||||
|
||||
updateDistributionMap(lowerBoundary, bin, observationFrequency);
|
||||
|
||||
});
|
||||
|
||||
return distributionMap;
|
||||
}
|
||||
|
||||
private void updateDistributionMap(int lowerBoundary, String bin, long observationFrequency) {
|
||||
|
||||
int prevLowerBoundary = (lowerBoundary > classWidth) ? lowerBoundary - classWidth : 0;
|
||||
String prevBin = prevLowerBoundary + "-" + lowerBoundary;
|
||||
if(!distributionMap.containsKey(prevBin))
|
||||
distributionMap.put(prevBin, 0);
|
||||
|
||||
if(!distributionMap.containsKey(bin)) {
|
||||
distributionMap.put(bin, observationFrequency);
|
||||
}
|
||||
else {
|
||||
long oldFrequency = Long.parseLong(distributionMap.get(bin).toString());
|
||||
distributionMap.replace(bin, oldFrequency + observationFrequency);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Histogram();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.ftp;
|
||||
|
||||
import org.apache.commons.net.PrintCommandListener;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
import org.apache.commons.net.ftp.FTPReply;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class FtpClient {
|
||||
|
||||
private final String server;
|
||||
private final int port;
|
||||
private final String user;
|
||||
private final String password;
|
||||
private FTPClient ftp;
|
||||
|
||||
FtpClient(String server, int port, String user, String password) {
|
||||
this.server = server;
|
||||
this.port = port;
|
||||
this.user = user;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
void open() throws IOException {
|
||||
ftp = new FTPClient();
|
||||
|
||||
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
|
||||
|
||||
ftp.connect(server, port);
|
||||
int reply = ftp.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
ftp.disconnect();
|
||||
throw new IOException("Exception in connecting to FTP Server");
|
||||
}
|
||||
|
||||
ftp.login(user, password);
|
||||
}
|
||||
|
||||
void close() throws IOException {
|
||||
ftp.disconnect();
|
||||
}
|
||||
|
||||
Collection<String> listFiles(String path) throws IOException {
|
||||
FTPFile[] files = ftp.listFiles(path);
|
||||
|
||||
return Arrays.stream(files)
|
||||
.map(FTPFile::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
void putFileToPath(File file, String path) throws IOException {
|
||||
ftp.storeFile(path, new FileInputStream(file));
|
||||
}
|
||||
|
||||
void downloadFile(String source, String destination) throws IOException {
|
||||
FileOutputStream out = new FileOutputStream(destination);
|
||||
ftp.retrieveFile(source, out);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user