-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
09837 9578 advanced wire transformers (#9894)
Signed-off-by: Cody Littley <cody@swirldslabs.com>
- Loading branch information
1 parent
f00aa3c
commit 649ea48
Showing
32 changed files
with
940 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...s-common/src/main/java/com/swirlds/common/wiring/transformers/AdvancedTransformation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (C) 2023 Hedera Hashgraph, LLC | ||
* | ||
* 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.swirlds.common.wiring.transformers; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import edu.umd.cs.findbugs.annotations.Nullable; | ||
|
||
/** | ||
* Executes a transformation for an advanced transformer as created by | ||
* {@link com.swirlds.common.wiring.wires.output.OutputWire#buildAdvancedTransformer(String, AdvancedTransformation)}. | ||
* | ||
* @param <A> the original wire output type | ||
* @param <B> the output type of the transformer | ||
*/ | ||
public interface AdvancedTransformation<A, B> { | ||
|
||
/** | ||
* Given data that comes off of the original output wire, this method transforms it before it is passed to each | ||
* input wire that is connected to this transformer. Called once per data element per listener. | ||
* | ||
* @param a a data element from the original output wire | ||
* @return the transformed data element, or null if the data should not be forwarded | ||
*/ | ||
@Nullable | ||
B transform(@NonNull A a); | ||
|
||
/** | ||
* Called on the original data element after it has been forwarded to all listeners. This method can do cleanup if | ||
* necessary. Doing nothing is perfectly ok if the use case does not require cleanup. | ||
* | ||
* @param a the original data element | ||
*/ | ||
void cleanup(@NonNull A a); | ||
} |
78 changes: 78 additions & 0 deletions
78
...rc/main/java/com/swirlds/common/wiring/transformers/internal/AdvancedWireTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (C) 2023 Hedera Hashgraph, LLC | ||
* | ||
* 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.swirlds.common.wiring.transformers.internal; | ||
|
||
import com.swirlds.common.wiring.WiringModel; | ||
import com.swirlds.common.wiring.builders.TaskSchedulerType; | ||
import com.swirlds.common.wiring.wires.output.OutputWire; | ||
import com.swirlds.common.wiring.wires.output.internal.ForwardingOutputWire; | ||
import com.swirlds.common.wiring.wires.output.internal.TransformingOutputWire; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import edu.umd.cs.findbugs.annotations.Nullable; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Similar to a {@link WireTransformer} but for more advanced use cases. Unlike a {@link WireTransformer}, the | ||
* transforming function is called once per output per data item, and a special method can be called after the data is | ||
* forwarded to all destinations. | ||
* | ||
* @param <A> the input type | ||
* @param <B> the output type | ||
*/ | ||
public class AdvancedWireTransformer<A, B> implements Consumer<A> { | ||
|
||
private final ForwardingOutputWire<A, B> outputWire; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param model the wiring model containing this output wire | ||
* @param name the name of the output wire | ||
* @param transformer the function to transform the data from the input type to the output type. Is called once per | ||
* output per data item. If this method returns null then the data is not forwarded. | ||
* @param cleanup an optional method that is called after the data is forwarded to all destinations. The | ||
* original data is passed to this method. Ignored if null. | ||
*/ | ||
public AdvancedWireTransformer( | ||
@NonNull final WiringModel model, | ||
@NonNull final String name, | ||
@NonNull final Function<A, B> transformer, | ||
@Nullable final Consumer<A> cleanup) { | ||
|
||
model.registerVertex(name, TaskSchedulerType.DIRECT_STATELESS, true); | ||
outputWire = new TransformingOutputWire<>(model, name, transformer, cleanup); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void accept(@NonNull final A a) { | ||
outputWire.forward(a); | ||
} | ||
|
||
/** | ||
* Get the output wire for this transformer. | ||
* | ||
* @return the output wire | ||
*/ | ||
@NonNull | ||
public OutputWire<B> getOutputWire() { | ||
return outputWire; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.