Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally add "optional items" with array initialization syntax?

I'm wondering if I can easily have an if statement somehow here:

public Object[] tableItemFromVisit(Visit visit, boolean editable) {
    return new Object[] {
            visit.getIdVisit(),
            visit.getProfession().getProfessionName(),
            visit.getSpiProfessional().getFullName(),
            RegularFunctions.getTimeFormat().format(visit.getVisitDate()),
            RegularFunctions.toNormalCapitalizedText(visit.getVisitState()
                    .toString()), visit.getReason(), 
            if (editable) { "Edit" }, 
            };
}

How is this structure even called? An array specification or what? Anyway if the variable "editable" is true, it has to have an "Edit" string, if it's false, it doesn't need anything... Obviously I don't want to write two return statements that are way too similar to each other...

like image 557
Arturas M Avatar asked Jan 09 '13 07:01

Arturas M


3 Answers

Construct the above array as an ArrayList and return its toArray?

Essence of the idea is to do something like this.

ArrayList<Object> ret = new ArrayList<Object>(new Object[] {
            visit.getIdVisit(),
            visit.getProfession().getProfessionName(),
            visit.getSpiProfessional().getFullName(),
            RegularFunctions.getTimeFormat().format(visit.getVisitDate()),
            RegularFunctions.toNormalCapitalizedText(visit.getVisitState()
                    .toString()), visit.getReason()
            // don't add Edit item at all yet
            })

if(editable)
     ret.add("Edit");

return ret.toArray();

I am not sure if this kind of initialization works though, if not Arrays.toList can also be used, or just add one by one.

like image 145
Karthik T Avatar answered Nov 08 '22 03:11

Karthik T


Passing extra parameter using the construct provided is not possible. You modify your code in one of following ways:

Try ternary operator:

editable ? "Edit" : ""

OR

editable ? "Edit" : null

if(editable)
{
    return new Object[] {
        visit.getIdVisit(),
        visit.getProfession().getProfessionName(),
        visit.getSpiProfessional().getFullName(),
        RegularFunctions.getTimeFormat().format(visit.getVisitDate()),
        RegularFunctions.toNormalCapitalizedText(visit.getVisitState()
                .toString()), visit.getReason(), 
        "Edit" }
}
else
{
return new Object[] {
        visit.getIdVisit(),
        visit.getProfession().getProfessionName(),
        visit.getSpiProfessional().getFullName(),
        RegularFunctions.getTimeFormat().format(visit.getVisitDate()),
        RegularFunctions.toNormalCapitalizedText(visit.getVisitState()
                .toString()), visit.getReason(), 
        }
}
like image 42
Azodious Avatar answered Nov 08 '22 02:11

Azodious


If I understand you right, you want the returned Object[] to have and extra element if editable is true.

That is not possible in java.

If null can be used there, you can use editable ? "Edit" : null in place of if (editable) { "Edit" } , but you probably already know that.

like image 1
Miserable Variable Avatar answered Nov 08 '22 02:11

Miserable Variable