From 1ff3d2a5fc2a1465ca3e0ad5b55d910b7c720368 Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 4 Feb 2018 19:17:56 +0100 Subject: Move contact edge stream creation Signed-off-by: pacien --- .../java/fr/umlv/java/wallj/block/JBoxBlock.java | 44 ++++++++++++++++++++++ .../java/fr/umlv/java/wallj/block/TrashBlock.java | 38 ------------------- 2 files changed, 44 insertions(+), 38 deletions(-) (limited to 'src/main/java/fr/umlv') diff --git a/src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java b/src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java index e01f4b0..1ef5503 100644 --- a/src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/JBoxBlock.java @@ -3,10 +3,17 @@ package fr.umlv.java.wallj.block; import org.jbox2d.collision.shapes.Shape; import org.jbox2d.common.Vec2; import org.jbox2d.dynamics.*; +import org.jbox2d.dynamics.contacts.ContactEdge; import java.util.Objects; +import java.util.Spliterator; +import java.util.function.Consumer; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; /** + * A block linked to a physical body. + * * @author Pacien TRAN-GIRARD */ public abstract class JBoxBlock extends Block { @@ -27,6 +34,43 @@ public abstract class JBoxBlock extends Block { return body; } + /** + * @return a stream of contact edges of this body + */ + public Stream streamContactEdges() { + // If only we had (a working) Java 9... + // return Stream.iterate(getBody().getContactList(), c -> c.next) + // .takeWhile(Objects::nonNull); + + return StreamSupport.stream(new Spliterator() { + private ContactEdge contactEdge = getBody().getContactList(); + + @Override + public boolean tryAdvance(Consumer consumer) { + if (contactEdge == null) return false; + + consumer.accept(contactEdge); + contactEdge = contactEdge.next; + return true; + } + + @Override + public Spliterator trySplit() { + return null; + } + + @Override + public long estimateSize() { + return Long.MAX_VALUE; + } + + @Override + public int characteristics() { + return NONNULL; + } + }, false); + } + @Override public Vec2 getPos() { if (body == null) throw new IllegalStateException("Uninitialised block."); diff --git a/src/main/java/fr/umlv/java/wallj/block/TrashBlock.java b/src/main/java/fr/umlv/java/wallj/block/TrashBlock.java index 1f2dfa6..cc76917 100644 --- a/src/main/java/fr/umlv/java/wallj/block/TrashBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/TrashBlock.java @@ -7,13 +7,9 @@ import fr.umlv.java.wallj.event.BlockDestroyEvent; import fr.umlv.java.wallj.event.Event; import org.jbox2d.common.Vec2; import org.jbox2d.dynamics.BodyType; -import org.jbox2d.dynamics.contacts.ContactEdge; import java.awt.*; -import java.util.Spliterator; -import java.util.function.Consumer; import java.util.stream.Stream; -import java.util.stream.StreamSupport; /** * A trash block. @@ -43,38 +39,4 @@ public class TrashBlock extends JBoxBlock { context.getGraphicsContext().paintRectangle(Color.RED, getPos(), TileVec2.TILE_DIM, TileVec2.TILE_DIM); return Stream.empty(); } - - private Stream streamContactEdges() { - // If only we had (a working) Java 9... - // return Stream.iterate(getBody().getContactList(), c -> c.next) - // .takeWhile(Objects::nonNull); - - return StreamSupport.stream(new Spliterator() { - private ContactEdge contactEdge = getBody().getContactList(); - - @Override - public boolean tryAdvance(Consumer consumer) { - if (contactEdge == null) return false; - - consumer.accept(contactEdge); - contactEdge = contactEdge.next; - return true; - } - - @Override - public Spliterator trySplit() { - return null; - } - - @Override - public long estimateSize() { - return Long.MAX_VALUE; - } - - @Override - public int characteristics() { - return NONNULL; - } - }, false); - } } -- cgit v1.2.3