Package io.fathom.cloud.state

Source Code of io.fathom.cloud.state.CollectionBuilderBase

package io.fathom.cloud.state;

import io.fathom.cloud.protobuf.ProtobufUtils;
import io.fathom.cloud.state.StateStore.StateNode;

import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.GeneratedMessage;
import com.google.protobuf.GeneratedMessage.Builder;

public abstract class CollectionBuilderBase<T> {
    final StateNode parentNode;
    final Class<T> protobufClass;
    protected Integer idFieldNumber;
    protected Integer keyFieldNumber;
    protected final Builder template;

    public CollectionBuilderBase(StateNode parentNode, Class<T> protobufClass) {
        this.parentNode = parentNode;
        this.protobufClass = protobufClass;

        this.template = (GeneratedMessage.Builder) ProtobufUtils.newBuilder(protobufClass);
    }

    public CollectionBuilderBase<T> idField(int idFieldNumber) {
        this.idFieldNumber = idFieldNumber;
        return this;
    }

    public CollectionBuilderBase<T> keyField(int keyFieldNumber) {
        this.keyFieldNumber = keyFieldNumber;
        return this;
    }

    public abstract ItemCollection create();

    protected FieldDescriptor getKeyField() {
        FieldDescriptor keyField = null;
        if (keyFieldNumber != null) {
            keyField = template.getDescriptorForType().findFieldByNumber(keyFieldNumber);
        }
        return keyField;
    }

    protected FieldDescriptor getIdField(GeneratedMessage.Builder template) {
        FieldDescriptor idField = null;
        if (idFieldNumber != null) {
            idField = template.getDescriptorForType().findFieldByNumber(idFieldNumber);
        } else {
            throw new IllegalArgumentException();
        }
        return idField;
    }
}
TOP

Related Classes of io.fathom.cloud.state.CollectionBuilderBase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.