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

io.ultreia.java4all.bean.JavaBeanDefinition Maven / Gradle / Ivy

There is a newer version: 0.3.3
Show newest version
package io.ultreia.java4all.bean;

/*-
 * #%L
 * Java Beans extends by Ultreia.io
 * %%
 * Copyright (C) 2018 Ultreia.io
 * %%
 * This program 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 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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 General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * Created by tchemit on 07/01/2018.
 *
 * @author Tony Chemit - [email protected]
 */
public interface JavaBeanDefinition {

    /**
     * Ge the set of types usgin this definition.
     * This means that the method {@link JavaBeanDefinitionStore#getDefinition(Class)} will give this instance of definition
     * for any types of this set.
     *
     * @return set of types using this definition
     */
    Set> types();

    /**
     * @return dictionary of getters of this JavaBean
     */
    Map getters();

    /**
     * @return dictionary of setters of this JavaBean
     */
    Map setters();

    /**
     * @return set of property names preset both in getters and setters
     */
    Set copyPropertyNames();

    @SuppressWarnings("unchecked")
    default  O get(JavaBean javaBean, String propertyName) {
        Function getter = Objects.requireNonNull(getters().get(propertyName), String.format("Can't find getter for property %s on %s", propertyName, javaBean.getClass().getName()));
        return (O) getter.apply(javaBean);
    }

    @SuppressWarnings("unchecked")
    default  void set(JavaBean javaBean, String propertyName, O propertyValue) {
        BiConsumer setter = Objects.requireNonNull(setters().get(propertyName), String.format("Can't find a setter for property: %s on %s", propertyName, javaBean.getClass().getName()));
        setter.accept(javaBean, propertyValue);
    }

    default void copy(JavaBean source, JavaBean target, String propertyName) {
        Object o = get(source, propertyName);
        set(target, propertyName, o);
    }

    default void copy(JavaBean source, JavaBean target) {
        for (String propertyName : copyPropertyNames()) {
            copy(source, target, propertyName);
        }
    }

    @SuppressWarnings("unchecked")
    default void clear(JavaBean javaBean) {
        for (BiConsumer o : setters().values()) {
            try {
                o.accept(javaBean, null);
            } catch (Exception e) {
                o.accept(javaBean, 0);
            }
        }
    }

    @SuppressWarnings("unchecked")
    default  Function getter(String propertyName) {
        return getters().get(propertyName);
    }

    @SuppressWarnings("unchecked")
    default  BiConsumer setter(String propertyName) {
        return setters().get(propertyName);
    }

    default JavaBeanPredicateBuilder predicateBuilder() {
        throw new UnsupportedOperationException();
    }

    default JavaBeanComparatorBuilder comparatorBuilder() {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy