net.java.ao.schema.info.ImmutableEntityInfo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of activeobjects Show documentation
Show all versions of activeobjects Show documentation
This is the full Active Objects library, if you don't know which one to use, you probably want this one.
The newest version!
package net.java.ao.schema.info;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import net.java.ao.RawEntity;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
class ImmutableEntityInfo, K> implements EntityInfo {
private final Class entityType;
private final String tableName;
private final FieldInfo primaryKey;
private final Map fieldByName;
private final Map fieldByMethod;
ImmutableEntityInfo(
Class entityType,
String tableName,
Set fields) {
this.entityType = Objects.requireNonNull(entityType, "entityType");
this.tableName = Objects.requireNonNull(tableName, "tableName");
ImmutableMap.Builder fieldByNameBuilder = ImmutableMap.builder();
ImmutableMap.Builder fieldByMethodBuilder = ImmutableMap.builder();
FieldInfo primaryKey = null;
for (FieldInfo field : fields) {
fieldByNameBuilder.put(field.getName(), field);
if (field.getPolymorphicName() != null) {
fieldByNameBuilder.put(field.getPolymorphicName(), field);
}
if (field.isPrimary()) {
primaryKey = field;
}
if (field.hasAccessor()) {
fieldByMethodBuilder.put(field.getAccessor(), field);
}
if (field.hasMutator()) {
fieldByMethodBuilder.put(field.getMutator(), field);
}
}
fieldByName = fieldByNameBuilder.build();
fieldByMethod = fieldByMethodBuilder.build();
//noinspection unchecked
this.primaryKey = Objects.requireNonNull(primaryKey, "primaryKey");
}
@Override
public Class getEntityType() {
return entityType;
}
@Override
public String getName() {
return tableName;
}
@Override
public FieldInfo getPrimaryKey() {
return primaryKey;
}
@Override
public Set getFields() {
return ImmutableSet.copyOf(fieldByName.values());
}
@Override
public Set getFieldNames() {
return getFields().stream().map(FieldInfo.PLUCK_NAME).collect(Collectors.toSet());
}
@Override
public FieldInfo getField(Method method) {
return fieldByMethod.get(method);
}
@Override
public FieldInfo getField(String fieldName) {
return fieldByName.get(fieldName);
}
@Override
public boolean hasAccessor(Method method) {
FieldInfo field = fieldByMethod.get(method);
return field != null && method.equals(field.getAccessor());
}
@Override
public boolean hasMutator(Method method) {
FieldInfo field = fieldByMethod.get(method);
return field != null && method.equals(field.getMutator());
}
@Override
public boolean hasField(String fieldName) {
return fieldByName.containsKey(fieldName);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ImmutableEntityInfo that = (ImmutableEntityInfo) o;
return !(entityType != null ? !entityType.equals(that.entityType) : that.entityType != null);
}
@Override
public int hashCode() {
return entityType != null ? entityType.hashCode() : 0;
}
}