io.ultreia.java4all.bean.definition.AbstractJavaBeanDefinition Maven / Gradle / Ivy
package io.ultreia.java4all.bean.definition;
/*-
* #%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 com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.ultreia.java4all.bean.JavaBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public abstract class AbstractJavaBeanDefinition implements JavaBeanDefinition {
private ImmutableSet> acceptedTypes;
private ImmutableMap> properties;
private Class> mainType;
@Override
public final ImmutableSet> types() {
return acceptedTypes == null ? acceptedTypes = Objects.requireNonNull(loadAcceptedTypes()) : acceptedTypes;
}
protected abstract ImmutableSet> loadAcceptedTypes();
protected abstract ImmutableMap> loadTypes();
protected abstract ImmutableMap> loadGetters();
protected abstract ImmutableMap> loadSetters();
@SuppressWarnings("unchecked")
@Override
public final ImmutableMap> properties() {
if (properties == null) {
ImmutableMap> getters = Objects.requireNonNull(loadGetters());
ImmutableMap> setters = Objects.requireNonNull(loadSetters());
ImmutableMap> types = Objects.requireNonNull(loadTypes());
List allPropertyNames = Stream.concat(getters.keySet().stream(), setters.keySet().stream()).distinct().sorted(String::compareTo).collect(Collectors.toList());
ImmutableMap.Builder> propertiesBuilder = ImmutableMap.builder();
for (String propertyName : allPropertyNames) {
Class type = types.get(propertyName);
Function getter = getters.get(propertyName);
BiConsumer setter = setters.get(propertyName);
propertiesBuilder.put(propertyName, new JavaBeanPropertyDefinition<>(propertyName, type, getter, setter));
}
this.properties = propertiesBuilder.build();
}
return properties;
}
@SuppressWarnings("unchecked")
@Override
public final O newInstance() {
try {
return (O) mainType().getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new IllegalStateException("Can't instantiate with: " + mainType, e.getCause());
}
}
private Class> mainType() {
if (mainType == null) {
for (Class> acceptedType : acceptedTypes) {
if (acceptedType.isInterface() || Modifier.isAbstract(acceptedType.getModifiers())) {
continue;
}
mainType = acceptedType;
}
Objects.requireNonNull(mainType, "Can't find a type to instantiate in: " + acceptedTypes);
}
return mainType;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy