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

ac.simons.neo4j.migrations.core.EditionPrecondition Maven / Gradle / Ivy

/*
 * Copyright 2020-2022 the original author or 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
 *
 *      https://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 ac.simons.neo4j.migrations.core;

import ac.simons.neo4j.migrations.core.internal.Neo4jEdition;

import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Gerrit Meier
 * @author Michael J. Simons
 * @since 1.5.0
 */
final class EditionPrecondition extends AbstractPrecondition implements Precondition {

	private static final Pattern CONDITION_PATTERN = Pattern.compile("(?i)^.*?edition is *(?\\w++)?$");

	/**
	 * Checks if the {@code hint} is matched by the {@link #CONDITION_PATTERN} and if so, tries to build a factory  for
	 * a corresponding precondition.
	 *
	 * @param hint The complete hint
	 * @return A factory for a precondition or an empty optional if this factory doesn't match the hint
	 */
	static Optional> tryToParse(String hint) {

		Matcher matcher = CONDITION_PATTERN.matcher(hint);
		if (!matcher.matches()) {
			return Optional.empty();
		} else {
			try {
				Neo4jEdition edition = Neo4jEdition.of(matcher.group("edition"), false);
				return Optional.of(type -> new EditionPrecondition(type, edition));
			} catch (Exception e) {
				throw new IllegalArgumentException(
					String.format(
							"Wrong edition precondition %s. Usage: ` that edition is `.",
							Precondition.formattedHint(hint)));
			}
		}
	}

	static Neo4jEdition getEdition(ConnectionDetails connectionDetails) {

		return Neo4jEdition.of(connectionDetails.getServerEdition());
	}

	private final Neo4jEdition edition;

	private EditionPrecondition(Type type, Neo4jEdition edition) {
		super(type);
		this.edition = edition;
	}

	@Override
	public boolean isMet(MigrationContext migrationContext) {
		return getEdition(migrationContext.getConnectionDetails()).equals(edition);
	}

	Neo4jEdition getEdition() {
		return edition;
	}

	@Override
	public String toString() {
		return String.format("// %s that edition is %s", getType().keyword(), getEdition().name());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy