Check which combinations of parameters are null in Java
I am new to Java. I am facing an issue now in which I couldn't find the easiest and cleanest way of solving it.
Suppose I have 3 parameters(string) passed to a function(could be a Hashmap too).I want to check if individual variable or combination of variables is not Null and act accordingly.
For example one way to do this is using if-else this way
if(a!=null && b == null && c == null) {
//doSomething
}
else if(a==null && b!= null && c == null ) {
//doSomething
}
else if(a==null && b0= null && c != null) {
//doSomething
}
......
//Similarly combination of two variables
if(a!=null && b != null && c == null) {
//doSomething
}
else if(a!=null && b== null && c != null) {
//doSomething
}
else if(a==null && b!= null && c != null) {
//doSomething
}
......
//and so on
//Similarly combination of three variables
if(a!=null && b != null && c != null) {
//doSomething
}
....
How to achieve this kind of situation. I found similar question, but didn't make the code clean. Any help will be appreciated
java if-statement conditional
add a comment |
I am new to Java. I am facing an issue now in which I couldn't find the easiest and cleanest way of solving it.
Suppose I have 3 parameters(string) passed to a function(could be a Hashmap too).I want to check if individual variable or combination of variables is not Null and act accordingly.
For example one way to do this is using if-else this way
if(a!=null && b == null && c == null) {
//doSomething
}
else if(a==null && b!= null && c == null ) {
//doSomething
}
else if(a==null && b0= null && c != null) {
//doSomething
}
......
//Similarly combination of two variables
if(a!=null && b != null && c == null) {
//doSomething
}
else if(a!=null && b== null && c != null) {
//doSomething
}
else if(a==null && b!= null && c != null) {
//doSomething
}
......
//and so on
//Similarly combination of three variables
if(a!=null && b != null && c != null) {
//doSomething
}
....
How to achieve this kind of situation. I found similar question, but didn't make the code clean. Any help will be appreciated
java if-statement conditional
2
The code you have linked is exactly what I would do. Where is the problem there?
– Silvan
Nov 23 '18 at 10:31
I found similar question, but didn't make the code clean - Can you be more specific, so that we can adapt it to be "cleaner" ?
– Benoit
Nov 23 '18 at 10:36
There are in total 8 cases. If the action for each of the 8 cases is a different one than the other 7, then your way is the only way.
– forpas
Nov 23 '18 at 11:14
Thanks all. I was looking if there is any possibilities to make it more elegant and clean code. Because I personally felt code smell ;). As everyone said I cant avoid using n conditional statements, I would like to take @khalid ans as almost correct
– Adrin Johnson
Nov 23 '18 at 13:15
add a comment |
I am new to Java. I am facing an issue now in which I couldn't find the easiest and cleanest way of solving it.
Suppose I have 3 parameters(string) passed to a function(could be a Hashmap too).I want to check if individual variable or combination of variables is not Null and act accordingly.
For example one way to do this is using if-else this way
if(a!=null && b == null && c == null) {
//doSomething
}
else if(a==null && b!= null && c == null ) {
//doSomething
}
else if(a==null && b0= null && c != null) {
//doSomething
}
......
//Similarly combination of two variables
if(a!=null && b != null && c == null) {
//doSomething
}
else if(a!=null && b== null && c != null) {
//doSomething
}
else if(a==null && b!= null && c != null) {
//doSomething
}
......
//and so on
//Similarly combination of three variables
if(a!=null && b != null && c != null) {
//doSomething
}
....
How to achieve this kind of situation. I found similar question, but didn't make the code clean. Any help will be appreciated
java if-statement conditional
I am new to Java. I am facing an issue now in which I couldn't find the easiest and cleanest way of solving it.
Suppose I have 3 parameters(string) passed to a function(could be a Hashmap too).I want to check if individual variable or combination of variables is not Null and act accordingly.
For example one way to do this is using if-else this way
if(a!=null && b == null && c == null) {
//doSomething
}
else if(a==null && b!= null && c == null ) {
//doSomething
}
else if(a==null && b0= null && c != null) {
//doSomething
}
......
//Similarly combination of two variables
if(a!=null && b != null && c == null) {
//doSomething
}
else if(a!=null && b== null && c != null) {
//doSomething
}
else if(a==null && b!= null && c != null) {
//doSomething
}
......
//and so on
//Similarly combination of three variables
if(a!=null && b != null && c != null) {
//doSomething
}
....
How to achieve this kind of situation. I found similar question, but didn't make the code clean. Any help will be appreciated
java if-statement conditional
java if-statement conditional
asked Nov 23 '18 at 10:28
Adrin JohnsonAdrin Johnson
5618
5618
2
The code you have linked is exactly what I would do. Where is the problem there?
– Silvan
Nov 23 '18 at 10:31
I found similar question, but didn't make the code clean - Can you be more specific, so that we can adapt it to be "cleaner" ?
– Benoit
Nov 23 '18 at 10:36
There are in total 8 cases. If the action for each of the 8 cases is a different one than the other 7, then your way is the only way.
– forpas
Nov 23 '18 at 11:14
Thanks all. I was looking if there is any possibilities to make it more elegant and clean code. Because I personally felt code smell ;). As everyone said I cant avoid using n conditional statements, I would like to take @khalid ans as almost correct
– Adrin Johnson
Nov 23 '18 at 13:15
add a comment |
2
The code you have linked is exactly what I would do. Where is the problem there?
– Silvan
Nov 23 '18 at 10:31
I found similar question, but didn't make the code clean - Can you be more specific, so that we can adapt it to be "cleaner" ?
– Benoit
Nov 23 '18 at 10:36
There are in total 8 cases. If the action for each of the 8 cases is a different one than the other 7, then your way is the only way.
– forpas
Nov 23 '18 at 11:14
Thanks all. I was looking if there is any possibilities to make it more elegant and clean code. Because I personally felt code smell ;). As everyone said I cant avoid using n conditional statements, I would like to take @khalid ans as almost correct
– Adrin Johnson
Nov 23 '18 at 13:15
2
2
The code you have linked is exactly what I would do. Where is the problem there?
– Silvan
Nov 23 '18 at 10:31
The code you have linked is exactly what I would do. Where is the problem there?
– Silvan
Nov 23 '18 at 10:31
I found similar question, but didn't make the code clean - Can you be more specific, so that we can adapt it to be "cleaner" ?
– Benoit
Nov 23 '18 at 10:36
I found similar question, but didn't make the code clean - Can you be more specific, so that we can adapt it to be "cleaner" ?
– Benoit
Nov 23 '18 at 10:36
There are in total 8 cases. If the action for each of the 8 cases is a different one than the other 7, then your way is the only way.
– forpas
Nov 23 '18 at 11:14
There are in total 8 cases. If the action for each of the 8 cases is a different one than the other 7, then your way is the only way.
– forpas
Nov 23 '18 at 11:14
Thanks all. I was looking if there is any possibilities to make it more elegant and clean code. Because I personally felt code smell ;). As everyone said I cant avoid using n conditional statements, I would like to take @khalid ans as almost correct
– Adrin Johnson
Nov 23 '18 at 13:15
Thanks all. I was looking if there is any possibilities to make it more elegant and clean code. Because I personally felt code smell ;). As everyone said I cant avoid using n conditional statements, I would like to take @khalid ans as almost correct
– Adrin Johnson
Nov 23 '18 at 13:15
add a comment |
5 Answers
5
active
oldest
votes
Write these utility functions and you can compare n terms easily.
public static boolean isAllNull(Object... objects) {
return Stream.of(objects).allMatch(Objects::isNull);
}
public static boolean isAllNotNull(Object... objects) {
return Stream.of(objects).allMatch(Objects::nonNull);
}
you can use these functions for n comparisons.
if(isAllNotNull(a) && isAllNull(b,c)) {
//doSomething
}
else if(isAllNotNull(b) && isAllNull(a,c)) {
//doSomething
}
else if(isAllNotNull(c) && isAllNull(b,a)) {
//doSomething
}
2
I like this approach. I would rewriteisAllNull()this way: ` public static boolean isAllNull(Object... objects) { return Stream.of(objects).allMatch(Objects::isNull); }`
– Benoit
Nov 23 '18 at 10:43
@Benoit ok i'll update it. thanks :)
– Khalid Shah
Nov 23 '18 at 10:44
Thus now the first methodisNull()is no more necessary ;-)
– Benoit
Nov 23 '18 at 11:00
@Benoit yeah now that is no mre needed.
– Khalid Shah
Nov 23 '18 at 11:03
@KhalidShah Looks more readable. But I still have a question. `if(isAllNotNull(a) && isAllNull(b,c)) { //doSomething } else if(isAllNotNull(b) && isAllNull(a,c)) { //doSomething } else if(isAllNotNull(c) && isAllNull(b,a)) { //doSomething }` This will handle only 3 cases right? Still we need like n if cases to handle all conditions?
– Adrin Johnson
Nov 23 '18 at 12:58
|
show 10 more comments
This is my solution. Note, that you have multiple if...else in one single method. And then you add doSomething. This is going to be terrible to ready and later to realize.
What about to move one single condition into separate method and name it with relative name. Then, lets encapsulate it into Consumer and all of it into a predefined list. Later, if your doSomething will be huge, then you can move from single method to single class, not modifying client code.
This is class, to collect required variable for conditions:
final class Data {
private final String a;
private final String b;
private final String c;
}
Then define one Consumer per on if statement:
Consumer<Data> wonderfulConsumer = data -> {
if (a != null && b == null && c == null) {
// do something for wonderful consumer
}
};
Consumer<Data> badLuckConsumer = data -> {
if (a == null && b != null && c == null) {
// do something for bad luck consumer
}
};
Note, all these consumers could be modified separately (even be in the different classes).
Then in the client code, define list of all known consumers: List<Consumer<Data>> consumers = Arrays.asList(wonderfulConsumer, badLuckConsumer).
And finally your method will be like this and you do not need to change it when you decide to modify or add consumers.
Data data = new Data(a, b, c);
consumers.forEach(consumer -> consumer.accept(data));
add a comment |
If I had to do this , i will do it in the same way that you have done.
but if you dont like that and if you think it is not readable you can do it in this way, i expect lot of negative comments to this answer because this is a bad solution.
public static void yourMethod(Object a,Object b,Object c)
{
int evaluation = howManyNotNull(a,b,c);
if(evaluation == 0) // all are nulls
{
// your code
}
else if(evaluation == 1) // only one is not null
{
if(a!=null)
{
}
else if(b!=null)
{
}
else
{
// c not null
}
}
else if(evaluation == 2) // two variables are not null but other one is null
{
if(a==null)
{
}
else if(b == null)
{
}
else
{
// c is null, a and b not null
}
}
else
{
// all abc not null
}
}
private static int howManyNotNull(Object a, Object b, Object c)
{
return (a==null?0:1) + (b==null?0:1) + (c==null?0:1);
}
There is extended version of this , assign 3 prime values for a, b , c (example :a=2,b=3,c=5), then use a supportive method like this
private static int convertAvailabilityToInt(Object a, Object b, Object c)
{
return (a==null?1:2) * (b==null?1:3) * (c==null?1:5);
}
if the answer is 1 ,then all are not null .
Thank you for the reply. This looks almost similar to my concept. Still thank you for the time
– Adrin Johnson
Nov 23 '18 at 13:10
add a comment |
You can use for example a 3 digit string simulating 3 flags.
You first set it to "000".
Then you check each variable for null, if it is not you will replace the 0 with 1.
Then you could use switch cases to treat each case.
1
This is exactly the same as the question to which the OP links, since he mentions this did NOT work I would say this is not a valid answer
– Sven Hakvoort
Nov 23 '18 at 10:33
It didn't say that is doesn't work. I consider it a cleaner solution still.
– Andrea Calin
Nov 23 '18 at 10:34
2
I agree with you that it is a cleaner solution, but IF the OP would see this as a valid answer this question would be a duplicate and the answer is obsolete
– Sven Hakvoort
Nov 23 '18 at 10:35
There it was suggested to use some sum approach, I just came with a more cleaner idea of using only the string flag.
– Andrea Calin
Nov 23 '18 at 10:37
I already found this solution in the link I referenced. But I was just checking out if other possiblities which make more elegant code. Still thank you for your time and effort
– Adrin Johnson
Nov 23 '18 at 13:12
add a comment |
You are doing everything right but you have to remember that primitive data types cannot be null. For example string is not null, but empty string "", or int cannot be null, its by default sets to 0. In conclusion Objects like Map , ArrayList or Integer.. you can check for null, but primitive data types cannot be null, so you cannot check them for it. For deeper understanding just learn about primitive and advanced data types.
I hope I got your problem right :)
who said that variables have primitive data type ?
– Khalid Shah
Nov 23 '18 at 10:42
1
String is NOT a primitive in java, so it can be null
– Sven Hakvoort
Nov 23 '18 at 10:43
2
String is not a primitive type and a string can be null
– Erwin Bolwidt
Nov 23 '18 at 10:43
1
well it doesnt solve the problem. But thank you for your time and consideration
– Adrin Johnson
Nov 23 '18 at 13:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444933%2fcheck-which-combinations-of-parameters-are-null-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Write these utility functions and you can compare n terms easily.
public static boolean isAllNull(Object... objects) {
return Stream.of(objects).allMatch(Objects::isNull);
}
public static boolean isAllNotNull(Object... objects) {
return Stream.of(objects).allMatch(Objects::nonNull);
}
you can use these functions for n comparisons.
if(isAllNotNull(a) && isAllNull(b,c)) {
//doSomething
}
else if(isAllNotNull(b) && isAllNull(a,c)) {
//doSomething
}
else if(isAllNotNull(c) && isAllNull(b,a)) {
//doSomething
}
2
I like this approach. I would rewriteisAllNull()this way: ` public static boolean isAllNull(Object... objects) { return Stream.of(objects).allMatch(Objects::isNull); }`
– Benoit
Nov 23 '18 at 10:43
@Benoit ok i'll update it. thanks :)
– Khalid Shah
Nov 23 '18 at 10:44
Thus now the first methodisNull()is no more necessary ;-)
– Benoit
Nov 23 '18 at 11:00
@Benoit yeah now that is no mre needed.
– Khalid Shah
Nov 23 '18 at 11:03
@KhalidShah Looks more readable. But I still have a question. `if(isAllNotNull(a) && isAllNull(b,c)) { //doSomething } else if(isAllNotNull(b) && isAllNull(a,c)) { //doSomething } else if(isAllNotNull(c) && isAllNull(b,a)) { //doSomething }` This will handle only 3 cases right? Still we need like n if cases to handle all conditions?
– Adrin Johnson
Nov 23 '18 at 12:58
|
show 10 more comments
Write these utility functions and you can compare n terms easily.
public static boolean isAllNull(Object... objects) {
return Stream.of(objects).allMatch(Objects::isNull);
}
public static boolean isAllNotNull(Object... objects) {
return Stream.of(objects).allMatch(Objects::nonNull);
}
you can use these functions for n comparisons.
if(isAllNotNull(a) && isAllNull(b,c)) {
//doSomething
}
else if(isAllNotNull(b) && isAllNull(a,c)) {
//doSomething
}
else if(isAllNotNull(c) && isAllNull(b,a)) {
//doSomething
}
2
I like this approach. I would rewriteisAllNull()this way: ` public static boolean isAllNull(Object... objects) { return Stream.of(objects).allMatch(Objects::isNull); }`
– Benoit
Nov 23 '18 at 10:43
@Benoit ok i'll update it. thanks :)
– Khalid Shah
Nov 23 '18 at 10:44
Thus now the first methodisNull()is no more necessary ;-)
– Benoit
Nov 23 '18 at 11:00
@Benoit yeah now that is no mre needed.
– Khalid Shah
Nov 23 '18 at 11:03
@KhalidShah Looks more readable. But I still have a question. `if(isAllNotNull(a) && isAllNull(b,c)) { //doSomething } else if(isAllNotNull(b) && isAllNull(a,c)) { //doSomething } else if(isAllNotNull(c) && isAllNull(b,a)) { //doSomething }` This will handle only 3 cases right? Still we need like n if cases to handle all conditions?
– Adrin Johnson
Nov 23 '18 at 12:58
|
show 10 more comments
Write these utility functions and you can compare n terms easily.
public static boolean isAllNull(Object... objects) {
return Stream.of(objects).allMatch(Objects::isNull);
}
public static boolean isAllNotNull(Object... objects) {
return Stream.of(objects).allMatch(Objects::nonNull);
}
you can use these functions for n comparisons.
if(isAllNotNull(a) && isAllNull(b,c)) {
//doSomething
}
else if(isAllNotNull(b) && isAllNull(a,c)) {
//doSomething
}
else if(isAllNotNull(c) && isAllNull(b,a)) {
//doSomething
}
Write these utility functions and you can compare n terms easily.
public static boolean isAllNull(Object... objects) {
return Stream.of(objects).allMatch(Objects::isNull);
}
public static boolean isAllNotNull(Object... objects) {
return Stream.of(objects).allMatch(Objects::nonNull);
}
you can use these functions for n comparisons.
if(isAllNotNull(a) && isAllNull(b,c)) {
//doSomething
}
else if(isAllNotNull(b) && isAllNull(a,c)) {
//doSomething
}
else if(isAllNotNull(c) && isAllNull(b,a)) {
//doSomething
}
edited Nov 23 '18 at 11:04
answered Nov 23 '18 at 10:38
Khalid ShahKhalid Shah
1,3601820
1,3601820
2
I like this approach. I would rewriteisAllNull()this way: ` public static boolean isAllNull(Object... objects) { return Stream.of(objects).allMatch(Objects::isNull); }`
– Benoit
Nov 23 '18 at 10:43
@Benoit ok i'll update it. thanks :)
– Khalid Shah
Nov 23 '18 at 10:44
Thus now the first methodisNull()is no more necessary ;-)
– Benoit
Nov 23 '18 at 11:00
@Benoit yeah now that is no mre needed.
– Khalid Shah
Nov 23 '18 at 11:03
@KhalidShah Looks more readable. But I still have a question. `if(isAllNotNull(a) && isAllNull(b,c)) { //doSomething } else if(isAllNotNull(b) && isAllNull(a,c)) { //doSomething } else if(isAllNotNull(c) && isAllNull(b,a)) { //doSomething }` This will handle only 3 cases right? Still we need like n if cases to handle all conditions?
– Adrin Johnson
Nov 23 '18 at 12:58
|
show 10 more comments
2
I like this approach. I would rewriteisAllNull()this way: ` public static boolean isAllNull(Object... objects) { return Stream.of(objects).allMatch(Objects::isNull); }`
– Benoit
Nov 23 '18 at 10:43
@Benoit ok i'll update it. thanks :)
– Khalid Shah
Nov 23 '18 at 10:44
Thus now the first methodisNull()is no more necessary ;-)
– Benoit
Nov 23 '18 at 11:00
@Benoit yeah now that is no mre needed.
– Khalid Shah
Nov 23 '18 at 11:03
@KhalidShah Looks more readable. But I still have a question. `if(isAllNotNull(a) && isAllNull(b,c)) { //doSomething } else if(isAllNotNull(b) && isAllNull(a,c)) { //doSomething } else if(isAllNotNull(c) && isAllNull(b,a)) { //doSomething }` This will handle only 3 cases right? Still we need like n if cases to handle all conditions?
– Adrin Johnson
Nov 23 '18 at 12:58
2
2
I like this approach. I would rewrite
isAllNull() this way: ` public static boolean isAllNull(Object... objects) { return Stream.of(objects).allMatch(Objects::isNull); }`– Benoit
Nov 23 '18 at 10:43
I like this approach. I would rewrite
isAllNull() this way: ` public static boolean isAllNull(Object... objects) { return Stream.of(objects).allMatch(Objects::isNull); }`– Benoit
Nov 23 '18 at 10:43
@Benoit ok i'll update it. thanks :)
– Khalid Shah
Nov 23 '18 at 10:44
@Benoit ok i'll update it. thanks :)
– Khalid Shah
Nov 23 '18 at 10:44
Thus now the first method
isNull() is no more necessary ;-)– Benoit
Nov 23 '18 at 11:00
Thus now the first method
isNull() is no more necessary ;-)– Benoit
Nov 23 '18 at 11:00
@Benoit yeah now that is no mre needed.
– Khalid Shah
Nov 23 '18 at 11:03
@Benoit yeah now that is no mre needed.
– Khalid Shah
Nov 23 '18 at 11:03
@KhalidShah Looks more readable. But I still have a question. `
if(isAllNotNull(a) && isAllNull(b,c)) { //doSomething } else if(isAllNotNull(b) && isAllNull(a,c)) { //doSomething } else if(isAllNotNull(c) && isAllNull(b,a)) { //doSomething } ` This will handle only 3 cases right? Still we need like n if cases to handle all conditions?– Adrin Johnson
Nov 23 '18 at 12:58
@KhalidShah Looks more readable. But I still have a question. `
if(isAllNotNull(a) && isAllNull(b,c)) { //doSomething } else if(isAllNotNull(b) && isAllNull(a,c)) { //doSomething } else if(isAllNotNull(c) && isAllNull(b,a)) { //doSomething } ` This will handle only 3 cases right? Still we need like n if cases to handle all conditions?– Adrin Johnson
Nov 23 '18 at 12:58
|
show 10 more comments
This is my solution. Note, that you have multiple if...else in one single method. And then you add doSomething. This is going to be terrible to ready and later to realize.
What about to move one single condition into separate method and name it with relative name. Then, lets encapsulate it into Consumer and all of it into a predefined list. Later, if your doSomething will be huge, then you can move from single method to single class, not modifying client code.
This is class, to collect required variable for conditions:
final class Data {
private final String a;
private final String b;
private final String c;
}
Then define one Consumer per on if statement:
Consumer<Data> wonderfulConsumer = data -> {
if (a != null && b == null && c == null) {
// do something for wonderful consumer
}
};
Consumer<Data> badLuckConsumer = data -> {
if (a == null && b != null && c == null) {
// do something for bad luck consumer
}
};
Note, all these consumers could be modified separately (even be in the different classes).
Then in the client code, define list of all known consumers: List<Consumer<Data>> consumers = Arrays.asList(wonderfulConsumer, badLuckConsumer).
And finally your method will be like this and you do not need to change it when you decide to modify or add consumers.
Data data = new Data(a, b, c);
consumers.forEach(consumer -> consumer.accept(data));
add a comment |
This is my solution. Note, that you have multiple if...else in one single method. And then you add doSomething. This is going to be terrible to ready and later to realize.
What about to move one single condition into separate method and name it with relative name. Then, lets encapsulate it into Consumer and all of it into a predefined list. Later, if your doSomething will be huge, then you can move from single method to single class, not modifying client code.
This is class, to collect required variable for conditions:
final class Data {
private final String a;
private final String b;
private final String c;
}
Then define one Consumer per on if statement:
Consumer<Data> wonderfulConsumer = data -> {
if (a != null && b == null && c == null) {
// do something for wonderful consumer
}
};
Consumer<Data> badLuckConsumer = data -> {
if (a == null && b != null && c == null) {
// do something for bad luck consumer
}
};
Note, all these consumers could be modified separately (even be in the different classes).
Then in the client code, define list of all known consumers: List<Consumer<Data>> consumers = Arrays.asList(wonderfulConsumer, badLuckConsumer).
And finally your method will be like this and you do not need to change it when you decide to modify or add consumers.
Data data = new Data(a, b, c);
consumers.forEach(consumer -> consumer.accept(data));
add a comment |
This is my solution. Note, that you have multiple if...else in one single method. And then you add doSomething. This is going to be terrible to ready and later to realize.
What about to move one single condition into separate method and name it with relative name. Then, lets encapsulate it into Consumer and all of it into a predefined list. Later, if your doSomething will be huge, then you can move from single method to single class, not modifying client code.
This is class, to collect required variable for conditions:
final class Data {
private final String a;
private final String b;
private final String c;
}
Then define one Consumer per on if statement:
Consumer<Data> wonderfulConsumer = data -> {
if (a != null && b == null && c == null) {
// do something for wonderful consumer
}
};
Consumer<Data> badLuckConsumer = data -> {
if (a == null && b != null && c == null) {
// do something for bad luck consumer
}
};
Note, all these consumers could be modified separately (even be in the different classes).
Then in the client code, define list of all known consumers: List<Consumer<Data>> consumers = Arrays.asList(wonderfulConsumer, badLuckConsumer).
And finally your method will be like this and you do not need to change it when you decide to modify or add consumers.
Data data = new Data(a, b, c);
consumers.forEach(consumer -> consumer.accept(data));
This is my solution. Note, that you have multiple if...else in one single method. And then you add doSomething. This is going to be terrible to ready and later to realize.
What about to move one single condition into separate method and name it with relative name. Then, lets encapsulate it into Consumer and all of it into a predefined list. Later, if your doSomething will be huge, then you can move from single method to single class, not modifying client code.
This is class, to collect required variable for conditions:
final class Data {
private final String a;
private final String b;
private final String c;
}
Then define one Consumer per on if statement:
Consumer<Data> wonderfulConsumer = data -> {
if (a != null && b == null && c == null) {
// do something for wonderful consumer
}
};
Consumer<Data> badLuckConsumer = data -> {
if (a == null && b != null && c == null) {
// do something for bad luck consumer
}
};
Note, all these consumers could be modified separately (even be in the different classes).
Then in the client code, define list of all known consumers: List<Consumer<Data>> consumers = Arrays.asList(wonderfulConsumer, badLuckConsumer).
And finally your method will be like this and you do not need to change it when you decide to modify or add consumers.
Data data = new Data(a, b, c);
consumers.forEach(consumer -> consumer.accept(data));
answered Nov 23 '18 at 10:52
oleg.cherednikoleg.cherednik
6,92921118
6,92921118
add a comment |
add a comment |
If I had to do this , i will do it in the same way that you have done.
but if you dont like that and if you think it is not readable you can do it in this way, i expect lot of negative comments to this answer because this is a bad solution.
public static void yourMethod(Object a,Object b,Object c)
{
int evaluation = howManyNotNull(a,b,c);
if(evaluation == 0) // all are nulls
{
// your code
}
else if(evaluation == 1) // only one is not null
{
if(a!=null)
{
}
else if(b!=null)
{
}
else
{
// c not null
}
}
else if(evaluation == 2) // two variables are not null but other one is null
{
if(a==null)
{
}
else if(b == null)
{
}
else
{
// c is null, a and b not null
}
}
else
{
// all abc not null
}
}
private static int howManyNotNull(Object a, Object b, Object c)
{
return (a==null?0:1) + (b==null?0:1) + (c==null?0:1);
}
There is extended version of this , assign 3 prime values for a, b , c (example :a=2,b=3,c=5), then use a supportive method like this
private static int convertAvailabilityToInt(Object a, Object b, Object c)
{
return (a==null?1:2) * (b==null?1:3) * (c==null?1:5);
}
if the answer is 1 ,then all are not null .
Thank you for the reply. This looks almost similar to my concept. Still thank you for the time
– Adrin Johnson
Nov 23 '18 at 13:10
add a comment |
If I had to do this , i will do it in the same way that you have done.
but if you dont like that and if you think it is not readable you can do it in this way, i expect lot of negative comments to this answer because this is a bad solution.
public static void yourMethod(Object a,Object b,Object c)
{
int evaluation = howManyNotNull(a,b,c);
if(evaluation == 0) // all are nulls
{
// your code
}
else if(evaluation == 1) // only one is not null
{
if(a!=null)
{
}
else if(b!=null)
{
}
else
{
// c not null
}
}
else if(evaluation == 2) // two variables are not null but other one is null
{
if(a==null)
{
}
else if(b == null)
{
}
else
{
// c is null, a and b not null
}
}
else
{
// all abc not null
}
}
private static int howManyNotNull(Object a, Object b, Object c)
{
return (a==null?0:1) + (b==null?0:1) + (c==null?0:1);
}
There is extended version of this , assign 3 prime values for a, b , c (example :a=2,b=3,c=5), then use a supportive method like this
private static int convertAvailabilityToInt(Object a, Object b, Object c)
{
return (a==null?1:2) * (b==null?1:3) * (c==null?1:5);
}
if the answer is 1 ,then all are not null .
Thank you for the reply. This looks almost similar to my concept. Still thank you for the time
– Adrin Johnson
Nov 23 '18 at 13:10
add a comment |
If I had to do this , i will do it in the same way that you have done.
but if you dont like that and if you think it is not readable you can do it in this way, i expect lot of negative comments to this answer because this is a bad solution.
public static void yourMethod(Object a,Object b,Object c)
{
int evaluation = howManyNotNull(a,b,c);
if(evaluation == 0) // all are nulls
{
// your code
}
else if(evaluation == 1) // only one is not null
{
if(a!=null)
{
}
else if(b!=null)
{
}
else
{
// c not null
}
}
else if(evaluation == 2) // two variables are not null but other one is null
{
if(a==null)
{
}
else if(b == null)
{
}
else
{
// c is null, a and b not null
}
}
else
{
// all abc not null
}
}
private static int howManyNotNull(Object a, Object b, Object c)
{
return (a==null?0:1) + (b==null?0:1) + (c==null?0:1);
}
There is extended version of this , assign 3 prime values for a, b , c (example :a=2,b=3,c=5), then use a supportive method like this
private static int convertAvailabilityToInt(Object a, Object b, Object c)
{
return (a==null?1:2) * (b==null?1:3) * (c==null?1:5);
}
if the answer is 1 ,then all are not null .
If I had to do this , i will do it in the same way that you have done.
but if you dont like that and if you think it is not readable you can do it in this way, i expect lot of negative comments to this answer because this is a bad solution.
public static void yourMethod(Object a,Object b,Object c)
{
int evaluation = howManyNotNull(a,b,c);
if(evaluation == 0) // all are nulls
{
// your code
}
else if(evaluation == 1) // only one is not null
{
if(a!=null)
{
}
else if(b!=null)
{
}
else
{
// c not null
}
}
else if(evaluation == 2) // two variables are not null but other one is null
{
if(a==null)
{
}
else if(b == null)
{
}
else
{
// c is null, a and b not null
}
}
else
{
// all abc not null
}
}
private static int howManyNotNull(Object a, Object b, Object c)
{
return (a==null?0:1) + (b==null?0:1) + (c==null?0:1);
}
There is extended version of this , assign 3 prime values for a, b , c (example :a=2,b=3,c=5), then use a supportive method like this
private static int convertAvailabilityToInt(Object a, Object b, Object c)
{
return (a==null?1:2) * (b==null?1:3) * (c==null?1:5);
}
if the answer is 1 ,then all are not null .
edited Nov 23 '18 at 11:46
answered Nov 23 '18 at 11:08
hunterhunter
1,584715
1,584715
Thank you for the reply. This looks almost similar to my concept. Still thank you for the time
– Adrin Johnson
Nov 23 '18 at 13:10
add a comment |
Thank you for the reply. This looks almost similar to my concept. Still thank you for the time
– Adrin Johnson
Nov 23 '18 at 13:10
Thank you for the reply. This looks almost similar to my concept. Still thank you for the time
– Adrin Johnson
Nov 23 '18 at 13:10
Thank you for the reply. This looks almost similar to my concept. Still thank you for the time
– Adrin Johnson
Nov 23 '18 at 13:10
add a comment |
You can use for example a 3 digit string simulating 3 flags.
You first set it to "000".
Then you check each variable for null, if it is not you will replace the 0 with 1.
Then you could use switch cases to treat each case.
1
This is exactly the same as the question to which the OP links, since he mentions this did NOT work I would say this is not a valid answer
– Sven Hakvoort
Nov 23 '18 at 10:33
It didn't say that is doesn't work. I consider it a cleaner solution still.
– Andrea Calin
Nov 23 '18 at 10:34
2
I agree with you that it is a cleaner solution, but IF the OP would see this as a valid answer this question would be a duplicate and the answer is obsolete
– Sven Hakvoort
Nov 23 '18 at 10:35
There it was suggested to use some sum approach, I just came with a more cleaner idea of using only the string flag.
– Andrea Calin
Nov 23 '18 at 10:37
I already found this solution in the link I referenced. But I was just checking out if other possiblities which make more elegant code. Still thank you for your time and effort
– Adrin Johnson
Nov 23 '18 at 13:12
add a comment |
You can use for example a 3 digit string simulating 3 flags.
You first set it to "000".
Then you check each variable for null, if it is not you will replace the 0 with 1.
Then you could use switch cases to treat each case.
1
This is exactly the same as the question to which the OP links, since he mentions this did NOT work I would say this is not a valid answer
– Sven Hakvoort
Nov 23 '18 at 10:33
It didn't say that is doesn't work. I consider it a cleaner solution still.
– Andrea Calin
Nov 23 '18 at 10:34
2
I agree with you that it is a cleaner solution, but IF the OP would see this as a valid answer this question would be a duplicate and the answer is obsolete
– Sven Hakvoort
Nov 23 '18 at 10:35
There it was suggested to use some sum approach, I just came with a more cleaner idea of using only the string flag.
– Andrea Calin
Nov 23 '18 at 10:37
I already found this solution in the link I referenced. But I was just checking out if other possiblities which make more elegant code. Still thank you for your time and effort
– Adrin Johnson
Nov 23 '18 at 13:12
add a comment |
You can use for example a 3 digit string simulating 3 flags.
You first set it to "000".
Then you check each variable for null, if it is not you will replace the 0 with 1.
Then you could use switch cases to treat each case.
You can use for example a 3 digit string simulating 3 flags.
You first set it to "000".
Then you check each variable for null, if it is not you will replace the 0 with 1.
Then you could use switch cases to treat each case.
answered Nov 23 '18 at 10:32
Andrea CalinAndrea Calin
1889
1889
1
This is exactly the same as the question to which the OP links, since he mentions this did NOT work I would say this is not a valid answer
– Sven Hakvoort
Nov 23 '18 at 10:33
It didn't say that is doesn't work. I consider it a cleaner solution still.
– Andrea Calin
Nov 23 '18 at 10:34
2
I agree with you that it is a cleaner solution, but IF the OP would see this as a valid answer this question would be a duplicate and the answer is obsolete
– Sven Hakvoort
Nov 23 '18 at 10:35
There it was suggested to use some sum approach, I just came with a more cleaner idea of using only the string flag.
– Andrea Calin
Nov 23 '18 at 10:37
I already found this solution in the link I referenced. But I was just checking out if other possiblities which make more elegant code. Still thank you for your time and effort
– Adrin Johnson
Nov 23 '18 at 13:12
add a comment |
1
This is exactly the same as the question to which the OP links, since he mentions this did NOT work I would say this is not a valid answer
– Sven Hakvoort
Nov 23 '18 at 10:33
It didn't say that is doesn't work. I consider it a cleaner solution still.
– Andrea Calin
Nov 23 '18 at 10:34
2
I agree with you that it is a cleaner solution, but IF the OP would see this as a valid answer this question would be a duplicate and the answer is obsolete
– Sven Hakvoort
Nov 23 '18 at 10:35
There it was suggested to use some sum approach, I just came with a more cleaner idea of using only the string flag.
– Andrea Calin
Nov 23 '18 at 10:37
I already found this solution in the link I referenced. But I was just checking out if other possiblities which make more elegant code. Still thank you for your time and effort
– Adrin Johnson
Nov 23 '18 at 13:12
1
1
This is exactly the same as the question to which the OP links, since he mentions this did NOT work I would say this is not a valid answer
– Sven Hakvoort
Nov 23 '18 at 10:33
This is exactly the same as the question to which the OP links, since he mentions this did NOT work I would say this is not a valid answer
– Sven Hakvoort
Nov 23 '18 at 10:33
It didn't say that is doesn't work. I consider it a cleaner solution still.
– Andrea Calin
Nov 23 '18 at 10:34
It didn't say that is doesn't work. I consider it a cleaner solution still.
– Andrea Calin
Nov 23 '18 at 10:34
2
2
I agree with you that it is a cleaner solution, but IF the OP would see this as a valid answer this question would be a duplicate and the answer is obsolete
– Sven Hakvoort
Nov 23 '18 at 10:35
I agree with you that it is a cleaner solution, but IF the OP would see this as a valid answer this question would be a duplicate and the answer is obsolete
– Sven Hakvoort
Nov 23 '18 at 10:35
There it was suggested to use some sum approach, I just came with a more cleaner idea of using only the string flag.
– Andrea Calin
Nov 23 '18 at 10:37
There it was suggested to use some sum approach, I just came with a more cleaner idea of using only the string flag.
– Andrea Calin
Nov 23 '18 at 10:37
I already found this solution in the link I referenced. But I was just checking out if other possiblities which make more elegant code. Still thank you for your time and effort
– Adrin Johnson
Nov 23 '18 at 13:12
I already found this solution in the link I referenced. But I was just checking out if other possiblities which make more elegant code. Still thank you for your time and effort
– Adrin Johnson
Nov 23 '18 at 13:12
add a comment |
You are doing everything right but you have to remember that primitive data types cannot be null. For example string is not null, but empty string "", or int cannot be null, its by default sets to 0. In conclusion Objects like Map , ArrayList or Integer.. you can check for null, but primitive data types cannot be null, so you cannot check them for it. For deeper understanding just learn about primitive and advanced data types.
I hope I got your problem right :)
who said that variables have primitive data type ?
– Khalid Shah
Nov 23 '18 at 10:42
1
String is NOT a primitive in java, so it can be null
– Sven Hakvoort
Nov 23 '18 at 10:43
2
String is not a primitive type and a string can be null
– Erwin Bolwidt
Nov 23 '18 at 10:43
1
well it doesnt solve the problem. But thank you for your time and consideration
– Adrin Johnson
Nov 23 '18 at 13:12
add a comment |
You are doing everything right but you have to remember that primitive data types cannot be null. For example string is not null, but empty string "", or int cannot be null, its by default sets to 0. In conclusion Objects like Map , ArrayList or Integer.. you can check for null, but primitive data types cannot be null, so you cannot check them for it. For deeper understanding just learn about primitive and advanced data types.
I hope I got your problem right :)
who said that variables have primitive data type ?
– Khalid Shah
Nov 23 '18 at 10:42
1
String is NOT a primitive in java, so it can be null
– Sven Hakvoort
Nov 23 '18 at 10:43
2
String is not a primitive type and a string can be null
– Erwin Bolwidt
Nov 23 '18 at 10:43
1
well it doesnt solve the problem. But thank you for your time and consideration
– Adrin Johnson
Nov 23 '18 at 13:12
add a comment |
You are doing everything right but you have to remember that primitive data types cannot be null. For example string is not null, but empty string "", or int cannot be null, its by default sets to 0. In conclusion Objects like Map , ArrayList or Integer.. you can check for null, but primitive data types cannot be null, so you cannot check them for it. For deeper understanding just learn about primitive and advanced data types.
I hope I got your problem right :)
You are doing everything right but you have to remember that primitive data types cannot be null. For example string is not null, but empty string "", or int cannot be null, its by default sets to 0. In conclusion Objects like Map , ArrayList or Integer.. you can check for null, but primitive data types cannot be null, so you cannot check them for it. For deeper understanding just learn about primitive and advanced data types.
I hope I got your problem right :)
edited Nov 23 '18 at 11:29
Khalid Shah
1,3601820
1,3601820
answered Nov 23 '18 at 10:39
Artis VilcinsArtis Vilcins
22
22
who said that variables have primitive data type ?
– Khalid Shah
Nov 23 '18 at 10:42
1
String is NOT a primitive in java, so it can be null
– Sven Hakvoort
Nov 23 '18 at 10:43
2
String is not a primitive type and a string can be null
– Erwin Bolwidt
Nov 23 '18 at 10:43
1
well it doesnt solve the problem. But thank you for your time and consideration
– Adrin Johnson
Nov 23 '18 at 13:12
add a comment |
who said that variables have primitive data type ?
– Khalid Shah
Nov 23 '18 at 10:42
1
String is NOT a primitive in java, so it can be null
– Sven Hakvoort
Nov 23 '18 at 10:43
2
String is not a primitive type and a string can be null
– Erwin Bolwidt
Nov 23 '18 at 10:43
1
well it doesnt solve the problem. But thank you for your time and consideration
– Adrin Johnson
Nov 23 '18 at 13:12
who said that variables have primitive data type ?
– Khalid Shah
Nov 23 '18 at 10:42
who said that variables have primitive data type ?
– Khalid Shah
Nov 23 '18 at 10:42
1
1
String is NOT a primitive in java, so it can be null
– Sven Hakvoort
Nov 23 '18 at 10:43
String is NOT a primitive in java, so it can be null
– Sven Hakvoort
Nov 23 '18 at 10:43
2
2
String is not a primitive type and a string can be null
– Erwin Bolwidt
Nov 23 '18 at 10:43
String is not a primitive type and a string can be null
– Erwin Bolwidt
Nov 23 '18 at 10:43
1
1
well it doesnt solve the problem. But thank you for your time and consideration
– Adrin Johnson
Nov 23 '18 at 13:12
well it doesnt solve the problem. But thank you for your time and consideration
– Adrin Johnson
Nov 23 '18 at 13:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444933%2fcheck-which-combinations-of-parameters-are-null-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
The code you have linked is exactly what I would do. Where is the problem there?
– Silvan
Nov 23 '18 at 10:31
I found similar question, but didn't make the code clean - Can you be more specific, so that we can adapt it to be "cleaner" ?
– Benoit
Nov 23 '18 at 10:36
There are in total 8 cases. If the action for each of the 8 cases is a different one than the other 7, then your way is the only way.
– forpas
Nov 23 '18 at 11:14
Thanks all. I was looking if there is any possibilities to make it more elegant and clean code. Because I personally felt code smell ;). As everyone said I cant avoid using n conditional statements, I would like to take @khalid ans as almost correct
– Adrin Johnson
Nov 23 '18 at 13:15