All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.palantir.atlasdb.table.description.constraints.ConstraintMetadata Maven / Gradle / Ivy

There is a newer version: 0.1193.0
Show newest version
/*
 * (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
 *
 * 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.palantir.atlasdb.table.description.constraints;

import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public final class ConstraintMetadata {
    private final List rowConstraints;
    private final List tableConstraints;
    private final List foreignKeyConstraints;

    public static Builder builder() {
        return new Builder();
    }

    public static ConstraintMetadata none() {
        return new ConstraintMetadata(new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
    }

    private ConstraintMetadata(
            List rowConstraints,
            List tableConstraints,
            List foreignKeyConstraints) {
        this.rowConstraints = ImmutableList.copyOf(rowConstraints);
        this.tableConstraints = ImmutableList.copyOf(tableConstraints);
        this.foreignKeyConstraints = ImmutableList.copyOf(foreignKeyConstraints);
    }

    public List getRowConstraints() {
        return rowConstraints;
    }

    public List getTableConstraints() {
        return tableConstraints;
    }

    public List getForeignKeyConstraints() {
        return foreignKeyConstraints;
    }

    public boolean isEmpty() {
        return rowConstraints.isEmpty() && tableConstraints.isEmpty() && foreignKeyConstraints.isEmpty();
    }

    public Set getAllRowVariableNames() {
        Set names = new HashSet<>();
        for (RowConstraintMetadata constraint : rowConstraints) {
            names.addAll(constraint.getRowVariables());
        }
        for (ForeignKeyConstraintMetadata constraint : foreignKeyConstraints) {
            names.addAll(constraint.getRowVariables());
        }
        return names;
    }

    public Set getAllColumnVariableNames() {
        Set names = new HashSet<>();
        for (RowConstraintMetadata constraint : rowConstraints) {
            names.addAll(constraint.getColumnVariables());
        }
        for (ForeignKeyConstraintMetadata constraint : foreignKeyConstraints) {
            names.addAll(constraint.getColumnVariables());
        }
        return names;
    }

    public static final class Builder {
        private final List rowConstraints = new ArrayList<>();
        private final List tableConstraints = new ArrayList<>();
        private final List foreignKeyConstraints = new ArrayList<>();

        Builder() {
            /**/
        }

        public Builder addRowConstraint(RowConstraintMetadata constraint) {
            rowConstraints.add(constraint);
            return this;
        }

        public Builder addRowConstraints(List constraints) {
            rowConstraints.addAll(constraints);
            return this;
        }

        public Builder addTableConstraint(TableConstraint constraint) {
            tableConstraints.add(constraint);
            return this;
        }

        public Builder addTableConstraints(List constraints) {
            tableConstraints.addAll(constraints);
            return this;
        }

        public Builder addForeignKeyConstraint(ForeignKeyConstraintMetadata constraint) {
            foreignKeyConstraints.add(constraint);
            return this;
        }

        public Builder addForeignKeyConstraints(List constraints) {
            foreignKeyConstraints.addAll(constraints);
            return this;
        }

        public ConstraintMetadata build() {
            return new ConstraintMetadata(rowConstraints, tableConstraints, foreignKeyConstraints);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy