Implement GruphiBuider with default values
This commit is contained in:
		
							parent
							
								
									32821549bb
								
							
						
					
					
						commit
						e7c267446f
					
				
					 4 changed files with 126 additions and 34 deletions
				
			
		|  | @ -6,13 +6,30 @@ import java.util.function.BiPredicate; | |||
| 
 | ||||
| public interface Gruphi { | ||||
| 
 | ||||
|     /** | ||||
|      * @return Delay between draw calls | ||||
|      */ | ||||
|     long getFrameDelay(); | ||||
| 
 | ||||
|     /** | ||||
|      * @return Velocity to move camera and selected node with | ||||
|      */ | ||||
|     double getVelocity(); | ||||
| 
 | ||||
|     /** | ||||
|      * @return Horizontal and vertical spacing between nodes whilst generating a grid | ||||
|      */ | ||||
|     double getGridSpacing(); | ||||
| 
 | ||||
|     /** | ||||
|      * @return A {@link BiPredicate} that returns <code>true</code> | ||||
|      * iff a pair of nodes should be considered neighbours | ||||
|      * by the {@link MazeGenerator}. | ||||
|      */ | ||||
|     BiPredicate<Node, Node> getNeighbourPredicate(); | ||||
| 
 | ||||
|     /** | ||||
|      * @return A {@link DirectedGraphFactory} to create the initial {@link h07.graph.DirectedGraph} | ||||
|      */ | ||||
|     DirectedGraphFactory<Node, Double> getDirectedGraphFactory(); | ||||
| } | ||||
|  |  | |||
							
								
								
									
										42
									
								
								src/main/java/de/oshgnacknak/gruphi/GruphiBuilder.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/main/java/de/oshgnacknak/gruphi/GruphiBuilder.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,42 @@ | |||
| package de.oshgnacknak.gruphi; | ||||
| 
 | ||||
| import h07.graph.DirectedGraphFactory; | ||||
| 
 | ||||
| import java.util.function.BiPredicate; | ||||
| 
 | ||||
| public class GruphiBuilder { | ||||
|     private Long frameDelay; | ||||
|     private Double velocity; | ||||
|     private Double gridSpacing; | ||||
|     private BiPredicate<Node, Node> neighbourPredicate; | ||||
|     private DirectedGraphFactory<Node, Double> directedGraphFactory; | ||||
| 
 | ||||
|     public GruphiBuilder setFrameDelay(Long frameDelay) { | ||||
|         this.frameDelay = frameDelay; | ||||
|         return this; | ||||
|     } | ||||
| 
 | ||||
|     public GruphiBuilder setVelocity(Double velocity) { | ||||
|         this.velocity = velocity; | ||||
|         return this; | ||||
|     } | ||||
| 
 | ||||
|     public GruphiBuilder setGridSpacing(Double gridSpacing) { | ||||
|         this.gridSpacing = gridSpacing; | ||||
|         return this; | ||||
|     } | ||||
| 
 | ||||
|     public GruphiBuilder setNeighbourPredicate(BiPredicate<Node, Node> neighbourPredicate) { | ||||
|         this.neighbourPredicate = neighbourPredicate; | ||||
|         return this; | ||||
|     } | ||||
| 
 | ||||
|     public GruphiBuilder setDirectedGraphFactory(DirectedGraphFactory<Node, Double> directedGraphFactory) { | ||||
|         this.directedGraphFactory = directedGraphFactory; | ||||
|         return this; | ||||
|     } | ||||
| 
 | ||||
|     public GruphiImpl createGruphi() { | ||||
|         return new GruphiImpl(frameDelay, velocity, gridSpacing, neighbourPredicate, directedGraphFactory); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										64
									
								
								src/main/java/de/oshgnacknak/gruphi/GruphiImpl.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/main/java/de/oshgnacknak/gruphi/GruphiImpl.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,64 @@ | |||
| package de.oshgnacknak.gruphi; | ||||
| 
 | ||||
| import h07.graph.DirectedGraphFactory; | ||||
| 
 | ||||
| import java.util.Objects; | ||||
| import java.util.function.BiPredicate; | ||||
| 
 | ||||
| public class GruphiImpl implements Gruphi { | ||||
| 
 | ||||
|     private static final long DEFAULT_FRAME_DELAY = 1000 / 60; | ||||
|     private static final double DEFAULT_VELOCITY = 5; | ||||
|     private static final double DEFAULT_GRID_SPACING = 50; | ||||
| 
 | ||||
|     private final long frameDelay; | ||||
| 
 | ||||
|     private final double velocity; | ||||
| 
 | ||||
|     private final double gridSpacing; | ||||
| 
 | ||||
|     private final BiPredicate<Node, Node> neighbourPredicate; | ||||
| 
 | ||||
|     private final DirectedGraphFactory<Node, Double> directedGraphFactory; | ||||
| 
 | ||||
|     public GruphiImpl(Long frameDelay, | ||||
|                       Double velocity, | ||||
|                       Double gridSpacing, | ||||
|                       BiPredicate<Node, Node> neighbourPredicate, | ||||
|                       DirectedGraphFactory<Node, Double> directedGraphFactory) { | ||||
|         this.frameDelay = frameDelay == null ? DEFAULT_FRAME_DELAY : frameDelay; | ||||
|         this.velocity = velocity == null ? DEFAULT_VELOCITY : velocity; | ||||
|         this.gridSpacing = gridSpacing == null ? DEFAULT_GRID_SPACING : gridSpacing; | ||||
|         this.neighbourPredicate = neighbourPredicate == null ? this::defaultAreNeighbours : neighbourPredicate; | ||||
|         this.directedGraphFactory = Objects.requireNonNull(directedGraphFactory, "A directedGraphFactory must be set"); | ||||
|     } | ||||
| 
 | ||||
|     private boolean defaultAreNeighbours(Node a, Node b) { | ||||
|         return a.pos.dist(b.pos) <= gridSpacing; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public long getFrameDelay() { | ||||
|         return frameDelay; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public double getVelocity() { | ||||
|         return velocity; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public double getGridSpacing() { | ||||
|         return gridSpacing; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public BiPredicate<Node, Node> getNeighbourPredicate() { | ||||
|         return neighbourPredicate; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public DirectedGraphFactory<Node, Double> getDirectedGraphFactory() { | ||||
|         return directedGraphFactory; | ||||
|     } | ||||
| } | ||||
|  | @ -1,42 +1,11 @@ | |||
| package de.oshgnacknak.gruphi; | ||||
| 
 | ||||
| import h07.graph.DirectedGraphFactory; | ||||
| 
 | ||||
| import java.util.function.BiPredicate; | ||||
| 
 | ||||
| public class GruphiMain { | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
|         var spacing = 50; | ||||
| 
 | ||||
|         var gruphi = new Gruphi() { | ||||
| 
 | ||||
|             @Override | ||||
|             public long getFrameDelay() { | ||||
|                 return 1000 / 60; | ||||
|             } | ||||
| 
 | ||||
|             @Override | ||||
|             public double getVelocity() { | ||||
|                 return 5; | ||||
|             } | ||||
| 
 | ||||
|             @Override | ||||
|             public double getGridSpacing() { | ||||
|                 return spacing; | ||||
|             } | ||||
| 
 | ||||
|             @Override | ||||
|             public BiPredicate<Node, Node> getNeighbourPredicate() { | ||||
|                 return (a, b) -> | ||||
|                     a.pos.dist(b.pos) <= spacing; | ||||
|             } | ||||
| 
 | ||||
|             @Override | ||||
|             public DirectedGraphFactory<Node, Double> getDirectedGraphFactory() { | ||||
|                 return DirectedGraphFactory.defaultFactory(); | ||||
|             } | ||||
|         }; | ||||
|         var gruphi = new GruphiBuilder() | ||||
|             // .setDirectedGraphFactory(someFactory) | ||||
|             .createGruphi(); | ||||
| 
 | ||||
|         var frame = new GruphiFrame(gruphi); | ||||
|         frame.setVisible(true); | ||||
|  |  | |||
		Reference in a new issue