Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3424100
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:26:03+00:00 2026-05-18T06:26:03+00:00

I have a small set of validation classes which I have created which have

  • 0

I have a small set of validation classes which I have created which have served me well, however now I need to update them to handle prioritized rules. If a high priority rule is met, then I don’t need to run any further validations, as we will just tell the user one single error message instead of adding the entire set of messages to the user.

Here is the set of classes I have:

//Rule.java
public interface Rule<T> {
    List<ErrorMessage> validate(T value);
}

//ValidationStrategy.java
public interface ValidationStrategy<T> {
    public List<Rule<? super T>> getRules();
}

//Validator.java
public class Validator<T> implements Rule<T> {

    private List<Rule<? super T>> tests = new ArrayList<Rule<? super T>>();

    public Validator(ValidationStrategy<T> type) {
        this.tests = type.getRules();
    }

    public List<ErrorMessage> validate(T value) {
        List <ErrorMessage> errors = new ArrayList<ErrorMessage>();
            for (Rule<? super T> rule : tests) {
                errors.addAll(rule.check(value));
            }
            return errors;
    }
}

I am having some trouble modifying this code to deal with prioritized rules. Surely there is something out there that I can modify to use instead of bringing in a Rules Engine.

Ideally I’d then be able to create rules like this:

private static final Rule<SomeClass> ensureAllFieldsNotBlank = new Rule<SomeClass>(RulePriority.HIGHEST) {

    public List<ErrorMessage> check(SomeClass someClass) {
        List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
        if (StringUtils.isBlank(someClass.getValue1())
            && StringUtils.isBlank(someClass.getValue2())
            && StringUtils.isBlank(someClass.getValue3())) {
                errors.add("Provide a response for \"" + someClass.getName() + "\"");
        }
        return errors;
    }
};

Edit to updated classes:

//ValidationStrategy.java
public interface ValidationStrategy<T> {
    public List<Rule<? super T>> getRules(RulePriority rulePriority);
}

//RulePriority.java
public enum RulePriority { HIGHEST, DEFAULT, LOWEST; }

//Validator.java
public class Validator<T> implements Rule<T> {
   private List<Rule<? super T>> tests = new ArrayList<Rule<? super T>>();
   private ValidationStrategy<T> validationStrategy;

   public Validator(ValidationStrategy<T> validationStrategy) {
       this.validationStrategy = validationStrategy;
       for (RulePriority rp : RulePriority.values()) {
           this.tests.addAll(validationStrategy.getRules(rulePriority));
       }
   }

   public List<ErrorMessage> validate(T value) {
       List<ErrorMessage> errors = new ArrayList<String>();
       for (RulePriority rp : RulePriority.values()) {
           for (Rule<? super T> rule : validationStrategy.getRules(rp)) {
               errors.addAll(rule.validate(value));
           }
           if (errors.size() > 0) {
               break;
           }
       }
       return errors;
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:26:04+00:00Added an answer on May 18, 2026 at 6:26 am

    How about creating an abstract base class to handle rule comparisons:

    abstract class PrioritizedRule<T> implements Rule<T>, Comparable<PrioritizedRule<T>>{
        public int compareTo(PrioritizedRule<T> other){
            //Implement something that compares rule priorities here.
            //This will probably require support from the constructor, which-
            //since this is abstract- must be Protected.
    

    From there, your PrioritizedValidator (which requires PrioritizedRule) will sort() its collection in the start of Validate (if its collection of rules has been modified since the last validate; that’s the right time to sort the collection, since we don’t want to repeat the sort on every modification if there are consecutive modifications, or do a sort if we don’t need to), and the Validate loop should early-out if its list of error messages is non-empty across a transition between rule priorities:

    public List<ErrorMessage> validate(T value) {
        if(ruleSetModified){
            //be careful: validate becomes unsafe for multithreading here, even if you
            //aren't modifying the ruleset; if this is a problem, implement locking
            //inside here. Multiple threads may try to sort the collection, but not
            //simultaneously. Usually, the set won't be modified, so locking before
            //the test is much, much slower. Synchronizing the method is safest,
            //but carries a tremendous performance penalty
            Collections.sort(rule);
            ruleSetModified = false;
        }
        List <ErrorMessage> errors = new ArrayList<String>();
            PrioritizedRule prev = null;
            for (PrioritizedRule<? super T> rule : tests) {
                if(prev != null && prev.compareTo(rule) != 0 && !errors.isEmpty()){
                    return errors;
                }
                errors.addAll(rule.check(value));
                prev = rule;
            }
            return errors;
    }
    

    I’m not sure what you mean by “…without bringing in a rules engine”, but defining rules to sort themselves is probably the most graceful approach. Be careful, though- any two PrioritizedRule-s will have to be comparable to each other, which is why I’m recommending PrioritizedRule be an abstract base instead of an interface, because that’s where the compareTo implementation needs to live, for consistency. Your compareTo does not need to be consistent with equals unless you try to keep your collection in a sorted set, which can never end well (PrioritizedRule can’t be aware of enough to make itself consistent with equals!), so don’t try.

    Alternatively, implement a Comparator>, but again, your Rule interface will need to be modified to expose enough information to sort with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small (500kb) swing applet that displays very simple/limited set of small
I have a small ajax application built with php. Using phpMyAdmin I have set
I have small page which has label, DropDownList and a submit button. <div> <asp:label
I have 4 small classes to deserialize xml from an incomming xml poll, to
I have small problem with my .net 2.0 winforms application. I want to embed
Where I work, we have small teams of 2 - 5 people. As a
I have a small JS function that does Ajax for me and another like
I have a small utility that I use to download an MP3 file from
I have a small local network. Only one of the machines is available to
I have a small VB.NET application that I'm working on using the full version

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.