Skip to content

Commit 995d063

Browse files
committed
package publication
1 parent 1e8fe27 commit 995d063

File tree

8 files changed

+203
-99
lines changed

8 files changed

+203
-99
lines changed

.github/workflows/publish.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Publish package to the Maven Central Repository
2+
on:
3+
release:
4+
types: [created]
5+
jobs:
6+
publish:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
- name: Set up Java
11+
uses: actions/setup-java@v3
12+
with:
13+
java-version: '11'
14+
distribution: 'adopt'
15+
- name: Validate Gradle wrapper
16+
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
17+
- name: Publish package
18+
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
19+
with:
20+
arguments: publish
21+
env:
22+
SECRET_USERNAME: ${{ secrets.SECRET_USERNAME }}
23+
SECRET_TOKEN: ${{ secrets.SECRET_TOKEN }}
24+
SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }}
25+
SIGNING_SECRET_KEY: ${{ secrets.SIGNING_SECRET_KEY }}

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"files.trimTrailingWhitespace": false,
55
"spotlessGradle.diagnostics.enable": true,
66
"spotlessGradle.format.enable": true,
7-
"editor.defaultFormatter": "richardwillis.vscode-spotless-gradle",
7+
"editor.defaultFormatter": "ms-vsliveshare.vsliveshare",
88
"editor.codeActionsOnSave": {
99
"source.fixAll.spotlessGradle": true
1010
}

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ subprojects {
3333
formatAnnotations()
3434
}
3535
}
36-
}
36+
}

gradlew

100644100755
File mode changed.

lib/build.gradle

+97
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
plugins {
1010
// Apply the application plugin to add support for building a CLI application in Java.
1111
id 'java-library'
12+
id 'maven-publish'
13+
id 'signing'
1214
}
1315

1416
repositories {
@@ -28,3 +30,98 @@ tasks.named('test') {
2830
// Use JUnit Platform for unit tests.
2931
useJUnitPlatform()
3032
}
33+
34+
test {
35+
testLogging {
36+
exceptionFormat = 'full'
37+
}
38+
}
39+
40+
task javadocJar(type: Jar) {
41+
classifier = 'javadoc'
42+
from javadoc
43+
}
44+
45+
task sourcesJar(type: Jar) {
46+
classifier = 'sources'
47+
from sourceSets.main.allSource
48+
}
49+
50+
artifacts {
51+
archives javadocJar, sourcesJar
52+
}
53+
54+
java {
55+
withJavadocJar()
56+
withSourcesJar()
57+
}
58+
59+
group = "com.11329icerobotics.jfinite"
60+
archivesBaseName = "jfinite"
61+
version = "1.0.0"
62+
63+
publishing {
64+
repositories {
65+
maven {
66+
name = "GitHubPackages"
67+
url = uri("https://maven.pkg.github.com/11329icerobotics/jfinite")
68+
credentials {
69+
username = System.getenv("SECRET_USERNAME")
70+
password = System.getenv("SECRET_TOKEN")
71+
}
72+
}
73+
}
74+
publications {
75+
gpr(MavenPublication) {
76+
artifactId = 'jfinite'
77+
from components.java
78+
79+
pom {
80+
name = 'jfinite'
81+
packaging = 'jar'
82+
// optionally artifactId can be defined here
83+
description = 'A lightweight tool to define finite state machines in Java'
84+
url = 'https://github.com/11329ICERobotics/JFinite'
85+
86+
scm {
87+
connection = 'scm:git:https://github.com/11329ICERobotics/JFinite.git'
88+
developerConnection = 'scm:git:https://github.com/11329ICERobotics/JFinite.git'
89+
url = 'https://github.com/11329ICERobotics/JFinite'
90+
}
91+
92+
licenses {
93+
license {
94+
name = 'The Apache License, Version 2.0'
95+
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
96+
}
97+
}
98+
99+
developers {
100+
developer {
101+
id = 'FizzyApple12'
102+
name = 'Hazel Roeder'
103+
email = 'davideroeder@gmail.com'
104+
}
105+
developer {
106+
id = 'Kennan'
107+
name = 'Kennan Hunter'
108+
email = 'kennanhunter5@gmail.com'
109+
}
110+
developer {
111+
id = 'SB510'
112+
name = 'Sean Borneman'
113+
email = 'sean.borneman@gmail.com'
114+
}
115+
}
116+
}
117+
}
118+
}
119+
}
120+
121+
signing {
122+
sign publishing.publications.gpr
123+
useInMemoryPgpKeys(
124+
System.getenv('SIGNING_SECRET_KEY'),
125+
System.getenv('SIGNING_PASSWORD')
126+
)
127+
}

lib/src/main/java/jfinite/AnnotationTest.java

-86
This file was deleted.

lib/src/main/java/jfinite/StateMachine.java

+43-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
public class StateMachine<T extends Enum<T>> {
99

1010
protected T currentState;
11+
1112
protected HashMap<T, List<StateBehaviour>> behaviours = new HashMap<>();
13+
protected HashMap<T, List<StateBehaviour>> loopedBehaviours = new HashMap<>();
14+
protected HashMap<T, List<StateBehaviour>> cleanupBehaviours = new HashMap<>();
15+
1216
protected HashMap<T, HashMap<T, Conditions>> transitions = new HashMap<>();
1317

1418
/*
@@ -26,12 +30,26 @@ public StateMachine(T initialState) {
2630
* @param state The target state to wait for
2731
* @param callee The method to call on transition
2832
*/
29-
public void register(T state, StateBehaviour callee) {
33+
public void addBehaviour(T state, StateBehaviour callee) {
3034
behaviours.computeIfAbsent(state, k -> new ArrayList<>());
3135

3236
behaviours.get(state).add(callee);
3337
}
3438

39+
public void addLoopedBehaviour(T state, StateBehaviour loopedBehaviour) {
40+
loopedBehaviours.computeIfAbsent(state, k -> new ArrayList<>());
41+
42+
loopedBehaviours.get(state).add(loopedBehaviour);
43+
}
44+
45+
public void addLoopedBehaviour(T state, StateBehaviour loopedBehaviour, StateBehaviour cleanupBehaviour) {
46+
loopedBehaviours.computeIfAbsent(state, k -> new ArrayList<>());
47+
cleanupBehaviours.computeIfAbsent(state, k -> new ArrayList<>());
48+
49+
loopedBehaviours.get(state).add(loopedBehaviour);
50+
cleanupBehaviours.get(state).add(cleanupBehaviour);
51+
}
52+
3553
/*
3654
* Allows the code to add a condition to describe a transition between two states.
3755
*
@@ -49,19 +67,37 @@ public void setTransitionCondition(T activeState, T targetState, Conditions cond
4967
* The function to periodically call in order to allow the <pre>EventEmitter</pre> to make transitions.
5068
*/
5169
public void update() {
70+
// Check transitions
5271
HashMap<T, Conditions> activeTransitions = transitions.get(currentState);
5372

54-
Set<T> conditions = activeTransitions.keySet();
73+
if (activeTransitions != null) {
74+
Set<T> conditions = activeTransitions.keySet();
5575

56-
for (T event : conditions) {
57-
if (activeTransitions.get(event).check()) {
58-
transitionTo(event);
59-
break;
76+
if (conditions != null) {
77+
for (T event : conditions) {
78+
if (activeTransitions.get(event).check()) {
79+
transitionTo(event);
80+
return;
81+
}
82+
}
83+
}
84+
}
85+
86+
// Run Looped Behaviours
87+
if (loopedBehaviours.get(currentState) != null) {
88+
for (StateBehaviour loopedBehaviour : loopedBehaviours.get(currentState)) {
89+
loopedBehaviour.call();
6090
}
6191
}
6292
}
6393

6494
private void transitionTo(T state) {
95+
if (cleanupBehaviours.get(currentState) != null) {
96+
for (StateBehaviour cleanup : cleanupBehaviours.get(currentState)) {
97+
cleanup.call();
98+
}
99+
}
100+
65101
currentState = state;
66102

67103
if (behaviours.get(state) != null) {
@@ -74,7 +110,7 @@ private void transitionTo(T state) {
74110
}
75111
}
76112
}
77-
113+
// If you read this your cringe why are you reading this stinky library :)
78114
/*
79115
* Gets the current state of the <pre>EventEmitter</pre>
80116
*

lib/src/test/java/jfinite/StateMachineTest.java

+36-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class StateMachineTest {
88

99
Boolean hasTransitioned;
10+
int iteratingStuff;
1011

1112
@Test
1213
void stateMachineTransitions() {
@@ -15,26 +16,57 @@ void stateMachineTransitions() {
1516
stateMachine.setTransitionCondition(State.ENTRY, State.EXIT, () -> false);
1617

1718
stateMachine.update();
18-
assertEquals(stateMachine.getState(), State.ENTRY);
19+
assertEquals(State.ENTRY, stateMachine.getState());
1920

2021
stateMachine.setTransitionCondition(State.ENTRY, State.EXIT, () -> true);
2122

2223
stateMachine.update();
23-
assertEquals(stateMachine.getState(), State.EXIT);
24+
assertEquals(State.EXIT, stateMachine.getState());
2425
}
2526

2627
@Test
2728
void stateMachineBehavior() {
2829
StateMachine<State> stateMachine = new StateMachine<>(State.ENTRY);
2930
hasTransitioned = false;
3031

31-
stateMachine.register(State.EXIT, () -> hasTransitioned = true);
32+
stateMachine.addBehaviour(State.EXIT, () -> hasTransitioned = true);
3233

3334
assertEquals(hasTransitioned, false);
3435

3536
stateMachine.setTransitionCondition(State.ENTRY, State.EXIT, () -> true);
3637
stateMachine.update();
3738

38-
assertEquals(hasTransitioned, true);
39+
assertEquals(true, hasTransitioned);
40+
}
41+
42+
@Test
43+
void stateMachineLoopingBehaviour() {
44+
StateMachine<State> stateMachine = new StateMachine<State>(State.ENTRY);
45+
46+
iteratingStuff = 0;
47+
48+
stateMachine.addLoopedBehaviour(State.ENTRY, () -> iteratingStuff++);
49+
50+
for (int i = 0; i < 5; i++) {
51+
stateMachine.update();
52+
}
53+
54+
assertEquals(5, iteratingStuff);
55+
}
56+
57+
@Test
58+
void stateMachineCleanupBehaviour() {
59+
StateMachine<State> stateMachine = new StateMachine<State>(State.ENTRY);
60+
61+
iteratingStuff = 0;
62+
63+
stateMachine.addLoopedBehaviour(State.ENTRY, () -> iteratingStuff++, () -> iteratingStuff = 69420);
64+
stateMachine.setTransitionCondition(State.ENTRY, State.EXIT, () -> iteratingStuff == 3);
65+
66+
for (int i = 0; i < 5; i++) {
67+
stateMachine.update();
68+
}
69+
70+
assertEquals(69420, iteratingStuff);
3971
}
4072
}

0 commit comments

Comments
 (0)