|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Hedera Hashgraph, LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.swirlds.common.wiring.schedulers.internal; |
| 18 | + |
| 19 | +import com.swirlds.common.wiring.model.internal.StandardWiringModel; |
| 20 | +import com.swirlds.common.wiring.schedulers.TaskScheduler; |
| 21 | +import com.swirlds.common.wiring.schedulers.builders.TaskSchedulerType; |
| 22 | +import com.swirlds.common.wiring.wires.input.BindableInputWire; |
| 23 | +import com.swirlds.common.wiring.wires.input.NoOpInputWire; |
| 24 | +import com.swirlds.common.wiring.wires.output.NoOpOutputWire; |
| 25 | +import com.swirlds.common.wiring.wires.output.StandardOutputWire; |
| 26 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.function.Consumer; |
| 29 | + |
| 30 | +/** |
| 31 | + * A no-op task scheduler that does nothing. |
| 32 | + * |
| 33 | + * @param <OUT> the output type of the scheduler (use {@link Void} for a task scheduler with no output type). This is |
| 34 | + * just to appease the compiler, as this scheduler never produces output. |
| 35 | + */ |
| 36 | +public class NoOpTaskScheduler<OUT> extends TaskScheduler<OUT> { |
| 37 | + |
| 38 | + private final StandardWiringModel model; |
| 39 | + |
| 40 | + /** |
| 41 | + * Constructor. |
| 42 | + * |
| 43 | + * @param model the wiring model containing this task scheduler |
| 44 | + * @param name the name of the task scheduler |
| 45 | + * @param type the type of task scheduler |
| 46 | + * @param flushEnabled if true, then {@link #flush()} will be enabled, otherwise it will throw. |
| 47 | + * @param squelchingEnabled if true, then squelching will be enabled, otherwise trying to squelch will throw. |
| 48 | + */ |
| 49 | + public NoOpTaskScheduler( |
| 50 | + @NonNull final StandardWiringModel model, |
| 51 | + @NonNull final String name, |
| 52 | + @NonNull final TaskSchedulerType type, |
| 53 | + final boolean flushEnabled, |
| 54 | + final boolean squelchingEnabled) { |
| 55 | + super(model, name, type, flushEnabled, squelchingEnabled, false); |
| 56 | + |
| 57 | + this.model = Objects.requireNonNull(model); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * {@inheritDoc} |
| 62 | + */ |
| 63 | + @Override |
| 64 | + public long getUnprocessedTaskCount() { |
| 65 | + return 0; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritDoc} |
| 70 | + */ |
| 71 | + @Override |
| 72 | + public void flush() { |
| 73 | + throwIfFlushDisabled(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * {@inheritDoc} |
| 78 | + */ |
| 79 | + @Override |
| 80 | + protected void put(@NonNull final Consumer<Object> handler, @NonNull final Object data) { |
| 81 | + throw new UnsupportedOperationException( |
| 82 | + "Data should have been discarded before being sent to this no-op scheduler"); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * {@inheritDoc} |
| 87 | + */ |
| 88 | + @Override |
| 89 | + protected boolean offer(@NonNull final Consumer<Object> handler, @NonNull final Object data) { |
| 90 | + throw new UnsupportedOperationException( |
| 91 | + "Data should have been discarded before being sent to this no-op scheduler"); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * {@inheritDoc} |
| 96 | + */ |
| 97 | + @Override |
| 98 | + protected void inject(@NonNull final Consumer<Object> handler, @NonNull final Object data) { |
| 99 | + throw new UnsupportedOperationException( |
| 100 | + "Data should have been discarded before being sent to this no-op scheduler"); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * {@inheritDoc} |
| 105 | + */ |
| 106 | + @NonNull |
| 107 | + @Override |
| 108 | + protected StandardOutputWire<OUT> buildPrimaryOutputWire( |
| 109 | + @NonNull final StandardWiringModel model, @NonNull final String name) { |
| 110 | + return new NoOpOutputWire<>(model, getName()); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * {@inheritDoc} |
| 115 | + */ |
| 116 | + @NonNull |
| 117 | + @Override |
| 118 | + public <T> StandardOutputWire<T> buildSecondaryOutputWire() { |
| 119 | + return new NoOpOutputWire<>(model, getName()); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * {@inheritDoc} |
| 124 | + */ |
| 125 | + @NonNull |
| 126 | + @Override |
| 127 | + public <I> BindableInputWire<I, OUT> buildInputWire(@NonNull final String name) { |
| 128 | + return new NoOpInputWire<>(model, this, name); |
| 129 | + } |
| 130 | +} |
0 commit comments