Implement GruphiBuider with default values

This commit is contained in:
2021-07-11 15:13:25 +02:00
parent 32821549bb
commit e7c267446f
4 changed files with 126 additions and 34 deletions

View File

@ -6,13 +6,30 @@ import java.util.function.BiPredicate;
public interface Gruphi { public interface Gruphi {
/**
* @return Delay between draw calls
*/
long getFrameDelay(); long getFrameDelay();
/**
* @return Velocity to move camera and selected node with
*/
double getVelocity(); double getVelocity();
/**
* @return Horizontal and vertical spacing between nodes whilst generating a grid
*/
double getGridSpacing(); 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(); BiPredicate<Node, Node> getNeighbourPredicate();
/**
* @return A {@link DirectedGraphFactory} to create the initial {@link h07.graph.DirectedGraph}
*/
DirectedGraphFactory<Node, Double> getDirectedGraphFactory(); DirectedGraphFactory<Node, Double> getDirectedGraphFactory();
} }

View 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);
}
}

View 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;
}
}

View File

@ -1,42 +1,11 @@
package de.oshgnacknak.gruphi; package de.oshgnacknak.gruphi;
import h07.graph.DirectedGraphFactory;
import java.util.function.BiPredicate;
public class GruphiMain { public class GruphiMain {
public static void main(String[] args) { public static void main(String[] args) {
var spacing = 50; var gruphi = new GruphiBuilder()
// .setDirectedGraphFactory(someFactory)
var gruphi = new Gruphi() { .createGruphi();
@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 frame = new GruphiFrame(gruphi); var frame = new GruphiFrame(gruphi);
frame.setVisible(true); frame.setVisible(true);