com.infomaximum.database.schema.Field Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rdao Show documentation
Show all versions of rdao Show documentation
Library for creating a light cluster
The newest version!
package com.infomaximum.database.schema;
import com.infomaximum.database.exception.IllegalTypeException;
import com.infomaximum.database.exception.StructEntityException;
import com.infomaximum.database.utils.TypeConvert;
import java.io.Serializable;
public class Field {
private final int number;
private final String name;
private final byte[] nameBytes;
private final Class extends Serializable> type;
private final TypeConverter converter;
private final StructEntity foreignDependency;
Field(com.infomaximum.database.anotation.Field field, StructEntity parent) {
this.number = field.number();
this.name = field.name();
this.nameBytes = TypeConvert.pack(field.name());
this.type = field.type();
this.converter = buildPacker(field.packerType());
if (field.foreignDependency() != Class.class) {
if (parent.getObjectClass() != field.foreignDependency()) {
this.foreignDependency = Schema.resolve(field.foreignDependency());
} else {
this.foreignDependency = parent;
}
} else {
this.foreignDependency = null;
}
if (isForeign() && this.type != Long.class) {
throw new StructEntityException("Type of foreign field " + field.name() + " must be " + Long.class + ".");
}
}
public int getNumber() {
return number;
}
public String getName() {
return name;
}
public byte[] getNameBytes() {
return nameBytes;
}
public Class extends Serializable> getType() {
return type;
}
public TypeConverter getConverter() {
return converter;
}
public boolean isForeign() {
return foreignDependency != null;
}
public StructEntity getForeignDependency() {
return foreignDependency;
}
public void throwIfNotMatch(Class type) {
if (this.type != type) {
throw new IllegalTypeException(this.type, type);
}
}
private static TypeConverter buildPacker(Class> packerClass) {
if (packerClass == Class.class) {
return null;
}
try {
return (TypeConverter) packerClass.newInstance();
} catch (ReflectiveOperationException e) {
throw new IllegalTypeException(e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy