patternjavaMinor
Deciding between four user statuses based on two flags
Viewed 0 times
userfourstatusestwobetweendecidingbasedflags
Problem
I think that the following code isn't really nice. I can't see however another way (other than a switch statement) to better implement this. Especially since I don't think replacing this with the strategy pattern for instance is a better way.
I want to return the value of status to the view (this code is from the controller) of the page to show to the user whether that profile he is seeing is already a contact, pending (he already sent a status), accept (it was sent to him), or remove (he already has him as contact).
String status;
if(userHasContact&&contactHasUser) {
status = "HAS_CONTACT";
} else if(userHasContact&&!contactHasUser) {
status = "PENDING";
} else if(!userHasContact&&contactHasUser) {
status = "ACCEPT_REQUEST";
} else {
status = "ADD_CONTACT";
}I want to return the value of status to the view (this code is from the controller) of the page to show to the user whether that profile he is seeing is already a contact, pending (he already sent a status), accept (it was sent to him), or remove (he already has him as contact).
Solution
I think it is time to bring out the
Enums in Java are pretty handy overall when there is a fixed number of possible values. In this case there is only four possible values, so an enum can be a good candidate for the job.
You can get the string representation of the enum by calling
Overall I don't recommend using Strings for this kind of data, you have no guarantee that your
enum!Enums in Java are pretty handy overall when there is a fixed number of possible values. In this case there is only four possible values, so an enum can be a good candidate for the job.
public enum Status {
HAS_CONTACT(true, true),
PENDING(true, false),
ACCEPT_REQUEST(false, true),
ADD_CONTACT(false, false);
private final boolean hasContact;
private final boolean hasUser;
private Status(boolean hasContact, boolean hasUser) {
this.hasContact = true;
this.hasUser = true;
}
public static Status findStatusWith(boolean hasContact, boolean hasUser) {
for (Status status : Status.values()) {
if (status.hasContact == hasContact && status.hasUser == hasUser) {
return status;
}
}
return null;
}
}You can get the string representation of the enum by calling
.name() or .toString()Status status = Status.findStatusWith(userHasContact, contactHasUser);
String statusDescription = status.name();Overall I don't recommend using Strings for this kind of data, you have no guarantee that your
String will only have one of the four possible values. What if the String suddenly says "ACCPT_SMTHNG"? An enum is much more preferable in this way.Code Snippets
public enum Status {
HAS_CONTACT(true, true),
PENDING(true, false),
ACCEPT_REQUEST(false, true),
ADD_CONTACT(false, false);
private final boolean hasContact;
private final boolean hasUser;
private Status(boolean hasContact, boolean hasUser) {
this.hasContact = true;
this.hasUser = true;
}
public static Status findStatusWith(boolean hasContact, boolean hasUser) {
for (Status status : Status.values()) {
if (status.hasContact == hasContact && status.hasUser == hasUser) {
return status;
}
}
return null;
}
}Status status = Status.findStatusWith(userHasContact, contactHasUser);
String statusDescription = status.name();Context
StackExchange Code Review Q#87973, answer score: 5
Revisions (0)
No revisions yet.