-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: cmoulliard <[email protected]>
- Loading branch information
1 parent
f314ddf
commit 0359e7d
Showing
11 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
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,19 @@ | ||
package dev.snowdrop.visitorpattern; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
public class Action { | ||
private String name; | ||
private String description; | ||
private List<Step> steps; | ||
|
||
public Action(String name, String description) { | ||
this.name = name; | ||
this.description = description; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/dev/snowdrop/visitorpattern/Configurator.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,14 @@ | ||
package dev.snowdrop.visitorpattern; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class Configurator { | ||
private String name; | ||
private String provider; | ||
private String resourceType; | ||
private String description; | ||
private Job job; | ||
} |
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,14 @@ | ||
package dev.snowdrop.visitorpattern; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
public class Job { | ||
private List<Action> actions; | ||
|
||
public Job() {} | ||
} |
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,19 @@ | ||
package dev.snowdrop.visitorpattern; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class JobFactory { | ||
public static Visitable createResource(Configurator config) { | ||
if (config.getResourceType().equalsIgnoreCase("PipelineRun")) { | ||
return new PipelineRun(config); | ||
} else if (config.getResourceType().equalsIgnoreCase("TaskRun")) { | ||
// Convert Actions to Steps | ||
List<Step> steps = config.getJob().getActions().stream() | ||
.map(action -> new Step(action.getName())) | ||
.collect(Collectors.toList()); | ||
return new TaskRun(config.getName(), steps); | ||
} | ||
throw new IllegalArgumentException("Unknown resource type: " + config.getResourceType()); | ||
} | ||
} |
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,29 @@ | ||
package dev.snowdrop.visitorpattern; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class MainApp { | ||
public static void main(String[] args) { | ||
// Create a sample Configurator | ||
Job job = new Job(); | ||
|
||
List<Action> actions = Arrays.asList( | ||
new Action("Action1", "Description1"), | ||
new Action("Action2", "Description2") | ||
); | ||
job.setActions(actions); | ||
|
||
Configurator configurator = new Configurator(); | ||
configurator.setName("Example Config"); | ||
configurator.setResourceType("PipelineRun"); // Or "TaskRun" | ||
configurator.setJob(job); | ||
|
||
// Create resource (PipelineRun or TaskRun) | ||
Visitable resource = JobFactory.createResource(configurator); | ||
|
||
// Process resource using visitor | ||
ResourceVisitor visitor = new ResourceVisitor(); | ||
resource.accept(visitor); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/dev/snowdrop/visitorpattern/PipelineRun.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,25 @@ | ||
package dev.snowdrop.visitorpattern; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
public class PipelineRun implements Visitable { | ||
private String name; | ||
private List<Action> actions; | ||
|
||
public PipelineRun(Configurator cfg) { | ||
this.name = cfg.getName(); | ||
this.actions = cfg.getJob().getActions(); | ||
} | ||
|
||
@Override | ||
public void accept(Visitor visitor) { | ||
visitor.visit(this); | ||
} | ||
|
||
// Other methods related to PipelineRun | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/dev/snowdrop/visitorpattern/ResourceVisitor.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,16 @@ | ||
package dev.snowdrop.visitorpattern; | ||
|
||
public class ResourceVisitor implements Visitor { | ||
|
||
@Override | ||
public void visit(PipelineRun pipelineRun) { | ||
System.out.println("Processing PipelineRun: " + pipelineRun.getName()); | ||
// Logic to process PipelineRun | ||
} | ||
|
||
@Override | ||
public void visit(TaskRun taskRun) { | ||
System.out.println("Processing TaskRun: " + taskRun.getName()); | ||
// Logic to process TaskRun | ||
} | ||
} |
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,14 @@ | ||
package dev.snowdrop.visitorpattern; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class Step { | ||
private String name; | ||
|
||
public Step(String name) { | ||
this.name = name; | ||
} | ||
} |
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,27 @@ | ||
package dev.snowdrop.visitorpattern; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
public class TaskRun implements Visitable { | ||
private String name; | ||
private List<Step> steps; | ||
|
||
public TaskRun(String name, List<Step> steps) { | ||
this.name = name; | ||
this.steps = steps; | ||
} | ||
|
||
@Override | ||
public void accept(Visitor visitor) { | ||
visitor.visit(this); | ||
} | ||
|
||
// Other methods related to TaskRun | ||
} | ||
|
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,6 @@ | ||
package dev.snowdrop.visitorpattern; | ||
|
||
public interface Visitable { | ||
void accept(Visitor visitor); | ||
} | ||
|
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,7 @@ | ||
package dev.snowdrop.visitorpattern; | ||
|
||
public interface Visitor { | ||
void visit(PipelineRun pipelineRun); | ||
void visit(TaskRun taskRun); | ||
} | ||
|