/* SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. * * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you 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. */ using System; namespace OpenSearch.Net.VirtualizedCluster.Rules { /// /// Represents the union of two types, and . /// Used to represent a rule with multiple return states /// /// The first type /// The second type public class RuleOption { internal readonly int Tag; internal readonly TFirst Item1; internal readonly TSecond Item2; /// /// Creates an new instance of that encapsulates value /// /// The value to encapsulate public RuleOption(TFirst item) { Item1 = item; Tag = 0; } /// /// Creates an new instance of that encapsulates value /// /// The value to encapsulate public RuleOption(TSecond item) { Item2 = item; Tag = 1; } /// /// Runs an delegate against the encapsulated value /// /// The delegate to run when this instance encapsulates an instance of /// The delegate to run when this instance encapsulates an instance of public void Match(Action first, Action second) { switch (Tag) { case 0: first(Item1); break; case 1: second(Item2); break; default: throw new Exception($"Unrecognized tag value: {Tag}"); } } /// /// Runs a delegate against the encapsulated value /// /// The delegate to run when this instance encapsulates an instance of /// The delegate to run when this instance encapsulates an instance of public T Match(Func first, Func second) { switch (Tag) { case 0: return first(Item1); case 1: return second(Item2); default: throw new Exception($"Unrecognized tag value: {Tag}"); } } public static implicit operator RuleOption(TFirst first) => new RuleOption(first); public static implicit operator RuleOption(TSecond second) => new RuleOption(second); } }