Skip to content

Commit 202a19a

Browse files
amihaiemilpivovarit
authored andcommitted
Automata indentation and link (eugenp#1425)
* finite automata example * indentation and link
1 parent 9d0cb1e commit 202a19a

8 files changed

Lines changed: 126 additions & 125 deletions

File tree

algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
- [Dijkstra Algorithm in Java](http://www.baeldung.com/java-dijkstra)
44
- [Introduction to Cobertura](http://www.baeldung.com/cobertura)
55
- [Ant Colony Optimization](http://www.baeldung.com/java-ant-colony-optimization)
6+
- [Validating Input With Finite Automata in Java](http://www.baeldung.com/finite-automata-java)

algorithms/src/main/java/com/baeldung/automata/FiniteStateMachine.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66
public interface FiniteStateMachine {
77

8-
/**
9-
* Follow a transition, switch the state of the machine.
10-
* @param c Char.
11-
* @return A new finite state machine with the new state.
12-
*/
13-
FiniteStateMachine switchState(final CharSequence c);
8+
/**
9+
* Follow a transition, switch the state of the machine.
10+
* @param c Char.
11+
* @return A new finite state machine with the new state.
12+
*/
13+
FiniteStateMachine switchState(final CharSequence c);
1414

1515
/**
1616
* Is the current state a final one?

algorithms/src/main/java/com/baeldung/automata/RtFiniteStateMachine.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@
66
*/
77
public final class RtFiniteStateMachine implements FiniteStateMachine {
88

9-
/**
10-
* Current state.
11-
*/
12-
private State current;
9+
/**
10+
* Current state.
11+
*/
12+
private State current;
1313

14-
/**
15-
* Ctor.
16-
* @param initial Initial state of this machine.
17-
*/
18-
public RtFiniteStateMachine(final State initial) {
19-
this.current = initial;
20-
}
14+
/**
15+
* Ctor.
16+
* @param initial Initial state of this machine.
17+
*/
18+
public RtFiniteStateMachine(final State initial) {
19+
this.current = initial;
20+
}
2121

22-
public FiniteStateMachine switchState(final CharSequence c) {
23-
return new RtFiniteStateMachine(this.current.transit(c));
24-
}
22+
public FiniteStateMachine switchState(final CharSequence c) {
23+
return new RtFiniteStateMachine(this.current.transit(c));
24+
}
2525

26-
public boolean canStop() {
27-
return this.current.isFinal();
28-
}
26+
public boolean canStop() {
27+
return this.current.isFinal();
28+
}
2929

3030
}

algorithms/src/main/java/com/baeldung/automata/RtState.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public boolean isFinal() {
3333
return this.isFinal;
3434
}
3535

36-
@Override
37-
public State with(Transition tr) {
38-
this.transitions.add(tr);
39-
return this;
40-
}
36+
@Override
37+
public State with(Transition tr) {
38+
this.transitions.add(tr);
39+
return this;
40+
}
4141

4242
}

algorithms/src/main/java/com/baeldung/automata/RtTransition.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66
*/
77
public final class RtTransition implements Transition {
88

9-
private String rule;
10-
private State next;
11-
12-
/**
13-
* Ctor.
14-
* @param rule Rule that a character has to meet
15-
* in order to get to the next state.
16-
* @param next Next state.
17-
*/
18-
public RtTransition (String rule, State next) {
19-
this.rule = rule;
20-
this.next = next;
21-
}
22-
23-
public State state() {
24-
return this.next;
25-
}
9+
private String rule;
10+
private State next;
11+
12+
/**
13+
* Ctor.
14+
* @param rule Rule that a character has to meet
15+
* in order to get to the next state.
16+
* @param next Next state.
17+
*/
18+
public RtTransition (String rule, State next) {
19+
this.rule = rule;
20+
this.next = next;
21+
}
22+
23+
public State state() {
24+
return this.next;
25+
}
2626

27-
public boolean isPossible(CharSequence c) {
28-
return this.rule.equalsIgnoreCase(String.valueOf(c));
29-
}
27+
public boolean isPossible(CharSequence c) {
28+
return this.rule.equalsIgnoreCase(String.valueOf(c));
29+
}
3030

3131
}

algorithms/src/main/java/com/baeldung/automata/State.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66
public interface State {
77

8-
/**
9-
* Add a Transition to this state.
10-
* @param tr Given transition.
11-
* @return Modified State.
12-
*/
13-
State with(final Transition tr);
8+
/**
9+
* Add a Transition to this state.
10+
* @param tr Given transition.
11+
* @return Modified State.
12+
*/
13+
State with(final Transition tr);
1414

1515
/**
1616
* Follow one of the transitions, to get

algorithms/src/main/java/com/baeldung/automata/Transition.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
*/
66
public interface Transition {
77

8-
/**
9-
* Is the transition possible with the given character?
10-
* @param c char.
11-
* @return true or false.
12-
*/
13-
boolean isPossible(final CharSequence c);
8+
/**
9+
* Is the transition possible with the given character?
10+
* @param c char.
11+
* @return true or false.
12+
*/
13+
boolean isPossible(final CharSequence c);
1414

15-
/**
16-
* The state to which this transition leads.
17-
* @return State.
18-
*/
15+
/**
16+
* The state to which this transition leads.
17+
* @return State.
18+
*/
1919
State state();
2020
}

algorithms/src/test/java/algorithms/RtFiniteStateMachineTest.java

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,74 @@
99
*/
1010
public final class RtFiniteStateMachineTest {
1111

12-
@Test
12+
@Test
1313
public void acceptsSimplePair() {
14-
String json = "{\"key\":\"value\"}";
15-
FiniteStateMachine machine = this.buildJsonStateMachine();
16-
for (int i=0;i<json.length();i++) {
17-
machine = machine.switchState(String.valueOf(json.charAt(i)));
18-
}
19-
assertTrue(machine.canStop());
14+
String json = "{\"key\":\"value\"}";
15+
FiniteStateMachine machine = this.buildJsonStateMachine();
16+
for (int i=0;i<json.length();i++) {
17+
machine = machine.switchState(String.valueOf(json.charAt(i)));
18+
}
19+
assertTrue(machine.canStop());
2020
}
21-
@Test
21+
@Test
2222
public void acceptsMorePairs() {
23-
String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
24-
FiniteStateMachine machine = this.buildJsonStateMachine();
25-
for (int i=0;i<json.length();i++) {
26-
machine = machine.switchState(String.valueOf(json.charAt(i)));
27-
}
28-
assertTrue(machine.canStop());
23+
String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
24+
FiniteStateMachine machine = this.buildJsonStateMachine();
25+
for (int i=0;i<json.length();i++) {
26+
machine = machine.switchState(String.valueOf(json.charAt(i)));
27+
}
28+
assertTrue(machine.canStop());
2929
}
30-
31-
@Test(expected = IllegalArgumentException.class)
30+
31+
@Test(expected = IllegalArgumentException.class)
3232
public void missingColon() {
33-
String json = "{\"key\"\"value\"}";
34-
FiniteStateMachine machine = this.buildJsonStateMachine();
35-
for (int i=0;i<json.length();i++) {
36-
machine = machine.switchState(String.valueOf(json.charAt(i)));
37-
}
33+
String json = "{\"key\"\"value\"}";
34+
FiniteStateMachine machine = this.buildJsonStateMachine();
35+
for (int i=0;i<json.length();i++) {
36+
machine = machine.switchState(String.valueOf(json.charAt(i)));
37+
}
3838
}
3939

40-
/**
41-
* Builds a finite state machine to validate a simple
42-
* Json object.
43-
* @return
44-
*/
45-
private FiniteStateMachine buildJsonStateMachine() {
46-
State first = new RtState();
47-
State second = new RtState();
48-
State third = new RtState();
49-
State fourth = new RtState();
50-
State fifth = new RtState();
51-
State sixth = new RtState();
52-
State seventh = new RtState();
53-
State eighth = new RtState(true);
40+
/**
41+
* Builds a finite state machine to validate a simple
42+
* Json object.
43+
* @return
44+
*/
45+
private FiniteStateMachine buildJsonStateMachine() {
46+
State first = new RtState();
47+
State second = new RtState();
48+
State third = new RtState();
49+
State fourth = new RtState();
50+
State fifth = new RtState();
51+
State sixth = new RtState();
52+
State seventh = new RtState();
53+
State eighth = new RtState(true);
5454

55-
first.with(new RtTransition("{", second));
56-
second.with(new RtTransition("\"", third));
57-
//Add transitions with chars 0-9 and a-z
58-
for (int i = 0; i < 26; i++) {
59-
if(i<10) {
60-
third = third.with(
61-
new RtTransition(String.valueOf(i), third)
62-
);
63-
sixth = sixth.with(
64-
new RtTransition(String.valueOf(i), sixth)
55+
first.with(new RtTransition("{", second));
56+
second.with(new RtTransition("\"", third));
57+
//Add transitions with chars 0-9 and a-z
58+
for (int i = 0; i < 26; i++) {
59+
if(i<10) {
60+
third = third.with(
61+
new RtTransition(String.valueOf(i), third)
6562
);
66-
}
67-
third = third.with(
68-
new RtTransition(String.valueOf((char) ('a' + i)), third)
69-
);
70-
sixth = sixth.with(
71-
new RtTransition(String.valueOf((char) ('a' + i)), sixth)
72-
);
73-
}
74-
third.with(new RtTransition("\"", fourth));
75-
fourth.with(new RtTransition(":", fifth));
76-
fifth.with(new RtTransition("\"", sixth));
77-
sixth.with(new RtTransition("\"", seventh));
78-
seventh.with(new RtTransition(",", second));
79-
seventh.with(new RtTransition("}", eighth));
80-
return new RtFiniteStateMachine(first);
81-
}
63+
sixth = sixth.with(
64+
new RtTransition(String.valueOf(i), sixth)
65+
);
66+
}
67+
third = third.with(
68+
new RtTransition(String.valueOf((char) ('a' + i)), third)
69+
);
70+
sixth = sixth.with(
71+
new RtTransition(String.valueOf((char) ('a' + i)), sixth)
72+
);
73+
}
74+
third.with(new RtTransition("\"", fourth));
75+
fourth.with(new RtTransition(":", fifth));
76+
fifth.with(new RtTransition("\"", sixth));
77+
sixth.with(new RtTransition("\"", seventh));
78+
seventh.with(new RtTransition(",", second));
79+
seventh.with(new RtTransition("}", eighth));
80+
return new RtFiniteStateMachine(first);
81+
}
8282
}

0 commit comments

Comments
 (0)