/*
 * Copyright (C) 2014 Richard Linsdale (richard.linsdale at blueyonder.co.uk).
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */
package uk.org.rlinsdale.nbpcglibrary.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Manages a set of rules - usually associated with a field. Allows the rules to
 * be tested and error messages combined.
 *
 * @author Richard Linsdale (richard.linsdale at blueyonder.co.uk)
 */
public class Rules {

    private final List<Rule> rules = new ArrayList<>();

    /**
     * Add a rule to this Rule Set.
     *
     * @param rule the rule to add
     */
    public final void addRule(Rule rule) {
        rules.add(rule);
    }

    /**
     * Add rules to this Rule Set.
     *
     * @param newrules the rules to add
     */
    public final void addRules(Rules newrules) {
        if (newrules != null) {
            for (Rule rule : newrules.rules) {
                rules.add(rule);
            }
        }
    }

    /**
     * Add failure messages to the StringBuilder for each rule in this rule set
     * which is failing.
     *
     * @param sb the StringBuilder collecting failure messages
     */
    public final void addFailureMessages(StringBuilder sb) {
        rules.stream().forEach((rule) -> {
            rule.addFailureMessage(sb);
        });
    }

    /**
     * Check if all rules in the set are valid.
     *
     * @return true if all rules are valid
     */
    public final boolean checkRules() {
        boolean valid = true;
        for (Rule rule : rules) {
            if (!rule.check()) {
                valid = false;
            }
        }
        return valid;
    }

    /**
     * Check if all rules (except those marked as unique) in the set are valid.
     *
     * @return true if all rules (except unique) are valid
     */
    public final boolean checkRulesAtLoad() {
        boolean valid = true;
        for (Rule rule : rules) {
            if (!(rule instanceof UniqueRule)) {
                if (!rule.check()) {
                    valid = false;
                }
            }
        }
        return valid;
    }
}

@@ -904,37 +904,8 @@
 st;%0A
-import java.util.Collection;%0A
 impo
@@ -923,16 +923,16 @@
 l.List;%0A
+
 %0A/**%0A * 
@@ -1568,40 +1568,49 @@
     
-for (Rule 
+newrules.
 rule
- : newrules.rules)
+s.stream().forEach((rule) -%3E
  %7B%0A 
@@ -1646,32 +1646,34 @@
 );%0A            %7D
+);
 %0A        %7D%0A    %7D
@@ -2009,16 +2009,16 @@
 ge(sb);%0A
-
         
@@ -2018,32 +2018,334 @@
        %7D);%0A    %7D
+%0A    %0A    /**%0A     * Get failure messages for each failing rule in this rule set.%0A     *%0A     * @return the combined failure message.%0A     */%0A    public final String getErrorMessages() %7B%0A        StringBuilder sb = new StringBuilder();%0A        addFailureMessages(sb);%0A        return sb.toString();%0A    %7D
 %0A%0A    /**%0A     *

create a direct method to produce error messages for a rule set.