Skip to content

Commit

Permalink
Merge pull request #1 from falkreon/newevents
Browse files Browse the repository at this point in the history
Event system rework - generics and invokers
  • Loading branch information
falkreon committed Mar 2, 2024
2 parents 51346dd + fa5c254 commit fb7be3c
Show file tree
Hide file tree
Showing 15 changed files with 830 additions and 835 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021-2023 Falkreon (Isaac Ellingson)
Copyright (c) 2021-2024 Falkreon (Isaac Ellingson)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ plugins {

group = "blue.endless";
archivesBaseName = "TinyEvents";
version = "1.0.0";
version = "2.0.0";

repositories {
mavenCentral()
Expand Down
153 changes: 0 additions & 153 deletions src/main/java/blue/endless/tinyevents/BiConsumerEvent.java

This file was deleted.

171 changes: 171 additions & 0 deletions src/main/java/blue/endless/tinyevents/ChainEventFactories.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* MIT License
*
* Copyright (c) 2021-2024 Falkreon (Isaac Ellingson)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package blue.endless.tinyevents;

import java.util.function.BiFunction;
import java.util.function.DoubleUnaryOperator;
import java.util.function.IntUnaryOperator;
import java.util.function.LongUnaryOperator;
import java.util.function.UnaryOperator;

import blue.endless.tinyevents.Event.Entry;
import blue.endless.tinyevents.function.BooleanUnaryOperator;

/**
* Contains EventFactories and static factory methods for making Events which have the same inputs and outputs, and
* whose handlers "progressively modify" a value to create a result. There is no priority system for these events, and
* no handler can count on having the final say in the result.
*
* <p><b>Example:</b><br>
* A jump-strength-event. Let's say the event is an {@code Event<IntUnaryOperator>}, and that there are
* two handlers registered:
*
* <ul>
* <p>{@code (x) -> x + 1}
* <p>{@code (x) -> x / 2}
* </ul>
*
* The first handler receives the value 5, and adds one, yielding 6. The second one receives 6,
* and halves the value, yielding 3. The event's invoker returns with the value 3. Each handler receives the value "so
* far", and modifies it.
*/
public class ChainEventFactories {
public static final EventFactory<IntUnaryOperator> MODIFY_INT = ChainEventFactories::intUnaryOperator;
public static final EventFactory<LongUnaryOperator> MODIFY_LONG = ChainEventFactories::longUnaryOperator;
public static final EventFactory<DoubleUnaryOperator> MODIFY_DOUBLE = ChainEventFactories::doubleUnaryOperator;
public static final EventFactory<BooleanUnaryOperator> MODIFY_BOOLEAN= ChainEventFactories::booleanUnaryOperator;
public static final EventFactory<UnaryOperator<String>> MODIFY_STRING = () -> unaryOperator();

/**
* Creates an Event which takes in a single value and modifies it.
* @param <T> The type of data that will be modified
* @return the new Event
*/
public static <T> Event<UnaryOperator<T>> unaryOperator() {
return new Event<UnaryOperator<T>>(
(handlers) -> (T t) -> {
T result = t;

for(Entry<UnaryOperator<T>> entry : handlers) {
result = entry.handler().apply(result);
}

return result;
}
);
}

/**
* Creates an Event which takes in an int value and modifies it.
* @return the new Event
*/
public static Event<IntUnaryOperator> intUnaryOperator() {
return new Event<IntUnaryOperator>(
(handlers) -> (int value) -> {
int result = value;

for(Entry<IntUnaryOperator> entry : handlers) {
result = entry.handler().applyAsInt(result);
}

return result;
}
);
}

/**
* Creates an Event which takes in a long value and modifies it.
* @return the new Event
*/
public static Event<LongUnaryOperator> longUnaryOperator() {
return new Event<LongUnaryOperator>(
(handlers) -> (long value) -> {
long result = value;

for(Entry<LongUnaryOperator> entry : handlers) {
result = entry.handler().applyAsLong(result);
}

return result;
}
);
}

/**
* Creates an Event which takes in a double value and modifies it.
* @return the new Event
*/
public static Event<DoubleUnaryOperator> doubleUnaryOperator() {
return new Event<DoubleUnaryOperator>(
(handlers) -> (double value) -> {
double result = value;

for(Entry<DoubleUnaryOperator> entry : handlers) {
result = entry.handler().applyAsDouble(result);
}

return result;
}
);
}

/**
* Creates an Event which takes in a boolean value and modifies it.
* @return the new Event
*/
public static Event<BooleanUnaryOperator> booleanUnaryOperator() {
return new Event<BooleanUnaryOperator>(
(handlers) -> (boolean value) -> {
boolean result = value;

for(Entry<BooleanUnaryOperator> entry : handlers) {
result = entry.handler().applyAsBoolean(result);
}

return result;
}
);
}

/**
* Creates an Event which takes in two values and modifies the first one.
* @param <T> The type of data that will be modified
* @param <U> The type of the second function argument that will NOT be replaced/modified
* @return The fully-modified value.
*/
public static <T, U> Event<BiFunction<T, U, T>> biFunction() {
return new Event<BiFunction<T, U, T>>(
(handlers) -> (T t, U u) -> {
T result = t;

for(Entry<BiFunction<T, U, T>> entry : handlers) {
result = entry.handler().apply(result, u);
}

return result;
}
);
}
}
Loading

0 comments on commit fb7be3c

Please sign in to comment.