001/*-
002 * #%L
003 * HAPI FHIR - Core Library
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.util.monad;
021
022// Companion class for Either. Defines factory methods for creating Either instances.
023public class Eithers {
024
025        private Eithers() {
026                // intentionally empty
027        }
028
029        /**
030         * Creates an Either instance from two values. If the left value is not null, the Either will be left. Otherwise, it will be right.
031         * Exactly one of the values must be present.
032         *
033         * @param <L> the left type
034         * @param <R> the right type
035         * @param left the left value
036         * @param right the right value
037         * @return an Either instance containing either left or right value
038         */
039        public static <L, R> Either<L, R> for2(L left, R right) {
040                return left != null ? forLeft(left) : forRight(right);
041        }
042
043        /**
044         * Creates an Either instance from a left value. Left cannot be null.
045         *
046         * @param <L> the left type
047         * @param <R> the right type
048         * @param left the left value
049         * @return an Either instance containing the left value
050         */
051        public static <L, R> Either<L, R> forLeft(L left) {
052                return new Either<>(left, null);
053        }
054
055        /**
056         * Creates an Either instance from a right value. Right cannot be null.
057         * @param <L> the left type
058         * @param <R> the right type
059         * @param right the right value
060         * @return an Either instance containing the right value
061         */
062        public static <L, R> Either<L, R> forRight(R right) {
063                return new Either<>(null, right);
064        }
065
066        /**
067         * Creates an Either3 instance from three values. If the left value is not null, the Either3 will be left. If the middle value is not null, the Either3 will be middle. Otherwise, it will be right.
068         * Exactly of the values must be present.
069         *
070         * @param <L> the left type
071         * @param <M> the middle type
072         * @param <R> the right type
073         * @param left the left value
074         * @param middle the middle value
075         * @param right the right value
076         * @return the Either3 instance containing either left, middle, or right value
077         */
078        public static <L, M, R> Either3<L, M, R> for3(L left, M middle, R right) {
079                if (left != null) {
080                        return forLeft3(left);
081                }
082
083                if (middle != null) {
084                        return forMiddle3(middle);
085                }
086
087                return forRight3(right);
088        }
089
090        /**
091         * Creates an Either3 instance from a left value. Left cannot be null.
092         *
093         * @param <L> the left type
094         * @param <M> the middle type
095         * @param <R>  the right type
096         * @param left the left value
097         * @return the Either3 instance containing the left value
098         */
099        public static <L, M, R> Either3<L, M, R> forLeft3(L left) {
100                return new Either3<>(left, null, null);
101        }
102
103        /**
104         * Creates an Either3 instance from a middle value. Middle cannot be null.
105         * @param <L> the left type
106         * @param <M> the middle type
107         * @param <R> the right type
108         * @param middle the middle value
109         * @return the Either3 instance containing the middle value
110         */
111        public static <L, M, R> Either3<L, M, R> forMiddle3(M middle) {
112                return new Either3<>(null, middle, null);
113        }
114
115        /**
116         * Creates an Either3 instance from a right value. Right cannot be null.
117         * @param <L> the left type
118         * @param <M> the middle type
119         * @param <R> the right type
120         * @param right the right value
121         * @return the Either3 instance containing the right value
122         */
123        public static <L, M, R> Either3<L, M, R> forRight3(R right) {
124                return new Either3<>(null, null, right);
125        }
126}