/*
 * RecordTypeIndexesBuilder.java
 *
 * This source file is part of the FoundationDB open source project
 *
 * Copyright 2015-2018 Apple Inc. and the FoundationDB project authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.apple.foundationdb.record.metadata;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

/**
 * A base builder for {@link RecordType}.
 *
 * Contains:
 * <ul>
 * <li>name.</li>
 * <li>optional secondary indexes.</li>
 * <li>record type key.</li>
 * </ul>
 */
@SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod")
public abstract class RecordTypeIndexesBuilder {
    @Nonnull
    protected final String name;
    @Nonnull
    protected final List<Index> indexes;
    @Nonnull
    protected final List<Index> multiTypeIndexes;
    @Nullable
    protected Object recordTypeKey;

    protected RecordTypeIndexesBuilder(@Nonnull String name) {
        this.name = name;
        this.indexes = new ArrayList<>();
        this.multiTypeIndexes = new ArrayList<>();
    }

    protected RecordTypeIndexesBuilder(@Nonnull String name, @Nonnull RecordTypeIndexesBuilder other) {
        this.name = name;
        this.indexes = new ArrayList<>(other.indexes);
        this.multiTypeIndexes = new ArrayList<>(other.multiTypeIndexes);
        this.recordTypeKey = other.recordTypeKey;
    }

    @Nonnull
    public String getName() {
        return name;
    }

    @Nonnull
    public List<Index> getIndexes() {
        return indexes;
    }

    /**
     * The {@link Index}es that this record type is on that also contain other record types.
     * @return a list of all indexes that include this record type along with other types.
     */
    @Nonnull
    public List<Index> getMultiTypeIndexes() {
        return multiTypeIndexes;
    }

    @Nullable
    public Object getRecordTypeKey() {
        return recordTypeKey;
    }

    public RecordTypeIndexesBuilder setRecordTypeKey(@Nullable Object recordTypeKey) {
        if (!(recordTypeKey == null || recordTypeKey instanceof Number || recordTypeKey instanceof Boolean ||
                recordTypeKey instanceof String || recordTypeKey instanceof byte[])) {
            throw new MetaDataException("Only primitive types are allowed as record type key");
        }
        this.recordTypeKey = TupleTypeUtil.toTupleEquivalentValue(recordTypeKey);
        return this;
    }
}
