001/*-
002 * #%L
003 * HAPI FHIR Server - SQL Migration
004 * %%
005 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.migrate.taskdef;
021
022import java.util.Objects;
023import java.util.StringJoiner;
024import java.util.function.Supplier;
025
026/**
027 * Contains a pre-built precondition to evaluate once {@link BaseTask#execute()} is called.
028 * <p/>
029 * Includes both a {@link Supplier} containing the logic to determine if the precondition evaluates to true or false and
030 * a reason String to output to the logs if the precondition evaluates to false and halts execution of the task.
031 */
032public class ExecuteTaskPrecondition {
033        private final Supplier<Boolean> myPreconditionRunner;
034        private final String myPreconditionReason;
035
036        public ExecuteTaskPrecondition(Supplier<Boolean> thePreconditionRunner, String thePreconditionReason) {
037                myPreconditionRunner = thePreconditionRunner;
038                myPreconditionReason = thePreconditionReason;
039        }
040
041        public Supplier<Boolean> getPreconditionRunner() {
042                return myPreconditionRunner;
043        }
044
045        public String getPreconditionReason() {
046                return myPreconditionReason;
047        }
048
049        @Override
050        public boolean equals(Object theO) {
051                if (this == theO) {
052                        return true;
053                }
054                if (theO == null || getClass() != theO.getClass()) {
055                        return false;
056                }
057                ExecuteTaskPrecondition that = (ExecuteTaskPrecondition) theO;
058                return Objects.equals(myPreconditionRunner, that.myPreconditionRunner)
059                                && Objects.equals(myPreconditionReason, that.myPreconditionReason);
060        }
061
062        @Override
063        public int hashCode() {
064                return Objects.hash(myPreconditionRunner, myPreconditionReason);
065        }
066
067        @Override
068        public String toString() {
069                return new StringJoiner(", ", ExecuteTaskPrecondition.class.getSimpleName() + "[", "]")
070                                .add("myPreconditionRunner=" + myPreconditionRunner)
071                                .add("myPreconditionReason='" + myPreconditionReason + "'")
072                                .toString();
073        }
074}