All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.bval.jsr.groups.Groups Maven / Gradle / Ivy

There is a newer version: 10.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.bval.jsr.groups;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import javax.validation.GroupDefinitionException;

import org.apache.bval.util.Exceptions;

/**
 * Defines the order to validate groups during validation. with some inspiration from reference implementation
 *
 * @author Roman Stumm
 */
public class Groups {
    private final Set groups = new LinkedHashSet<>();
    private final Set sequences = new LinkedHashSet<>();

    /**
     * Get the Groups.
     * 
     * @return {@link Set} of {@link Group}.
     */
    public Set getGroups() {
        return Collections.unmodifiableSet(groups);
    }

    /**
     * Get the Group sequences.
     * 
     * @return {@link List} of {@link Group.Sequence}
     */
    public Collection getSequences() {
        return Collections.unmodifiableSet(sequences);
    }

    /**
     * Insert a {@link Group}.
     * 
     * @param group
     *            to insert
     * @return success
     */
    boolean insertGroup(Group group) {
        return groups.add(group);
    }

    /**
     * Insert a sequence.
     * 
     * @param groups
     *            {@link List} of {@link Group} to insert
     * @return success
     */
    boolean insertSequence(Collection groups) {
        return !(groups == null || groups.isEmpty()) && sequences.add(Group.sequence(groups));
    }

    /**
     * Assert that the default group can be expanded to defaultGroups.
     * Package-private method intended for unit tests.
     * 
     * @param defaultGroups
     */
    void assertDefaultGroupSequenceIsExpandable(List defaultGroups) {
        Consumer> action = (groupList) -> {
            final int idx = groupList.indexOf(Group.DEFAULT);
            if (idx >= 0) {
                ensureExpandable(groupList, defaultGroups, idx);
            }
        };
        sequences.stream().map(Group.Sequence::getGroups).map(ArrayList::new).forEach(action);
    }

    private void ensureExpandable(List groupList, List defaultGroupList, int defaultGroupIndex) {
        for (int i = 0, sz = defaultGroupList.size(); i < sz; i++) {
            final Group group = defaultGroupList.get(i);
            if (group.isDefault()) {
                continue; // the default group is the one we want to replace
            }
            // sequence contains group of default group sequence
            final int index = groupList.indexOf(group);
            if (index < 0) {
                // group is not in the sequence
                continue;
            }
            if ((i == 0 && index == defaultGroupIndex - 1)
                || (i == defaultGroupList.size() - 1 && index == defaultGroupIndex + 1)) {
                // if we are at the beginning or end of he defaultGroupSequence
                // and the matches are either directly before or after we can
                // continue, since we basically have two groups
                continue;
            }
            Exceptions.raise(GroupDefinitionException::new, "Unable to expand default group list %s into sequence %s",
                defaultGroupList, groupList);
        }
    }

    public GroupStrategy asStrategy() {
        final List components = new ArrayList<>();
        components.addAll(groups);
        components.addAll(sequences);
        return GroupStrategy.composite(components);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy