Right now, I have base abstract GenericDAO
class to execute CRUD operations with different kind of objects. I have Customer and Employee POJO classes which are used in specific DAO
classes that extends GenericDAO
class.
Questions:
- How correct is the current base
GenericDao
implementation andSpecificDAO
classes which extendGenericDAO
? - Is it a good idea to separate object(i.e.
EmployeeSpecs
,PersonalSpecs
) properties in separate class where some of them are refereced to specific POJO classes? I did it to usesearch()
method easier passing spec object(not by a certain object property) to it. - How can I implement a search method in the
GenericDAOImpl
abstract class correctly when theEmployee
search method requires 2 arguments (EmployeeSpecs
andPersonalSpecs
) and theCustomer
search method requires only one argument (PersonalSpecs
)? - How can I implement specs that I can invoke the following
getSpec()
method insideCustomer
orEmployee
classes receiving all specs claases(PersonalSpecs
,EmployeeSpecs
if we talk aboutEmployee
class) applicable for a certain pojo class?
I've read about varargs. Is it possible to use them with generics that I can put search method inside GenericDAO
class and can search by multiple object specs?
If you will find that some other places could be optimized or totally improved, please let me know.
NB! If someone knows the anwers on 2 & 3 question, take me know, please.
GenericDAO interface:
public interface GenericDAO<T> {
public boolean add(T t);
public boolean update(int index, T t);
public boolean remove(int index);
public T getByIndex(int index);
public List<T> getAll();
// I want to add search method here if it's possible to search by any multiple object specs
}
GenericDAO implementation:
public abstract class GenericDAOImpl<T> implements GenericDAO<T> {
public Set<T> objects = new HashSet<T>();
@Override
public boolean add(T t) {
if (t != null && !objects.contains(t))
objects.add(t);
return objects.contains(t);
}
@Override
public boolean update(int index, T t) {
if (index >= 0 && t != null) {
if (objects.remove(getByIndex(index))) {
objects.add(t);
return true;
}
}
return false;
}
@Override
public boolean remove(int index) {
Object obj = getByIndex(index);
if (obj != null && objects.contains(obj))
objects.remove(obj);
return !objects.contains(obj);
}
@Override
public T getByIndex(int index) {
if (index >= 0 && index < objects.size())
return new ArrayList<T>(objects).get(index);
return null;
}
@Override
public List<T> getAll() {
return new ArrayList<T>(objects);
}
}
EmployeeDAO interface:
public interface EmployeeDAO {
// Some kind of specific functionality for employee
public List<Employee> search(PersonalSpecs personalSpecs, EmployeeSpecs employeeSpecs);
}
EmployeeDAO implemenetation:
public class EmployeeDAOImpl extends GenericDAOImpl<Employee> implements EmployeeDAO {
@Override
public List<Employee> search(PersonalSpecs personalSpecs, EmployeeSpecs employeeSpecs) {
if (personalSpecs != null || employeeSpecs != null) {
for (Employee employee : getAll()) {
if (employee.getPersonalSpecs().matches(personalSpecs, false) || employee.getEmployeeSpecs().matches(employeeSpecs))
add(employee);
}
}
return getAll();
}
}
CustomerDAO interface:
public interface CustomerDAO {
// Some kind of specific functionality for customer
public List<Customer> search(PersonalSpecs personalSpecs);
}
CustomerDAO implementation:
public class CustomerDAOImpl extends GenericDAOImpl<Customer> implements CustomerDAO {
@Override
public List<Customer> search(PersonalSpecs personalSpecs) {
if (personalSpecs != null) {
for (Customer customer : getAll()) {
if (customer.getPersonalSpecs().matches(personalSpecs, false))
add(customer);
}
}
return getAll();
}
}
Employee class:
public class Employee {
private PersonalSpecs personalSpecs;
private EmployeeSpecs employeeSpecs;
public Employee(PersonalSpecs personalSpecs, EmployeeSpecs employeeSpecs) {
this.personalSpecs = personalSpecs;
this.employeeSpecs = employeeSpecs;
}
public PersonalSpecs getPersonalSpecs() {
return personalSpecs;
}
public void setPersonalSpecs(PersonalSpecs personalSpecs) {
this.personalSpecs = personalSpecs;
}
public EmployeeSpecs getEmployeeSpecs() {
return employeeSpecs;
}
public void setEmployeeSpecs(EmployeeSpecs employeeSpecs) {
this.employeeSpecs = employeeSpecs;
}
@Override
public int hashCode() {
return this.personalSpecs.getName().hashCode() + this.personalSpecs.getSurname().hashCode();
}
@Override
public boolean equals(Object obj) {
return ((Employee) obj).personalSpecs.getName().equals(this.personalSpecs.getName()) && ((Employee) obj).personalSpecs.getSurname().equals(this.personalSpecs.getSurname());
}
@Override
public String toString() {
return "Hash: " +
hashCode() +
" - Employee{\n" +
personalSpecs +
"\n" +
employeeSpecs +
'}';
}
}
Customer class:
public class Customer {
private PersonalSpecs personalSpecs;
private int bonus;
public Customer(PersonalSpecs personalSpecs) {
this.personalSpecs = personalSpecs;
}
public Customer(PersonalSpecs personalSpecs, int bonus) {
this.personalSpecs = personalSpecs;
this.bonus = bonus;
}
public PersonalSpecs getPersonalSpecs() {
return personalSpecs;
}
public void setPersonalSpecs(PersonalSpecs personalSpecs) {
this.personalSpecs = personalSpecs;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
@Override
public int hashCode() {
return this.personalSpecs.getName().hashCode() + this.personalSpecs.getSurname().hashCode();
}
@Override
public boolean equals(Object obj) {
return ((Customer) obj).personalSpecs.getName().equals(this.personalSpecs.getName()) && ((Customer) obj).personalSpecs.getSurname().equals(this.personalSpecs.getSurname());
}
@Override
public String toString() {
return "Customer: " +
personalSpecs +
", bonus=" + bonus;
}
}
EmployeeSpecs class:
public class EmployeeSpecs {
private EmployeeType type;
private String position;
private Date start;
public enum EmployeeType {
CONTRACTOR,
FULLTIME
}
public EmployeeSpecs(EmployeeType type, String position) {
this.type = type;
this.position = position;
}
public EmployeeType getType() {
return type;
}
public void setType(EmployeeType type) {
this.type = type;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
public boolean matches(Object obj) {
EmployeeSpecs employeeSpecs = (EmployeeSpecs) obj;
if (employeeSpecs != null) {
if ((employeeSpecs.getType() != null && employeeSpecs.getType().equals(type)) ||
(employeeSpecs.getPosition() != null && !employeeSpecs.getPosition().isEmpty() && employeeSpecs.getPosition().equals(position)) ||
(employeeSpecs.getStart() != null && employeeSpecs.getStart().equals(start)))
return true;
}
return false;
}
@Override
public String toString() {
return "[" +
"type=" + type +
", position='" + position + '\'' +
", start=" + start +
']';
}
}
PersonalSpecs class:
public class PersonalSpecs {
private String name;
private String surname;
private Sex sex;
private int age;
public enum Sex {MALE, FEMALE}
public PersonalSpecs(String name, String surname, Sex sex) {
this.name = name;
this.surname = surname;
this.sex = sex;
}
public PersonalSpecs(String name, String surname, Sex sex, int age) {
this.name = name;
this.surname = surname;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Sex getSex() {
return sex;
}
public void setSex(Sex sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean matches(Object obj, boolean strict) {
PersonalSpecs personalSpecs = (PersonalSpecs) obj;
if (personalSpecs != null) {
if (strict)
// Check matches only by full name
return (personalSpecs.getName() != null && !personalSpecs.getName().isEmpty() && personalSpecs.getName().equals(name)) &&
(personalSpecs.getSurname() != null && !personalSpecs.getSurname().isEmpty() && personalSpecs.getSurname().equals(surname));
else
// Check any matches
return (personalSpecs.getName() != null && !personalSpecs.getName().isEmpty() && personalSpecs.getName().equals(name)) ||
(personalSpecs.getSurname() != null && !personalSpecs.getSurname().isEmpty() && personalSpecs.getSurname().equals(surname)) ||
(personalSpecs.getSex() != null && personalSpecs.getSex().equals(sex)) ||
(personalSpecs.getAge() != 0 && personalSpecs.getAge() == age);
}
return false;
}
@Override
public String toString() {
return "[" +
"name='" + name + '\'' +
", surname='" + surname + '\'' +
", sex=" + sex +
", age=" + age +
']';
}
}