Conditional Logic to Determine Correct Status String [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
There is a status variable in a Java application that can be set to one of many statutes, depending on many conditions. The status field is a String. When a condition is met, the status should be returned immediately, as follows:
e.g
String status = "";
if (condition1) {
return "STATUS_1";
} else if (condition2) {
return "STATUS_2";
} else if (condition3) {
return "STATUS_3";
} else if (condition4) {
return "STATUS_4";
}
...
else if (condition10) {
return "STATUS_10";
}
I've considered which pattern would be best to make this code more SOLID... e.g. if a new condition is required then this class would need to edited to add the new condition, which would break the open / closed SOLID principle
I've looked at the Strategy Pattern, in particular "Replace Conditional Logic with Strategy", however that seems more appropriate when you want to decide on just one calculation / operation to use... My scenario does not seem to fit the Strategy Pattern as my logic determines the status, rather than determining which individual operation to execute - I need to run all the conditions until one is true
I wondered if the following pattern could work...
Have an interface as follows
public interace StatusCondition {
boolean condition(Context context);
String getStatus();
}
With an implementation as follows:
public class StatusAStatusCondition implements StatusCondition {
boolean condition(Context context){
return context.getValue1() == 0 && context.getValue2().equals("A");
}
String getStatus(){
return "STATUS_A";
}
}
This would allow a list of StatusCondition classes to be executed in order and return the status of the first StatusCondition where the condition() method returns true. e.g:
public String getStatus(List<StatusCondition> statusConditions) {
for (StatusCondition statusCondition : statusConditions) {
if (statusCondition.condition()) {
return statusCondition.getStatus();
}
}
return "";
}
usage:
List<StatusCondition> statusConditions = new ArrayList<>();
statusConditions.add(statusAStatusCondition);
statusConditions.add(statusBStatusCondition);
statusConditions.add(statusCStatusCondition);
statusConditions.add(statusDStatusCondition);
statusConditions.add(statusEStatusCondition);
statusConditions.add(statusFStatusCondition);
...
String status = getStatus(statusConditions);
To me this solves the open closed principle issue and also ensures the implementations are single responsibility... My question is, how could this pattern i've suggested be improved, or is there a pattern better suited to my scenario?

First, you are absolutely correct that the original if/else ladder violates the Open/Closed Principle. Second, converting the status value to an interface is exactly the right step to take, to move away from stringly-typed programming. Third, your solution is essentially the Chain of Responsibility Pattern. It's an excellent solution to this problem. In summary, your instincts are spot on.

Related

Stack of Plates: Cracking the coding interview , Personal Solution [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Stack of Plates: Imagine a (literal) stack of plates. If the stack gets too high, it might topple.
Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure SetOfStacks that mimics this. SetOfStacks should be composed of several stacks and should create a new stack once the previous one exceeds capacity.
SetOfStacks. push () and SetOfStacks. pop() should behave identically to a single stack (that is, pop ( ) should return the same values as it would if there were just a single stack).
I've implemented a solution with a HashMap.
I'd like to know if this is a good implementation (in space and complexity) and if it can be improve. Moreover, I've used correctly this.index ?
public class SetOfStacks {
private final int LIMIT= 5;
private HashMap<Integer,Stack<Integer>> setOfStack;
private int index;
public SetOfStacks(){
this.setOfStack= new HashMap<>();
this.index = 0;
}
public void addStack(){
this.index++;
if(!setOfStack.containsKey(index))
this.setOfStack.put(index, new Stack<Integer>());
}
public void pushElement(int value){
if (isFull())
addStack();
setOfStack.get(index).push(value);
}
public boolean isFull(){
return setOfStack.get(index).size() == LIMIT;
}
public void popElement(){
if(setOfStack.get(index).isEmpty())
index--;
setOfStack.get(index).pop();
}
public int peekElement(){
return setOfStack.get(index).peek();
}
}
FOLLOW UP
Implement a function popAt (int index) which performs a pop operation on a specific substack.
I didn't write the solution for it, but i think that his implementation should be easy. I only need to pass the parameter and just manage the push/pop, in case i need to refull that stack or not.
What do you think about maintenance of my code if you need to implement the follow up?
It's good but you have some mistake,
you never initialize the first stack,
also what happens if I initialize the SetOfStacks and call the pop?(assuming you fixed the first mistake)

I need to exit a methode, without returning a value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have methode that returnd the smallest value in an arrayList. However, it should return anything if the arrayList is empty. Is there something I can do?
private int smallest() {
if(alList.empty()) {
//whatever will exit the
//function.
}
small = alList.get(0);
for(int i = 1, i <
alList.size(), i++) {
if(small > alList.get(i)){
small = alList.get(i)
}
}
return small;
}
//Thanks for your help.
Since you need to return an int, then no, you must return a value.
If you can change the method signature, you can:
Return an Integer and so return null.
Return Optional<Integer> and return an empty Optional.
There are many options. The three most obvious ones:
throw an exception: `if (alList.isEmpty()) throw new IllegalStateException("alList must not be empty when invoking smallest()");
decide on a sentinel value, for example 0, and return that. Update the docs to make it clear that 0 is returned for empty lists.
private int smallest(int valueIfEmpty) { if (alList.isEmpty()) return valueIfEmpty; ... } - i.e. pass the value to be returned in that case, so that the caller can decide what should happen.

How to call a method which assigns a new value to argument if it's null in Java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm thinking about validateEntity. What do you think about it?
T validateEntity(#Nullable T entity)
{
if (entity == null)
{
entity = DEFAULT_ENTITY;
}
return entity;
}
It's not a validation, it's replacement. I think the method name is incorrect.
// replaceIfNull as possible option
T getDefaultIfNull(#Nullable T entity) {
if (entity == null)
return DEFAULT_ENTITY;
return entity;
}
Using thernary operator:
T getDefaultIfNull(#Nullable T entity) {
return entity == null ? DEFAULT_ENTITY : entity;
}
Java 8:
T getDefaultIfNull(#Nullable T entity) {
return Optional.ofNullable(entity).orElse(DEFAULT_ENTITY);
}
With Guava liblary com.google.common.base.Optional<T>:
T getDefaultIfNull(#Nullable T entity) {
return Optional.fromNullable(entity).or(DEFAULT_ENTITY);
}
Guava com.google.common.base.MoreObjects:
T getDefaultIfNull(#Nullable T entity) {
return MoreObjects.firstNonNull(entity, DEFAULT_ENTITY)
}
Actually, as you can see there is the Java 8 tools and external libraries that can establish reasonable doubt on existance of such method.
If you decide yo go without any libraries or using Java version lower than 8, you can stick to the first approach.
It depends on what exactly you want to achieve.
If you want to validate some element, the method should probably return a boolean or a value representing some validation status about the entity, rather than an entity itself.
Otherwise, if you want to replace the element — you cannot actually "replace" an object itself with another object. For instance, the following method returns the given entity if it is not null, or DEFAULT_ENTITY if it is.
T guaranteeNotNull(#Nullable T entity) {
return (entity != null ? entity : DEFAULT_ENTITY);
}
But it depends on the caller if the returned value is actually assigned to a variable, replacing another value. The following snippet has actually no effect:
String someValue = null;
guaranteeNotNull(someValue);
System.out.println(someValue); // Still null
The best thing is actually to either shortcut the method to this:
T guaranteeNotNull(#Nullable T entity) {
return Optional.ofNullable(entity).orElse(DEFAULT_ENTITY);
}
or get rid of the method altogether.

Should I avoid returning null in my java function? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I don't find the appropriate solution for my question. As far as I know returning null is a not a good way to write clean code, as the book Clean Code says. However there are many different opinions about this practice, and I am not sure about which one fits on my function.
private EArea getSimilarExistingArea(EImportArea importedArea) {
for (EArea existingArea : exsitingAreas) {
EList<EPoint> importedAreaPoints = importedArea.getPoly().getPoints();
EList<EPoint> existingAreaPoints = existingArea.getPoly().getPoints();
for (EPoint importedAreaPoint : importedAreaPoints) {
for (EPoint existingAreaPoint : existingAreaPoints) {
if (importedAreaPoint.equals(existingAreaPoint))
return existingArea;
}
}
}
return null;
}
What should I return if there is not an existing similar area?
PD: For optimize my code I am breaking the loops with the return if an existing area is founded.
You should take a look at the Optional class!
Make your method return type Optional<EArea> and simply return Optional.ofNullable(existingArea) you will have to slightly modify your Code but the benefits of Optional are really worth it!
Finally I used Optional Class to solve my problem.
Here is the code:
private Optional<EArea> getSimilarExistingArea(EImportArea importedArea) {
for (EArea existingArea : baseLineService.getBaseLine().getAreas()) {
EList<EPoint> importedAreaPoints = importedArea.getPoly().getPoints();
EList<EPoint> existingAreaPoints = existingArea.getPoly().getPoints();
for (EPoint importedAreaPoint : importedAreaPoints) {
for (EPoint existingAreaPoint : existingAreaPoints) {
if (importedAreaPoint.equals(existingAreaPoint))
return Optional.of(existingArea);
}
}
}
return Optional.empty();
}
And here is how I check the returned value:
if (getSimilarExistingArea(importedArea).isPresent())

Calling a Method Once [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having a problem of how to call a method once when the condition achieves many times! for example:
public void onLocaitonChanged(Location location){
// this if statement may achieve the condition many times
if(somethingHappened){
callAMethodOnce();// this method is called once even if the condition achieved again
}
}
Please help with that
public void onLocaitonChanged(Location location){
// this if statement may achieve the condition many times
if(somethingHappened){
if (!isAlreadyCalled){
callAMethodOnce();// this method is called once even if the condition achieved again
isAlreadyCalled = true;
}
}
}
boolean isHappendBefore = false;
public void onLocaitonChanged(Location location){
// this if statement may achieve the condition many times
if(somethingHappened && (! isHappendBefore) ){
isHappendBefore = true;
callAMethodOnce();// this method is called once even if the condition achieved again
}
}
You could simply set a flag. If you just need it to only happen once with each instance of the Activity then set a member variable.
public class MyActivity extends Activity
{
boolean itHappened = false;
...
public void onLocaitonChanged(Location location)
{
// this if statement may achieve the condition many times
if(somethingHappened && !itHappened)
{
callAMethodOnce();// this method is called once even if the condition achieved again
itHappened = true;
}
}
If you want it to only happen once ever in the life of the application then set that variable as a SharedPreference
set a class wide boolean
if(!hasRun){
callAMethodOnce();
hasRun = true;
}
Maybe I don't understand your question correctly but from your problem definition I would suggest using a boolean variable something like.
boolean run = false;
public void onLocaitonChanged(Location location){
// this if statement may achieve the condition many times
if(somethingHappened && run == false){
run = true;
callAMethodOnce();// this method is called once even if the condition achieved again
}
}
Once the code under if statement gets executed once run would be true and there won't be any subsequent calls to callAMethodOnce()

Categories

Resources