fr.ird.observe.dto.reference.DtoReferenceDefinition Maven / Gradle / Ivy
Show all versions of common-dto Show documentation
package fr.ird.observe.dto.reference;
/*-
* #%L
* ObServe Toolkit :: Common Dto
* %%
* Copyright (C) 2017 - 2020 IRD, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import fr.ird.observe.dto.IdDto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.Serializable;
import java.util.function.Function;
/**
* Describe the definition of a reference on a dto.
*
* Created on 11/11/15.
*
* @author Tony Chemit - [email protected]
*/
public abstract class DtoReferenceDefinition> implements Serializable {
private static final long serialVersionUID = 1L;
/** Logger. */
private static final Logger log = LogManager.getLogger(DtoReferenceDefinition.class);
private final Class dtoType;
private final Class referenceType;
private final ImmutableMap> properties;
private final ImmutableMap> functions;
protected DtoReferenceDefinition(Class dtoType, Class referenceType, ImmutableMap> properties, ImmutableMap> functions) {
this.dtoType = dtoType;
this.referenceType = referenceType;
this.properties = properties;
this.functions = functions;
}
public Class getType() {
return referenceType;
}
public Class getDtoType() {
return dtoType;
}
public ImmutableSet getPropertyNames() {
return properties.keySet();
}
Function getPropertyFunction(String propertyName) {
//noinspection unchecked
return (Function) functions.get(propertyName);
}
@Override
public String toString() {
MoreObjects.ToStringHelper toStringHelper = MoreObjects.toStringHelper(this)
.add("referenceType", referenceType.getSimpleName());
if (log.isDebugEnabled()) {
toStringHelper.add("properties", properties);
}
return toStringHelper.toString();
}
public abstract static class Builder, X extends DtoReferenceDefinition> {
final Class dtoType;
final Class referenceType;
final ImmutableMap.Builder> propertiesBuilder;
final ImmutableMap.Builder> functionsBuilder;
public Builder(Class dtoType, Class referenceType) {
this.dtoType = dtoType;
this.referenceType = referenceType;
propertiesBuilder = ImmutableMap.builder();
functionsBuilder = ImmutableMap.builder();
}
public Builder addProperty(Class type, String name, Function function) {
propertiesBuilder.put(name, type);
functionsBuilder.put(name, function);
return this;
}
public Class getType() {
return referenceType;
}
public abstract X build();
}
}