-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
273 additions
and
207 deletions.
There are no files selected for viewing
201 changes: 0 additions & 201 deletions
201
timelines/src/main/java/gg/xp/xivsupport/timelines/CactbotEventTypes.java
This file was deleted.
Oops, something went wrong.
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
22 changes: 22 additions & 0 deletions
22
timelines/src/main/java/gg/xp/xivsupport/timelines/cbevents/CbConversion.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,22 @@ | ||
package gg.xp.xivsupport.timelines.cbevents; | ||
|
||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Represents a conversion from some field (possibly nested) on an event, to a predicate that matches events. | ||
* All cactbot netregices use string values regardless of the underlying data type, so this always takes a string. | ||
* | ||
* @param <X> The event type. | ||
*/ | ||
@FunctionalInterface | ||
interface CbConversion<X> { | ||
/** | ||
* Example: on a 21-line, we want to check if the ability ID is "12AB". | ||
* We would call this with "12AB" as the argument, and it should return a predicate that checks that | ||
* a given AbilityUsedEvent has an ability ID of 0x12AB. | ||
* | ||
* @param input The input string. | ||
* @return The resulting predicate. | ||
*/ | ||
Predicate<X> convert(String input); | ||
} |
84 changes: 84 additions & 0 deletions
84
timelines/src/main/java/gg/xp/xivsupport/timelines/cbevents/CbConversions.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,84 @@ | ||
package gg.xp.xivsupport.timelines.cbevents; | ||
|
||
import gg.xp.xivsupport.events.actlines.events.NameIdPair; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.function.Function; | ||
import java.util.regex.Pattern; | ||
|
||
class CbConversions { | ||
|
||
/** | ||
* Convenience function for quickly making a ConvToCondition on an integer/long field. | ||
* If the input string in the resulting ConvToCondition is a plain number (and not something that would require | ||
* us to actually do regex), then the numbers will be compared directly. | ||
* | ||
* @param getter A function for getting the required value out of our event. | ||
* @param base The numerical base, typically 10 or 16 | ||
* @param <X> The event type | ||
* @return The condition matching the above requirements. | ||
*/ | ||
static <X> CbConversion<X> intConv(Function<X, Long> getter, int base) { | ||
return intConv(getter, base, 0); | ||
} | ||
|
||
/** | ||
* Convenience function for quickly making a ConvToCondition on an integer/long field. | ||
* If the input string in the resulting ConvToCondition is a plain number (and not something that would require | ||
* us to actually do regex), then the numbers will be compared directly. | ||
* <p> | ||
* This version of the method allows you to specify that the number should be left-padded to a minimum number of | ||
* characters, with zeroes. e.g. if the input is "00", and the value is "0", then in order for that to match, you | ||
* would need to specify minDigits == 2. | ||
* | ||
* @param getter A function for getting the required value out of our event. | ||
* @param base The numerical base, typically 10 or 16 | ||
* @param minDigits If ACT would left-pad the number with zeroes, then you should specify the minimum length | ||
* of the number here so that the value can be similarly padded out. | ||
* @param <X> The event type | ||
* @return The condition matching the above requirements. | ||
*/ | ||
static <X> CbConversion<X> intConv(Function<X, Long> getter, int base, int minDigits) { | ||
return str -> { | ||
try { | ||
// Fast path - input is a number literal, so do a direct number comparison | ||
long parsed = Long.parseLong(str, base); | ||
return item -> getter.apply(item) == parsed; | ||
} | ||
catch (NumberFormatException ignored) { | ||
// Slow path - input is a regex, so compile to regex first | ||
Pattern pattern = Pattern.compile(str, Pattern.CASE_INSENSITIVE); | ||
return item -> { | ||
String asString = Long.toString(getter.apply(item), base); | ||
if (minDigits > 1) { | ||
asString = StringUtils.leftPad(asString, minDigits, '0'); | ||
} | ||
return pattern.matcher(asString).matches(); | ||
}; | ||
} | ||
}; | ||
} | ||
|
||
static <X> CbConversion<X> strConv(Function<X, String> getter) { | ||
return str -> { | ||
Pattern pattern = Pattern.compile(str); | ||
return item -> pattern.matcher(getter.apply(item)).matches(); | ||
}; | ||
} | ||
|
||
static <X> CbConversion<X> id(Function<X, NameIdPair> getter) { | ||
return intConv(e -> getter.apply(e).getId(), 16); | ||
} | ||
|
||
static <X> CbConversion<X> named(Function<X, NameIdPair> getter) { | ||
return strConv(e -> getter.apply(e).getName()); | ||
} | ||
|
||
static <X> CbConversion<X> boolToInt(Function<X, Boolean> getter) { | ||
return str -> switch (str) { | ||
case "0" -> (item -> !getter.apply(item)); | ||
case "1" -> (getter::apply); | ||
default -> throw new IllegalArgumentException("Expected 0 or 1, got '%s'".formatted(str)); | ||
}; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
timelines/src/main/java/gg/xp/xivsupport/timelines/cbevents/CbEventDesc.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,12 @@ | ||
package gg.xp.xivsupport.timelines.cbevents; | ||
|
||
import gg.xp.reevent.events.Event; | ||
|
||
import java.util.List; | ||
|
||
public interface CbEventDesc<X extends Event> { | ||
|
||
Class<X> getEventType(); | ||
List<CbfMap<? super X>> getFieldMappings(); | ||
|
||
} |
Oops, something went wrong.