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

org.apache.bval.jsr.metadata.MetadataBuilders Maven / Gradle / Ivy

/*
 *  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.metadata;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import jakarta.validation.ValidationException;

import org.apache.bval.jsr.util.Methods;
import org.apache.bval.util.Exceptions;
import org.apache.bval.util.Validate;

public class MetadataBuilders {

    private final Map, List>> beanBuilders = new ConcurrentHashMap<>();

    public  void registerCustomBuilder(Class bean, MetadataBuilder.ForBean builder) {
        Validate.notNull(bean, "bean");
        Validate.notNull(builder, "builder");
        validateCustomBuilder(bean, builder);
        beanBuilders.computeIfAbsent(bean, c -> new ArrayList<>()).add(builder);
    }

    public  List> getCustomBuilders(Class bean) {
        @SuppressWarnings({ "unchecked", "rawtypes" })
        final List> list = (List) beanBuilders.get(bean);
        return list == null ? Collections.emptyList() : Collections.unmodifiableList(list);
    }

    public Set> getCustomizedTypes() {
        return beanBuilders.keySet();
    }

    private  void validateCustomBuilder(Class bean, MetadataBuilder.ForBean builder) {
        final Meta> meta = new Meta.ForClass<>(bean);
        final Set propertyNames = builder.getGetters(meta).keySet();
        builder.getMethods(meta).keySet().stream().map(Signature::getName).filter(Methods::isGetter)
            .map(Methods::propertyName).forEach(pn -> {
                Exceptions.raiseIf(propertyNames.contains(pn), ValidationException::new,
                    "%s user metadata cannot specify both method and getter elements for %s", f -> f.args(bean, pn));
            });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy