java compare multiple values and find best match - java

I need to find best matched employee salary in the DB records as:
Name: City: State:
A (null) (null)
A (null) DEL
(null) (null) (null)
A SAKET DEL
Match order should be:
1. NAME = name, STATE = state, CITY = city
2. NAME = name, STATE = state , CITY = NULL
3. NAME = name, STATE = NULL, CITY = NULL
4. NAME = NULL, STATE = NULL, CITY = NULL
Means if in a row where all attributes matches – it should be selected, if we do not have that kind of data we should go to next best option like select state and city as NULL, etc.
My code as below, is giving me correct results but I need a more efficient way.
private static BigDecimal getsalaryForBestMatch(ResultSet results, EmployeeRq request) throws Exception{
BigDecimal salary = null;
BigDecimal salaryWithState = null;
BigDecimal salaryWithName = null;
BigDecimal salaryWithNoMatch = null;
while (results.next()) {
String billerName = results.getString("EMP_NAME") != null ? results.getString("EMP_NAME").trim() : null;
String city = results.getString("CITY") != null ? results.getString("CITY").trim() : null;
String state = results.getString("STATE") != null ? results.getString("STATE").trim() : null;
BigDecimal salaryRslt = null;
if(results.getString("SALARY") != null){
salaryRslt = BigDecimal.valueOf(results.getDouble("SALARY"));
}
if(billerName != null && !billerName.equals("") && billerName.equals(request.getBillPaymentsalaryCalculateInfo().getBillerName())){
if(city != null && !city.equals("") && city.equals(request.getMsgRqHdr().getCity()) &&
state != null && !state.equals("") && state.equalsIgnoreCase(request.getMsgRqHdr().getstate())){
salary = salaryRslt;
break;
} else if((city == null || city.equals("")) && state != null && !state.equals("") &&
state.equalsIgnoreCase(request.getMsgRqHdr().getState())){
salaryWithState = salaryRslt;
} else if((city == null || city.equals("")) && (state == null || state.equals(""))){
salaryWithName = salaryRslt;
}
} else if((billerName == null || billerName.equals("")) && (city == null || city.equals("")) &&
(state == null || state.equals(""))){
salaryWithNoMatch = salaryRslt;
}
}
if(salary != null){
return salary;
} else if(salaryWithState != null){
salary = salaryWithState;
} else if(salaryWithName != null){
salary = salaryWithName;
} else if(salaryWithNoMatch != null){
salary = salaryWithNoMatch;
}
return salary;
}
EDIT: I dont want to use 3 extra variables: salaryWithState, salaryWithName, salaryWithNoMatch.

I want just to give the general idea how this can be implemented, so I haven't actually tested and checked if it will give you the right salary.
public BigDecimal getSalaryForBestMatch(ResultSet resultSet, PaymentSalaryInfo paymentSalaryInfo) {
Map<String, Supplier<String>> m1 = new HashMap<>();
m1.put("EMP_NAME", paymentSalaryInfo::getBillerName);
m1.put("STATE", paymentSalaryInfo::getState);
m1.put("CITY", paymentSalaryInfo::getCity);
Map<String, Supplier<String>> m2 = new HashMap<>();
m2.put("STATE", paymentSalaryInfo::getState);
m2.put("CITY", paymentSalaryInfo::getCity);
Map<String, Supplier<String>> m3 = new HashMap<>();
m3.put("CITY", paymentSalaryInfo::getCity);
Optional<String> salary = Optional.empty();
while(resultSet.next() && !salary.isPresent()) {
salary = apply(m1, resultSet);
//check salary and then apply(m2, resultSet) ....
}
return salary.isPresent() ? new BigDecimal(salary.get()) : null;
}
public Optional<String> apply(Map<String, Supplier<String>> filter, ResultSet resultSet) {
boolean allMatch = filter.entrySet().stream().allMatch(entry -> {
String value = resultSet.getString(entry.getKey());
return value != null && value.equals(entry.getValue().get());
});
return allMatch ? Optional.of(resultSet.getString("salary")) : Optional.empty();
}

I have written the same logic in a different way with using arrays. If your environment can afford to use arrays, you can use this code. But I have not tested the code.
private static BigDecimal getsalaryForBestMatch(ResultSet results, EmployeeRq request) throws Exception{
BigDecimal salary = null;
int matchCount = 0;
String rBillerName = request.getBillPaymentsalaryCalculateInfo().getBillerName();
String rCity = request.getMsgRqHdr().getCity();
String rState = request.getMsgRqHdr().getstate();
String [] truthArray = new String[] {rBillerName, rCity, rState};
while (results.next()) {
String billerName = results.getString("EMP_NAME") != null ? results.getString("EMP_NAME").trim() : null;
String city = results.getString("CITY") != null ? results.getString("CITY").trim() : null;
String state = results.getString("STATE") != null ? results.getString("STATE").trim() : null;
BigDecimal salaryRslt = results.getString("SALARY") != null ? BigDecimal.valueOf(results.getDouble("SALARY")): null;
String [] testArray = new String[] {billerName, city, state};
int localMatchCount = 0;
for(int i = 0; i < testArray.length; i++) {
if(testArray[i] != null && testArray[i].equals(truthArray[i]))
localMatchCount++;
else {
break;
}
}
if(localMatchCount >= matchCount){
matchCount = localMatchCount;
salary = salaryRslt;
}
}
return salary;
}

Related

Method refactoring to avoid to many npe checks

I have created the following method which returns a Triple of strings. However, I don't like the way I've written it because I think I've put in too many Npe checks making it unreadable.
private Triplet<String, String, String> getInfoFromTable(Person person) {
StringBuilder idWithText = new StringBuilder();
String idText;
Date time = null;
Level level;
Exercise exerciseRecord = getExercise(person);
if (exerciseRecord != null && exerciseRecord.getId() != null) {
if(exerciseRecord.getLevel1() != null && exerciseRecord.getLevel2() != null){
level = new Level(exerciseRecord.getLevel1(), exerciseRecord.getLevel2());
} else {
level = new Level("1", "1");
}
idText = getIdText(level, exerciseRecord.getId());
if(!Strings.isNullOrEmpty(idText)) {
idWithText = idWithText.append(exerciseRecord.getId()).append(" " + idText);
}
if (exerciseRecord.getTime() != null) {
time = exerciseRecord.getTime().toDate();
}
return new Triplet<>(idWithText.toString(), "1", formatTime(time));
}
return new Triplet<>("", "", "");
}
Ηow can I make the above code look simpler? I've seen a little use of Optional but I don't know if it's good to use them in my case. Could someone help with the method refactor?
You need to split the huge method into several simple, it will decrease complexity.
private Triplet<String, String, String> getInfoFromTable(Person person) {
Exercise exerciseRecord = getExercise(person);
if (exerciseRecord != null && exerciseRecord.getId() != null) {
return new Triplet<>(getIdWithText(exerciseRecord, getLevel(exerciseRecord)), "1", formatTime(exerciseRecord.getTime()));
}
return new Triplet<>("", "", "");
}
private String formatTime(LocalTime time) {
if (time == null) {
return "";
}
return formatTime(time.toDate());
}
private Level getLevel(Exercise exerciseRecord) {
Level level;
if(exerciseRecord.getLevel1() != null && exerciseRecord.getLevel2() != null){
level = new Level(exerciseRecord.getLevel1(), exerciseRecord.getLevel2());
} else {
level = new Level("1", "1");
}
return level;
}
private String getIdWithText(Exercise exerciseRecord, Level level) {
String idWithText = "";
String idText = getIdText(level, exerciseRecord.getId());
if(!Strings.isNullOrEmpty(idText)) {
idWithText = String.format("%s %s", exerciseRecord.getId(), idText);
}
return idWithText;
}

how to avoid multiple if-else statements for vaildation in Java

I have lots of multiple if-else statements. For code optimization, I need to write one function for all if else logic. As of now my code structure is in below.
input request is in JSONObject(org.json.simple.JSONObject), which have more than 10 values.
String s = (String) inputObj.get("test");
String s1 = (String) inputObj.get("test");
String s2 = (String) inputObj.get("test");
String s3 = (String) inputObj.get("test");
if (s != null && s.trim().isEmpty()) {
if (s1 != null && s1.trim().isEmpty()) {
if (s2 != null && s2.trim().isEmpty()) {
if (s3 != null && s3.trim().isEmpty()) {
if (s4 != null && s4.trim().isEmpty()) {
........
} else {
return;
}
} else {
return;
}
} else {
return;
}
} else {
return;
}
} else {
return;
}
How to avoid this kind of looping and throw an error message in common method.
Advance thanks.
Consider adding all your strings to array or ArrayList of string, and looping thru each entry in it, and check them for null or emptiness.
You can try this.
void main() {
List<String> sList = new ArrayList<>();
sList.add(inputObj.get("test"));
sList.add(inputObj.get("test"));
sList.add(inputObj.get("test"));
sList.add(inputObj.get("test"));
for(String s : sList){
try {
checkString(s);
}catch (Exception e){
//log or print the exception, however you like
}
}
}
void checkString(String s) throws Exception{
if(s!= null && !s.trim().isEmpty()){
//doStuff
}else{
throw new Exception("String is null or empty !!!");
}
}
You should also check this out.
public class YourClass{
private boolean isBlankDataPresent(JSONObject inputObj, String[] keys) throws Exception {
for (String key : keys) {
String input = (String) inputObj.get(key);
if( input == null || input.trim().isEmpty())
throw new Exception(key +" is Empty");
}
return false;
}
public boolean validateData(JSONObject inputObj, String[] keys) throws Exception {
boolean isBlankDataPresent= isBlankDataPresent(inputObj, keys);
if (!isBlankDataPresent) {
// do Your Stuff and return true
}
}
}
public Integer checkIsEmapty(String checkingString){
if(checkingString != null && !checkingString.trim().isEmpty()){
return 1;
}
return 0;
}
public String method(){
String s ="";
String s1 = "hi";
String s2 = "java";
String s3 = null;
String s4 = null;
Integer s1i = checkIsEmapty(s);
Integer s2i = checkIsEmapty(s1);
Integer s3i = checkIsEmapty(s2);
Integer s4i = checkIsEmapty(s3);
Integer s5i = checkIsEmapty(s4);
Integer total = s1i + s2i + s3i + s4i + s5i;
switch (total){
case 1 :
// To DO
case 2 :
// To DO
}
}
in switch used to checking the value, U can pass binary and Integer also
Like #Emre Acre mentioned,
List<String> sList = new ArrayList<>();
sList.add(inputObj.get("test"));
sList.add(inputObj.get("test"));
sList.add(inputObj.get("test"));
sList.add(inputObj.get("test"));
boolean allDataValid = sList
.stream()
.allMatch(s -> s != null && s.trim().isEmpty());
if(allDataValid) {
......
} else {
return;
}

Collections comparator with null at the end

I have some User objects that can have name and email, I want them sorted by name , then by email, but firstly show users that have not null names. I was trying like so:
Collections.sort(usersList, new Comparator<User>() {
#Override
public int compare(User obj1, User obj2) {
int res = 0;
//to make sure null names at the end
if (obj1.getFullName() == null && obj2.getFullName() == null)
res = 0;
if (obj1.getFullName() == null)
res = 1;
if (obj2.getFullName() == null)
res = -1;
//logic for name sorting then email sorting
if (obj1.getFullName() != null && obj2.getFullName() != null)
res = obj1.getFullName().compareTo(obj2.getFullName());
if (res == 0)
res = obj1.getEmail().compareTo(obj2.getEmail());
return res;
}
});
As result I get names first but sorted by email not by name.
This can be easily done with Java-8's Comparator utilities:
List<User> users = Arrays.asList(new User("pete", "pete#email"), new User(null, null), new User("alex", null), new User(null, "email"));
Collections.sort(users, Comparator.comparing(User::getName, Comparator.nullsLast(Comparator.naturalOrder()))
.thenComparing(Comparator.comparing(User::getEmail, Comparator.nullsLast(Comparator.naturalOrder()))));
System.out.println(users);
Output:
[
User{name='alex', email='null'},
User{name='pete', email='pete#email'},
User{name='null', email='email'},
User{name='null', email='null'}
]

How to apply composite BiPredicate in anyMatch() java 8 streams

I have two Lists. One I created from Database and one from Csv file. Now I want to collect records in a list that contain both in database and csv file. I wrote code like below
BiPredicate<Trainee, Trainee> sameTrainee = (dbTrainee, csvTrainee) -> {
String dbTraineeFirstName = dbTrainee.getFirstName();
String dbTraineeLastName = dbTrainee.getLastName();
String dbTraineeEmail = dbTrainee.getEmail();
LocalDateTime dbTraineeCompletionDate = dbTrainee.getSessionDateTime();
String text = dbTraineeCompletionDate.format(DATE_TIME_FORMATTER);
LocalDateTime dbTraineeSessionDateTime = LocalDateTime.parse(text);
String dbTraineePhoneNumber = dbTrainee.getPhoneNumber();
String dbTraineeSsn = dbTrainee.getSocialSecurityLastFour();
String dbTraineeStreetOne = dbTrainee.getStreetOne();
String dbTraineeCity = dbTrainee.getCity();
String csvTraineeFirstName = csvTrainee.getFirstName();
String csvTraineeLastName = csvTrainee.getLastName();
String csvTraineeEmail = csvTrainee.getEmail();
LocalDateTime csvTraineeSessionDateTime = csvTrainee.getSessionDateTime();
String csvTraineePhoneNumber = csvTrainee.getPhoneNumber();
String csvTraineeSsn = csvTrainee.getSocialSecurityLastFour();
String csvTraineeStreetOne = csvTrainee.getStreetOne();
String csvTraineeCity = csvTrainee.getCity();
int dbTraineeSsnLength = dbTraineeSsn.length();
int csvTraineeSsnLength = csvTraineeSsn.length();
if (dbTraineeSsnLength != csvTraineeSsnLength) {
if (dbTraineeSsnLength == 4 && dbTraineeSsn.startsWith("0")) {
String dbTraineeSsnLast3Digits = dbTraineeSsn.substring(dbTraineeSsn.length() - 3);
if (csvTraineeSsnLength == 3 && csvTraineeSsn.endsWith(dbTraineeSsnLast3Digits)) {
csvTraineeSsn = "0" + csvTraineeSsn;
}
}
}
return dbTraineeFirstName.equals(csvTraineeFirstName)
&& dbTraineeLastName.equals(csvTraineeLastName)
&& dbTraineeEmail.equals(csvTraineeEmail)
&& dbTraineeSessionDateTime.equals(csvTraineeSessionDateTime)
&& dbTraineePhoneNumber.equals(csvTraineePhoneNumber)
&& dbTraineeSsn.equals(csvTraineeSsn)
&& dbTraineeStreetOne.equals(csvTraineeStreetOne)
&& dbTraineeCity.equals(csvTraineeCity);
};
and called it like
List<Trainee> foundInBothList = dbMonthlyTraineeList.stream()
.filter(dbTrainee -> csvTraineeList.stream()
.anyMatch(csvTrainee -> {
return sameTrainee.test(dbTrainee, csvTrainee);
})
).collect(Collectors.toList());
List<Trainee> notInFileList = dbMonthlyTraineeList.stream()
.filter(dbTrainee -> csvTraineeList.stream()
.noneMatch(csvTrainee -> {
return sameTrainee.test(dbTrainee, csvTrainee);
})
).collect(Collectors.toList());
It works fine. But as my BiPredicate is getting long and untidy. So I made a class and collect all the predicates in a Collection like below
public class PlcbMonthlyReportStatisticsBiPredicates {
public static BiPredicate<Trainee, Trainee> isValidFirstName() {
return (dbTrainee, csvTrainee) -> {
String dbTraineeFirstName = dbTrainee.getFirstName();
String csvTraineeFirstName = csvTrainee.getFirstName();
return dbTraineeFirstName.equals(csvTraineeFirstName);
};
}
public static BiPredicate<Trainee, Trainee> isValidSsn() {
return (dbTrainee, csvTrainee) -> {
String dbTraineeSsn = dbTrainee.getSocialSecurityLastFour();
String csvTraineeSsn = csvTrainee.getSocialSecurityLastFour();
...
return dbTraineeSsn.equals(csvTraineeSsn);
};
}
....
public static List<BiPredicate<Trainee, Trainee>> getAllBiPredicates() {
List<BiPredicate<Trainee, Trainee>> allPredicates = Arrays.asList(
isValidFirstName(),
isValidSsn(),
...
);
return allPredicates;
}
}
Now I Collect all the predicates but how can I apply these predicates in my anyMatch() and noneMatch(). I tried this but of-cources getting error
List<Trainee> foundInBothList1 = dbMonthlyTraineeList.stream()
.filter(dbTrainee -> csvTraineeList.stream()
.anyMatch(csvTrainee -> {
List<BiPredicate<Trainee, Trainee>> allBiPredicates = getAllBiPredicates();
return allBiPredicates.stream().reduce(BiPredicate::and).orElse((x,y)->true); //error
})
).collect(Collectors.toList());
How can I apply this. Is my approach is right?
**Edit
#Entity
public class Trainee {
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
private LocalDateTime sessionDateTime;
private String firstName;
....
#Override
public boolean equals(Object otherObject) {
// Are the same?
if (this == otherObject) {
return true;
}
// Is otherObject a null reference?
if (otherObject == null) {
return false;
}
// Do they belong to the same class?
if (this.getClass() != otherObject.getClass()) {
return false;
}
// Get the reference of otherObject in a otherTrainee variable
Trainee otherTrainee = (Trainee)otherObject;
LocalDateTime dbTraineeCompletionDate = this.getSessionDateTime();
String text = dbTraineeCompletionDate.format(DATE_TIME_FORMATTER);
LocalDateTime dbTraineeSessionDateTime = LocalDateTime.parse(text);
String dbTraineeSsn = this.socialSecurityLastFour;
String csvTraineeSsn = otherTrainee.getSocialSecurityLastFour();
int dbTraineeSsnLength = dbTraineeSsn.length();
int csvTraineeSsnLength = csvTraineeSsn.length();
if (dbTraineeSsnLength != csvTraineeSsnLength) {
if (dbTraineeSsnLength == 4 && dbTraineeSsn.startsWith("0")) {
String dbTraineeSsnLast3Digits = dbTraineeSsn.substring(dbTraineeSsn.length() - 3);
if (csvTraineeSsnLength == 3 && csvTraineeSsn.endsWith(dbTraineeSsnLast3Digits)) {
csvTraineeSsn = "0" + csvTraineeSsn;
}
}
}
boolean isEqual = (this.firstName.equals(otherTrainee.firstName)
&& this.lastName.equals(otherTrainee.lastName)
&& this.email.equals(otherTrainee.email)
&& dbTraineeSessionDateTime.equals(otherTrainee.sessionDateTime)
&& this.phoneNumber.equals(otherTrainee.phoneNumber)
&& dbTraineeSsn.equals(csvTraineeSsn)
&& this.streetOne.equals(otherTrainee.streetOne)
&& this.city.equals(otherTrainee.city)
);
return isEqual;
}
#Override
public int hashCode() {
int hash = 37;
int code = 0;
code = (firstName == null ? 0 : firstName.hashCode());
hash = hash * 59 + code;
code = (lastName == null ? 0 : lastName.hashCode());
hash = hash * 59 + code;
code = (email == null ? 0 : email.hashCode());
hash = hash * 59 + code;
code = (sessionDateTime == null ? 0 : sessionDateTime.hashCode());
hash = hash * 59 + code;
code = (phoneNumber == null ? 0 : phoneNumber.hashCode());
hash = hash * 59 + code;
code = (socialSecurityLastFour == null ? 0 : socialSecurityLastFour.hashCode());
hash = hash * 59 + code;
code = (streetOne == null ? 0 : streetOne.hashCode());
hash = hash * 59 + code;
code = (city == null ? 0 : city.hashCode());
hash = hash * 59 + code;
return hash;
}
}
Edit 2 (After overridng hascode() and equals())
-------------------------------------------------
Found in both:
List<Trainee> foundInBothList1 = dbMonthlyTraineeList.stream()
.filter(dbTrainee -> csvTraineeList.stream()
.anyMatch(csvTrainee -> {
return allBiPredicates.stream().reduce(BiPredicate::and).orElse((x,y)->true).test(dbTrainee, csvTrainee);
})
).collect(Collectors.toList());
List<Trainee> foundInBothList = new ArrayList<>(dbMonthlyTraineeList);
//foundInBothList.retainAll(new HashSet<>(csvTraineeList));
foundInBothList.retainAll(csvTraineeList);
Found in database but not in CSV
List<Trainee> notInCsvFileList1 = dbMonthlyTraineeList.stream()
.filter(dbTrainee -> csvTraineeList.stream()
.noneMatch(csvTrainee -> {
return allBiPredicates.stream().reduce(BiPredicate::and).orElse((x,y)->true).test(dbTrainee, csvTrainee);
})
).collect(Collectors.toList());
//find out that elements of dbMonthlyTraineeList which is not present in arraylist(csvTraineeList).
List<Trainee> notInCsvFileList = new ArrayList<>(dbMonthlyTraineeList);
notInCsvFileList.removeAll(csvTraineeList);
It looks like you are over-thinking this. Why not just override equals with your sameTrainee bi-predicate code? (Don't forget to override hashCode also).
After you do this, you can keep the Trainees that are in both lists using:
Set<Trainee> foundInBothList = new HashSet<>(dbMonthlyTraineeList);
foundInBothList.retainAll(new HashSet<>(csvTraineeList));
This solution is O(n) and so it will perform a lot better than your solution, which is O(n²). This is because the contains operation is constant-time on a Set.
But if you really want your code to compile, you just need to call the test method where you have the error:
return allBiPredicates.stream().reduce(BiPredicate::and)
.orElse((x,y)->true)
.test(dbTrainee, csvTrainee);

Comparison method violates its general contract and method compareTo

I have a class Contact with fields firstName, lastName and emails. I need to sort them using Collection.sort(...), but I got an exception:
java.lang.IllegalArgumentException: Comparison method violates its general contract!
My compareTo method:
#Override
public int compareTo(Contact another) {
int compareFirstName = 0;
if (this.getFirstName() != null && another.getFirstName() != null) {
compareFirstName = this.getFirstName().compareToIgnoreCase(
another.getFirstName());
if (compareFirstName == 0) {
int compareLastName = 0;
if (this.getLastName() != null && another.getLastName() != null) {
compareLastName = this.getLastName().compareToIgnoreCase(
another.getLastName());
if (compareLastName == 0) {
int compareEmail = 0;
if (this.getEmails() != null
&& another.getEmails() != null) {
compareEmail = this.getEmails()
.compareToIgnoreCase(another.getEmails());
return compareEmail;
} else {
return 0;
}
} else {
return compareLastName;
}
} else {
int compareEmail = 0;
if (this.getEmails() != null && another.getEmails() != null) {
compareEmail = this.getEmails().compareToIgnoreCase(
another.getEmails());
return compareEmail;
} else {
return 0;
}
}
} else {
return compareFirstName;
}
} else {
int compareLastName = 0;
if (this.getLastName() != null && another.getLastName() != null) {
compareLastName = this.getLastName().compareToIgnoreCase(
another.getLastName());
if (compareLastName == 0) {
int compareEmail = 0;
if (this.getEmails() != null && another.getEmails() != null) {
compareEmail = this.getEmails().compareToIgnoreCase(
another.getEmails());
return compareEmail;
} else {
return 0;
}
} else {
return compareLastName;
}
} else {
int compareEmail = 0;
if (this.getEmails() != null && another.getEmails() != null) {
compareEmail = this.getEmails().compareToIgnoreCase(
another.getEmails());
return compareEmail;
} else {
return 0;
}
}
}
}
Please help me to find error in my compareTo method. Thanks.
Your implementation does violate the contract.
Suppose you have 3 Contacts :
contact1 : First Name = "John", Last Name = "Doe", Email = "x#gmail.com"
contact2 : First Name = "John", Last Name = "Doe", Email = null
contact3 : First Name = "John", Last Name = null, Email = "y#gmail.com"
Based on your logic :
contact1.compareTo(contact2) returns 0 (since they have the same first and last name).
contact2.compareTo(contact3) also returns 0 (since you only compare by first name).
But contact1.compareTo(contact3) doesn't return 0 (since they have different emails).
compareTo must be transitive.
The way to fix this is not to ignore a property that is null only in one of the contacts you are comparing. For example, if this.getLastName()==null && another.getLastName() != null, return 1 (assuming you want to order the null last names after the non-null last names).

Categories

Resources