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

org.apache.bval.jsr.descriptor.ComposedD Maven / Gradle / Ivy

There is a newer version: 10.0.0-M3
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.descriptor;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;

import javax.validation.metadata.CascadableDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.ContainerDescriptor;
import javax.validation.metadata.ContainerElementTypeDescriptor;
import javax.validation.metadata.ElementDescriptor;
import javax.validation.metadata.GroupConversionDescriptor;
import javax.validation.metadata.PropertyDescriptor;

import org.apache.bval.jsr.groups.GroupsComputer;
import org.apache.bval.jsr.util.ToUnmodifiable;
import org.apache.bval.util.Validate;

public abstract class ComposedD> implements ElementDescriptor {

    static abstract class ForCascadableContainer> extends ComposedD
        implements CascadableDescriptor, ContainerDescriptor {

        ForCascadableContainer(List delegates) {
            super(delegates);
        }

        @Override
        public Set getConstrainedContainerElementTypes() {
            return delegates.stream().map(ContainerDescriptor::getConstrainedContainerElementTypes)
                .flatMap(Collection::stream).collect(ToUnmodifiable.set());
        }

        @Override
        public boolean isCascaded() {
            return delegates.stream().anyMatch(CascadableDescriptor::isCascaded);
        }

        @Override
        public Set getGroupConversions() {
            return delegates.stream().map(CascadableDescriptor::getGroupConversions).flatMap(Collection::stream)
                .collect(ToUnmodifiable.set());
        }
    }

    static class ForProperty extends ComposedD.ForCascadableContainer> implements PropertyDescriptor {

        ForProperty(List> delegates) {
            super(delegates);
        }

        @Override
        public String getPropertyName() {
            return delegates.stream().map(PropertyDescriptor::getPropertyName).findFirst()
                .orElseThrow(IllegalStateException::new);
        }
    }

    public static > Stream unwrap(ElementDescriptor descriptor, Class delegateType) {
        final Stream s;

        if (descriptor instanceof ComposedD) {
            s = ((ComposedD) descriptor).delegates.stream()
                // unwrap recursively:
                .flatMap(d -> unwrap(d, delegateType));
        } else {
            s = Stream.of(descriptor);
        }
        return s.map(delegateType::cast);
    }

    protected final List delegates;

    ComposedD(List delegates) {
        super();
        this.delegates = delegates;

        Validate.notNull(delegates, "delegates");
        Validate.isTrue(!delegates.isEmpty(), "At least one delegate is required");
        Validate.isTrue(delegates.stream().noneMatch(Objects::isNull), "null delegates not permitted");
    }

    @Override
    public boolean hasConstraints() {
        return delegates.stream().anyMatch(ElementDescriptor::hasConstraints);
    }

    @Override
    public Class getElementClass() {
        return delegates.stream().map(ElementDescriptor::getElementClass).findFirst()
            .orElseThrow(IllegalStateException::new);
    }

    @Override
    public Set> getConstraintDescriptors() {
        return delegates.stream().map(ElementDescriptor::getConstraintDescriptors).flatMap(Collection::stream)
            .collect(ToUnmodifiable.set());
    }

    @Override
    public ConstraintFinder findConstraints() {
        final GroupsComputer groupsComputer =
            unwrap(this, ElementD.class).findFirst().orElseThrow(IllegalStateException::new).groupsComputer;

        return new Finder(groupsComputer, this);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy