7

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...

3
  • 1
    if is a statement and cannot be used as an Expression. ?: is an expression: of course, all expression must evaluate to a value. Commented Jan 9, 2013 at 7:08
  • The title should be improved, but I don't know how. Commented Jan 9, 2013 at 7:10
  • @MiserableVariable Yeah, I know, sorry, I couldn't think of a good title myself... Commented Jan 9, 2013 at 7:18

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

@MiserableVariable: I disagree. It is clear simple and readable. Unless there is a very good reason why NOT to use it (i.e. performance is critical in this section), there is no reason why not to.
@MiserableVariable In some other languages, yes (like Scala, C#, Ruby, etc :D). However, Java's mediocre Collection syntax/support is showing here .. of course, I would argue why not just return a List?
To each his own :) I did not think it pretty, that is why I wrote there is no good alternative.
Can you show what it would look like, cause I don't know what you mean by this? I'm not sure if I could return a List, since the new Object[] was used in the example, I'm using this for table items in Vaadin framework
Looks really good, I'm trying this out right now, but there seems to be something wrong with the type arguments or something: ArrayList<Object> ret = new ArrayList<>( it says cannot infer type arguments for ArrayList<> and '<>' operator not allowed for source level below 1.7 (I'm using 1.6 for my project)
|
2

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(), 
        }
}

8 Comments

NO! It does not have the same affect, it has an extra element in the array with value "" or null if the condition does not hold.
But then an extra member will be created which will be an empty string, I need no object there? Or do I misunderstand it?
@ArturasM: Do you want to add extra parameter based on editable state? if yes, not possible in this construct.
whoa easy with the downvotes, the question is somewhat ambiguous -- need nothing can be considered to be null is ok
@Azodious exactly, I want nothing if it's false. Is there some construct I can use to achieve that?
|
1

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.

1 Comment

So the only way would be to write the if before and write to return statements which differ in one last element either present or not?
0

It looks like you want to use conditional operator like this:

editable ? "Edit" : "" (or null if you want it that way).

Obviously, it has the extra element.

2 Comments

NO! It does not have the same affect, it has an extra element in the array with value "" or null if the condition does not hold.
Nope, it's more like it either has an element or doesn't have an element at all if the condition is false
0

Let's assume that you are trying to make an object to contain data about visits. Why don't you want to create a function for if (editable) { "Edit" } and utilize encapsulation?

private String getEditFeedback(boolean editable){
  if(editable){
   return "Edit";
  } else {
   return //something
  }
}

Then inside your code,

 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(), 
               getEditFeedback(editable), 
                };
    }

1 Comment

Very similar to the ternary operator issue - it will have an extra element in the array if the condition does not hold.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.