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...
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.
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(),
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With