0

I am trying to validate a set of values 'RecordStatus' and 'Condition'. Have come up with the below method.

  1. Define a class RecordStatusCodition with strings recordStatus and condition.

  2. Define a class RecordStatusCoditionValidator implemented as below

ArrayList validList = new ArrayList() {{

                  add(new RecordStatusCodition ("CREATED","A"));
                  add(new RecordStatusCodition ("INSERTED","B"));
                  add(new RecordStatusCodition ("INSERTED","A"));
                  }};
public boolean isProcessed(RecordStatusCodition recordStatusCodition ) {
                    return checkList.contains(recordStatusCodition);
    }
}

This class is being called from my code elsewhere to check for the conditions

  if(!RecordStatusCoditionValidator.isProcessed(new RecordStatusCodition("","")))
               // do processing
        }

Similar to the above, there are various other conditions with different lists, which might have to be changed frequently

Although this works fine, I need to know if there is a better approach for this since this is something which most part of my code will be depending on?

1 Answer 1

1

I suggest you use a Map (HashMap implementation) instead of an ArrayList. This way you can keep the object's identificator (in this case A or B) and store its state as the value. Then, your isProcessed method can be reduced to

map.containsKey(key); //Where key is either "A", "B", etc.

This map shouldn't be a raw field in a class. I suggest you wrap it in your own object, one you can reference in your application instead of the raw map. That'll give you control over the different operations and allow you to do some pre or post processing in those operations.

Your RecordStatusCondition class can be, also, refactored into an enumeration if you like, defining the range of conditions a given object can have:

RecordStatusCondition.CREATED;
RecordStatusCondition.INSERTED;

Then, you can add an object like this:

recordStatusContainer.put(record.getId(), RecordStatusCondition.CREATED);

You can obtain a status fairly easy too with the get() method but remember that if you use tour own object as a key, you'll have to redefine hascode() and equals() for it to work.

As a side suggestion, if possible you can have your RecordStatus (or whatever object represents your main logic unit here) to know its current condition bu adding a RecordStatusCondition field to it.

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

2 Comments

But if we use maps, what if we have a combination 'INSERTED and A' in addition to the other combinations in validList? That would be a duplicate key,'A' yea?
@user1583803 Would they refer to exactly the very same instance of A? That's why I used getId() in the example assuming that every state was related to a particular instance of whatever object type it would be. Otherwise, why would A have multiple states? In any case, if you need multiple states use a HashMap with a List<RecordStatusCondition> or Set<RecordStatusCondition> value instead.

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.