Skip to content

Commit 4822dbe

Browse files
Refactoring
1 parent b8141f7 commit 4822dbe

File tree

15 files changed

+130
-189
lines changed

15 files changed

+130
-189
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Build Status](https://travis-ci.org/alekseysotnikov/CmdTool.svg?branch=master)](https://travis-ci.org/alekseysotnikov/CmdTool) [![codecov](https://codecov.io/gh/alekseysotnikov/CmdTool/branch/master/graph/badge.svg)](https://codecov.io/gh/alekseysotnikov/CmdTool)
44

55
### Quick Overview
6-
Tiny, pure object-oriented, declarative and immutable wrapper of [zt-exec](https://github.com/zeroturnaround/zt-exec) with additional features around a process execution. Inspired by approach used in the [Cactoos](https://github.com/yegor256/cactoos) lib.
6+
Tiny, pure object-oriented, declarative and immutable wrapper of [zt-exec](https://github.com/zeroturnaround/zt-exec) with additional features around a process execution.
77

88
Java 8+ required.
99

@@ -76,10 +76,11 @@ new Cmd()
7676
new WorkDir("./foo"), // specify work directory ./foo (will be created automatically)
7777
new CleanUp() // delete work directory after process stop, only if the Cmd has created the directory
7878
)
79-
.listening().afterStop(process -> {
79+
.listening((Listening.AfterStop) process -> {
8080
System.out.println(new File("./foo").exists()); //true
81-
}).back()
82-
.command("echo", "hello world").execute();
81+
})
82+
.command("echo", "hello world")
83+
.execute();
8384

8485
System.out.println(new File("./foo").exists()); // false
8586
````

src/main/java/com/enot/cmd/core/Cmd.java

Lines changed: 55 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.enot.cmd.core;
22

3-
import com.enot.cmd.core.listening.*;
43
import org.cactoos.iterable.IterableOf;
54
import org.cactoos.iterable.Joined;
65
import org.cactoos.iterable.Mapped;
@@ -20,33 +19,72 @@
2019
* Command line representation with the additional features around a process execution
2120
*/
2221
public final class Cmd implements ICmd {
23-
private final CmdListening cmdListening;
24-
private final BeforeStart[] beforeStart;
22+
private final Iterable<ProcessListenerAdapter> listeners;
23+
private final Listening.BeforeStart[] configuring;
2524
private final String interpreter;
2625

2726
public Cmd() {
28-
this(new CmdListening(null, new IterableOf<>()), new BeforeStart[0], "");
27+
this(new IterableOf<>(), new Listening.BeforeStart[0], "");
2928
}
3029

31-
public Cmd(CmdListening cmdListening, BeforeStart[] beforeStart, String interpreter) {
32-
this.cmdListening = cmdListening;
33-
this.beforeStart = beforeStart;
30+
public Cmd(Iterable<ProcessListenerAdapter> listeners, Listening.BeforeStart[] configuring, String interpreter) {
31+
this.listeners = listeners;
32+
this.configuring = configuring;
3433
this.interpreter = interpreter;
3534
}
3635

3736
@Override
38-
public Cmd configuring(BeforeStart... configuring) {
39-
return new Cmd(cmdListening, configuring, interpreter);
37+
public Cmd configuring(Listening.BeforeStart... configuring) {
38+
return new Cmd(listeners, configuring, interpreter);
4039
}
4140

4241
@Override
43-
public Listening listening() {
44-
return new CmdListening(this, cmdListening.listeners);
42+
public ICmd listening(Listening.BeforeStart... beforeStart) {
43+
return new Cmd(
44+
new Joined<>(
45+
this.listeners,
46+
new Mapped<>(ProcessListenerAdapter::new,
47+
new IterableOf<>(beforeStart))),
48+
configuring,
49+
interpreter);
50+
}
51+
52+
@Override
53+
public ICmd listening(Listening.AfterStart... afterStart) {
54+
return new Cmd(
55+
new Joined<>(
56+
this.listeners,
57+
new Mapped<>(ProcessListenerAdapter::new,
58+
new IterableOf<>(afterStart))),
59+
configuring,
60+
interpreter);
61+
}
62+
63+
@Override
64+
public ICmd listening(Listening.AfterFinish... afterFinish) {
65+
return new Cmd(
66+
new Joined<>(
67+
this.listeners,
68+
new Mapped<>(ProcessListenerAdapter::new,
69+
new IterableOf<>(afterFinish))),
70+
configuring,
71+
interpreter);
72+
}
73+
74+
@Override
75+
public ICmd listening(Listening.AfterStop... afterStop) {
76+
return new Cmd(
77+
new Joined<>(
78+
this.listeners,
79+
new Mapped<>(ProcessListenerAdapter::new,
80+
new IterableOf<>(afterStop))),
81+
configuring,
82+
interpreter);
4583
}
4684

4785
@Override
4886
public Cmd interpreter(String interpreter) {
49-
return new Cmd(cmdListening, beforeStart, interpreter);
87+
return new Cmd(listeners, configuring, interpreter);
5088
}
5189

5290
public Command command(String... command) {
@@ -56,13 +94,13 @@ public Command command(String... command) {
5694
private ProcessExecutor processExecutor(String... command) {
5795
ProcessExecutor executor = new ProcessExecutor();
5896

59-
Map<Boolean, List<BeforeStart>> configuring = Arrays.stream(beforeStart).collect(
60-
Collectors.groupingBy(c -> c instanceof AfterStop));
61-
List<BeforeStart> configuringBefore = configuring.getOrDefault(false, Collections.emptyList());
62-
List<BeforeStart> configuringAfter = configuring.getOrDefault(true, Collections.emptyList());
97+
Map<Boolean, List<Listening.BeforeStart>> configuring = Arrays.stream(this.configuring).collect(
98+
Collectors.groupingBy(c -> c instanceof Listening.AfterStop));
99+
List<Listening.BeforeStart> configuringBefore = configuring.getOrDefault(false, Collections.emptyList());
100+
List<Listening.BeforeStart> configuringAfter = configuring.getOrDefault(true, Collections.emptyList());
63101

64102
configuringBefore.forEach(c -> c.run(executor));
65-
cmdListening.listeners.forEach(executor::addListener);
103+
listeners.forEach(executor::addListener);
66104
configuringAfter.forEach(c -> c.run(executor));
67105

68106
Iterable<String> commands = new IterableOf<>(command);
@@ -72,78 +110,6 @@ private ProcessExecutor processExecutor(String... command) {
72110
return executor.command(commands);
73111
}
74112

75-
76-
private static final class CmdListening implements Listening {
77-
private final Cmd owner;
78-
private final Iterable<LambdaListenerAdapter> listeners;
79-
80-
public CmdListening(Cmd owner, BeforeStart... lambdas) {
81-
this(owner, new Mapped<>(LambdaListenerAdapter::new, new IterableOf<>(lambdas)));
82-
}
83-
84-
public CmdListening(Cmd owner, AfterStart... lambdas) {
85-
this(owner, new Mapped<>(LambdaListenerAdapter::new, new IterableOf<>(lambdas)));
86-
}
87-
88-
public CmdListening(Cmd owner, AfterFinish... lambdas) {
89-
this(owner, new Mapped<>(LambdaListenerAdapter::new, new IterableOf<>(lambdas)));
90-
}
91-
92-
public CmdListening(Cmd owner, AfterStop... lambdas) {
93-
this(owner, new Mapped<>(LambdaListenerAdapter::new, new IterableOf<>(lambdas)));
94-
}
95-
96-
public CmdListening(Cmd owner, Iterable<LambdaListenerAdapter> listeners) {
97-
this.owner = owner;
98-
this.listeners = listeners;
99-
}
100-
101-
@Override
102-
public Listening beforeStart(BeforeStart... lambdas) {
103-
return new CmdListening(
104-
owner,
105-
new Joined<>(
106-
this.listeners,
107-
new Mapped<>(LambdaListenerAdapter::new,
108-
new IterableOf<>(lambdas))));
109-
}
110-
111-
@Override
112-
public Listening afterStart(AfterStart... lambdas) {
113-
return new CmdListening(
114-
owner,
115-
new Joined<>(
116-
this.listeners,
117-
new Mapped<>(LambdaListenerAdapter::new,
118-
new IterableOf<>(lambdas))));
119-
}
120-
121-
@Override
122-
public Listening afterFinish(AfterFinish... lambdas) {
123-
return new CmdListening(
124-
owner,
125-
new Joined<>(
126-
this.listeners,
127-
new Mapped<>(LambdaListenerAdapter::new,
128-
new IterableOf<>(lambdas))));
129-
}
130-
131-
@Override
132-
public Listening afterStop(AfterStop... lambdas) {
133-
return new CmdListening(
134-
owner,
135-
new Joined<>(
136-
this.listeners,
137-
new Mapped<>(LambdaListenerAdapter::new,
138-
new IterableOf<>(lambdas))));
139-
}
140-
141-
@Override
142-
public Cmd back() {
143-
return new Cmd(this, owner.beforeStart, owner.interpreter);
144-
}
145-
}
146-
147113
private static final class BaseCommand implements Command {
148114
private final ProcessExecutor processExecutor;
149115

src/main/java/com/enot/cmd/core/ICmd.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.enot.cmd.core;
22

3-
import com.enot.cmd.core.listening.BeforeStart;
4-
import com.enot.cmd.core.listening.Listening;
5-
63
public interface ICmd {
7-
ICmd configuring(BeforeStart... configuring);
4+
ICmd configuring(Listening.BeforeStart... configuring);
5+
6+
ICmd listening(Listening.BeforeStart... beforeStart);
7+
8+
ICmd listening(Listening.AfterStart... afterStart);
9+
10+
ICmd listening(Listening.AfterFinish... afterFinish);
811

9-
Listening listening();
12+
ICmd listening(Listening.AfterStop... afterStop);
1013

1114
/**
1215
* Specify command interpreter
@@ -15,6 +18,7 @@ public interface ICmd {
1518

1619
/**
1720
* Create executable command
21+
*
1822
* @param command
1923
* @return
2024
*/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.enot.cmd.core;
2+
3+
import org.zeroturnaround.exec.ProcessExecutor;
4+
import org.zeroturnaround.exec.ProcessResult;
5+
import org.zeroturnaround.exec.listener.ProcessListener;
6+
7+
public interface Listening {
8+
/**
9+
* See {@link ProcessListener#afterFinish(Process, ProcessResult)}
10+
*/
11+
interface AfterFinish {
12+
void run(Process process, ProcessResult result);
13+
}
14+
15+
/**
16+
* See {@link ProcessListener#afterStart(Process, ProcessExecutor)}
17+
*/
18+
interface AfterStart {
19+
void run(Process process, ProcessExecutor executor);
20+
}
21+
22+
/**
23+
* See {@link ProcessListener#afterStop(Process)}
24+
*/
25+
interface AfterStop {
26+
void run(Process process);
27+
}
28+
29+
/**
30+
* See {@link ProcessListener#beforeStart(ProcessExecutor)}
31+
*/
32+
interface BeforeStart {
33+
void run(ProcessExecutor executor);
34+
}
35+
}

src/main/java/com/enot/cmd/core/LambdaListenerAdapter.java renamed to src/main/java/com/enot/cmd/core/ProcessListenerAdapter.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
package com.enot.cmd.core;
22

3-
import com.enot.cmd.core.listening.AfterFinish;
4-
import com.enot.cmd.core.listening.AfterStart;
5-
import com.enot.cmd.core.listening.AfterStop;
6-
import com.enot.cmd.core.listening.BeforeStart;
73
import org.zeroturnaround.exec.ProcessExecutor;
84
import org.zeroturnaround.exec.ProcessResult;
95
import org.zeroturnaround.exec.listener.ProcessListener;
106

11-
public class LambdaListenerAdapter extends ProcessListener {
12-
private final BeforeStart beforeStart;
13-
private final AfterStart afterStart;
14-
private final AfterFinish afterFinish;
15-
private final AfterStop afterStop;
7+
public class ProcessListenerAdapter extends ProcessListener {
8+
private final Listening.BeforeStart beforeStart;
9+
private final Listening.AfterStart afterStart;
10+
private final Listening.AfterFinish afterFinish;
11+
private final Listening.AfterStop afterStop;
1612

17-
public LambdaListenerAdapter(BeforeStart beforeStart, AfterStart afterStart, AfterFinish afterFinish, AfterStop afterStop) {
13+
public ProcessListenerAdapter(Listening.BeforeStart beforeStart, Listening.AfterStart afterStart, Listening.AfterFinish afterFinish, Listening.AfterStop afterStop) {
1814
this.beforeStart = beforeStart;
1915
this.afterStart = afterStart;
2016
this.afterFinish = afterFinish;
2117
this.afterStop = afterStop;
2218
}
2319

24-
public LambdaListenerAdapter(BeforeStart beforeStart) {
20+
public ProcessListenerAdapter(Listening.BeforeStart beforeStart) {
2521
this(beforeStart, (p, e) -> {/*nothing*/}, (p, r) -> {/*nothing*/}, p -> {/*nothing*/});
2622
}
2723

28-
public LambdaListenerAdapter(AfterStart afterStart) {
24+
public ProcessListenerAdapter(Listening.AfterStart afterStart) {
2925
this(e -> {/*nothing*/}, afterStart, (p, r) -> {/*nothing*/}, p -> {/*nothing*/});
3026
}
3127

32-
public LambdaListenerAdapter(AfterFinish afterFinish) {
28+
public ProcessListenerAdapter(Listening.AfterFinish afterFinish) {
3329
this(e -> {/*nothing*/}, (p, e) -> {/*nothing*/}, afterFinish, p -> {/*nothing*/});
3430
}
3531

36-
public LambdaListenerAdapter(AfterStop afterStop) {
32+
public ProcessListenerAdapter(Listening.AfterStop afterStop) {
3733
this(e -> {/*nothing*/}, (p, e) -> {/*nothing*/}, (p, r) -> {/*nothing*/}, afterStop);
3834
}
3935

src/main/java/com/enot/cmd/core/listening/AfterFinish.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main/java/com/enot/cmd/core/listening/AfterStart.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main/java/com/enot/cmd/core/listening/AfterStop.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/java/com/enot/cmd/core/listening/BeforeStart.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main/java/com/enot/cmd/core/listening/Listening.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)