Best way to organize this logic? - function

I have a function with two parameters A and B which can both be null or not. I am looking for the most concise way of writing this function. The following is the rules for it.
case A == null, B == null : do nothing
case A == null, B != null : create new instance of A and call A.doStuff(B)
case A != null, B != null : call A.doStuff(B)
case A != null, B == null : call A.dostuff(B)
This is what I have come up with:
public void myFunction( MyObject A, MyObject b )
{
if (A == null)
{
if (B != null)
{
A = new A();
}
else
{
return;
}
}
A.doStuff( B );
}

Well, I think it is a matter of your personal preference. Honestly, I would write it like this:
public void myFunction( MyObject A, MyObject b ) {
if (A == null && B == null) return;
if (A == null) A = new A();
A.doStuff(b);
}

public void myFunction( MyObject A, MyObject b ) {
if (A){
A.doStuff(b);
}else if (B){
A = new A();
A.doStuff(b)
}
}
This would be how I would write it

Related

Junit How to assert Or Condition SQL Query

Or Condition Query Example
SELECT ID, NAME, CREATE_DATE, UPDATE_DATE
FROM USER
WHERE ( CREATE_DATE > SYSDATE-1 OR UPDATE_DATE > SYSDATE -1)
How to assert OR Condition Query with junit?
// this test is fail : createDate is "2022-07-28" || updateDate is null
String oneDaysAgo = "2022-07-27";
List<UserInfo> list = jpaRepository.findAllByCreateDateGreaterThanOrUpdateDateGreaterThan(oneDaysAgo,oneDaysAgo);
for (UserInfo vo : list) {
assertThat(vo.getCreatedDate(), is(greaterThan(oneDaysAgo)));
assertThat(vo.getUpdateDate(), is(greaterThan(oneDaysAgo)));
log.info(vo.toString());
}
Expect assert
( createDate > oneDaysAgo ) or ( updateDate > oneDaysAgo )
OneDaysAgo = "2022-07-27";
Create Date
Update Date
Expect Result
Actual Result
null or lessThan or equal
null or lessThan or equal
False
False
"2022-07-28"
null or lessThan or equal
True
False
null or lessThan or equal
"2022-07-28"
True
False
"2022-07-28"
"2022-07-28"
True
True
thx for comment #pL4Gu33
Solve problem with Add Helper Method
#Test
void test_orCondition() {
String oneDaysAgo = "2022-07-27";
List<UserVo> list = getUserVoList(oneDaysAgo);
assertThat(list.size(), is(greaterThan(0)));
for (UserVo vo : list) {
assertThat(
getOrConditionBoolean(LocalDate.parse(oneDaysAgo)
, vo.getCreateDate(), vo.getUpdateDate()), is(true));
}
}
private Boolean getOrConditionBoolean(LocalDate baseDate, LocalDate arg1, LocalDate arg2) {
if (checkDate(arg1, baseDate)) {
return true;
}
if (checkDate(arg2, baseDate)) {
return true;
}
return false;
}
private boolean checkDate(LocalDate argDate, LocalDate baseDate) {
if (argDate == null) {
return false;
}
if (baseDate.isEqual(argDate) || baseDate.isBefore(argDate)) {
return true;
}
return false;
}

How set variable with condition with immutable.js

This code in typescript:
const b
if (a == true) {
b = [1]
} else {
b = [2]
}
how to convert above code to immutable.js ?
You have two options:
let b
if (a) {
b = List([1])
} else {
b = List([2])
}
//or
const b = a ? List([1]) : List([2])

Add where clause if not NULL

here is my query:
List<string> kwList = GetFilterKeywords(); // returns NULL none keyword selected
var res = from d in ctx.Books
where (kwList == null || kwList.Contains(d.Name))
select d;
Looks like it is not legit to add where clause if kwList is NULL. So my question is: Is there any way to add more where clauses to the same query in IF/ELSE IF construction?
I mean:
var res = from d in ctx.Books
select d;
if (kwList != null)
{
res.Where(d => kwList.Contains(d.Name);
}
var res = ctx.Books; // no need to write select
if (kwList != null)
res = res.Where(x => kwList.Contains(x.Name));
foreach (d in res) {
...
}
You can use the tertiary operator
var res = kwList == null ? ctx.Books : ctx.Books.Where(x => kwList.Contains(x.Name));
If you want to modify the initial linq query in subsequent case statements, make sure to reassign the initial query to the modified:
var res = ctx.Books;
if (a == b)
{
// reassign here
res = res.Where(x => kwList.Contains(x.Name));
}
else if (a == c)
res = res.Where(x => x.Id == y);

Jackson bug (or feature!?) when using java.util.Set - mySet.size() is always 1

I am using Jackson 2.2.0 and Spring 3.2.0 with Hibernate 4.2.2.
I recently had to send an array of objects via POST to the server:
{"cancelationDate":"2013-06-05",
"positions":[
{"price":"EUR 12.00",
"count":1},
{"price":"EUR 99.00",
"count":1}
]
}
My classes look like this:
public class Bill extends {
LocalDate cancelationDate;
Set<Position> positions;
...
}
and:
public class Position {
Integer count;
BigMoney price;
#JsonIgnore
Bill bill;
...
}
When I call bill.getPositions().size() it tells me 1.
If I use List<Position> instead of Set<Position> it works nice. So what's the problem with Set?
Thank you :)
equals and hashCode:
public int hashCode() {
final int prime = 31;
int result = 1;
result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
return result;
}
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof Position))
return false;
Position equalCheck = (Position) obj;
if ((id == null && equalCheck.id != null) || (id != null && equalCheck.id == null))
return false;
if (id != null && !id.equals(equalCheck.id))
return false;
return true;
}
Since id is null for the Jackson deserialized Positions, hashCode returns the same value for the different objects, and equals returns true. A Set cannot contain to elements which are equal. Fix your equals/hashcode implentation and everything will work as it should.
Suggested new hashCode/equals:
public int hashCode() {
final int prime = 31;
int result = 1;
result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
result = (int) (prime * result + ((price== null) ? 0 : price.hashCode()));
return result;
}
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof Position))
return false;
Position equalCheck = (Position) obj;
if ((id == null && equalCheck.id != null) || (id != null && equalCheck.id == null))
return false;
if (id != null && !id.equals(equalCheck.id))
return false;
if ((price== null && equalCheck.price != null) || (price != null && equalCheck.price == null))
return false;
if (price!= null && !price.equals(equalCheck.idprice)
return false;
return true;
}
the Set use equals and hashcode methods when it insert a row, have you overriden them?
I had also some bug with jackson 2.2.1 (with Maps) , you should upgrade to jackson 2.2.2

LINQ2SQL A problem with return items based on a parameter

I occured a strange problem. I have that method
public static void ProcessCategories(int? myID)
{
var tmpList = Adapter.Category.Where(x => x.IdParentCategory == myID).ToList();
}
when myID == null (the parameter), the tmpList doesn't contain any elements, but if I type
x.IdParentCategory == null then some items are returned. Why ?
Try this:
public static void ProcessCategories(int? myID)
{
var tmpList = Adapter.Category.Where(x => x.IdParentCategory == myID || (myID == null && x.IdParentCategory == null)).ToList();
}