com.aliyun.datahub.client.model.RecordSchema Maven / Gradle / Ivy
The newest version!
package com.aliyun.datahub.client.model;
import com.aliyun.datahub.client.exception.InvalidParameterException;
import com.aliyun.datahub.client.impl.serializer.RecordSchemaDeserializer;
import com.aliyun.datahub.client.impl.serializer.RecordSchemaSerializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.util.*;
@JsonSerialize(using = RecordSchemaSerializer.class)
@JsonDeserialize(using = RecordSchemaDeserializer.class)
public class RecordSchema {
/** Schema contains a Field array, which you can treat as data Column */
private BitSet notNullFields = new BitSet();
private List fieldList = new ArrayList<>();
private Map fieldIndexMap = new HashMap<>();
public void addField(Field field) {
if (field == null) {
throw new InvalidParameterException("Field is null");
}
if (fieldIndexMap.containsKey(field.getName())) {
throw new InvalidParameterException("Field [" + field.getName() + "] already exists");
}
fieldList.add(field);
fieldIndexMap.put(field.getName(), fieldList.size() - 1);
if (!field.isAllowNull()) {
notNullFields.set(fieldList.size() - 1);
}
}
public Field getField(String name) {
return fieldList.get(getFieldIndex(name));
}
public Field getField(int idx) {
if (idx < 0 || idx >= fieldList.size()) {
throw new InvalidParameterException("idx out of range");
}
return fieldList.get(idx);
}
public int getFieldIndex(String name) {
if (name == null) {
throw new InvalidParameterException("Field name is null");
}
Integer idx = fieldIndexMap.get(name.toLowerCase());
if (idx == null) {
throw new InvalidParameterException("Field not exists " + name);
}
return idx;
}
public List getFields() {
return fieldList;
}
public boolean containsField(String filedName) {
if (filedName == null) {
throw new InvalidParameterException("Field name is null");
}
return fieldIndexMap.containsKey(filedName.toLowerCase());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof RecordSchema)) return false;
RecordSchema that = (RecordSchema) o;
return Objects.equals(fieldList, that.fieldList);
}
@Override
public int hashCode() {
return Objects.hash(fieldList);
}
public BitSet getNotNullFields() {
return notNullFields;
}
}