com.io7m.trasco.api.TrSchemaRevisionSet Maven / Gradle / Ivy
/*
* Copyright © 2022 Mark Raynsford https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package com.io7m.trasco.api;
import java.math.BigInteger;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Objects;
/**
* A set of schema revisions.
*
* @param parameters The parameters
* @param revisions The revisions
*/
public record TrSchemaRevisionSet(
Map parameters,
NavigableMap revisions)
{
/**
* A set of schema revisions.
*
* @param parameters The parameters
* @param revisions The revisions
*/
public TrSchemaRevisionSet
{
Objects.requireNonNull(parameters, "parameters");
Objects.requireNonNull(revisions, "revisions");
if (revisions.size() >= 2) {
BigInteger previous = null;
for (final var current : revisions.keySet()) {
if (previous != null) {
if (!current.subtract(previous).equals(BigInteger.ONE)) {
throw new IllegalArgumentException(
String.format(
"Revision versions must always increment by 1 (received %s followed by %s)",
previous,
current
));
}
}
previous = current;
}
}
for (final var revision : revisions.values()) {
for (final var statement : revision.statements()) {
if (statement instanceof final TrStatementParameterized parameterized) {
final var values =
parameterized.references().byName().values();
for (final var parameter : values) {
if (!parameters.containsKey(parameter.name())) {
throw new IllegalArgumentException(
String.format(
"Revision %s specifies a reference to a nonexistent parameter %s",
revision.version(),
parameter
));
}
}
}
}
}
}
}