11package com .enot .cmd .core ;
22
3- import com .enot .cmd .core .listening .*;
43import org .cactoos .iterable .IterableOf ;
54import org .cactoos .iterable .Joined ;
65import org .cactoos .iterable .Mapped ;
2019 * Command line representation with the additional features around a process execution
2120 */
2221public 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
0 commit comments