pixel5 = new MutablePair<>(6, 1);
+ drawPixels.add(pixel4);
+ drawPixels.add(pixel5);
+ scene.draw(drawPixels);
+ Buffer buffer2 = scene.getBuffer();
+ printBlackPixelCoordinate(buffer2);
+ }
+
+ private static void printBlackPixelCoordinate(Buffer buffer) {
+ var log = "Black Pixels: ";
+ Pixel[] pixels = buffer.getPixels();
+ for (var i = 0; i < pixels.length; ++i) {
+ if (pixels[i] == Pixel.BLACK) {
+ var y = i / FrameBuffer.WIDTH;
+ var x = i % FrameBuffer.WIDTH;
+ log += " (" + x + ", " + y + ")";
+ }
+ }
+ LOGGER.info(log);
+ }
+}
diff --git a/double-buffer/src/main/java/com/iluwatar/doublebuffer/Buffer.java b/double-buffer/src/main/java/com/iluwatar/doublebuffer/Buffer.java
new file mode 100644
index 000000000000..b8c15a914e35
--- /dev/null
+++ b/double-buffer/src/main/java/com/iluwatar/doublebuffer/Buffer.java
@@ -0,0 +1,56 @@
+/**
+ * The MIT License
+ * Copyright (c) 2014-2016 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+package com.iluwatar.doublebuffer;
+
+/**
+ * Buffer interface.
+ */
+public interface Buffer {
+
+ /**
+ * Clear the pixel in (x, y).
+ * @param x X coordinate
+ * @param y Y coordinate
+ */
+ void clear(int x, int y);
+
+ /**
+ * Draw the pixel in (x, y).
+ * @param x X coordinate
+ * @param y Y coordinate
+ */
+ void draw(int x, int y);
+
+ /**
+ * Clear all the pixels.
+ */
+ void clearAll();
+
+ /**
+ * Get all the pixels.
+ * @return pixel list
+ */
+ Pixel[] getPixels();
+
+}
diff --git a/double-buffer/src/main/java/com/iluwatar/doublebuffer/FrameBuffer.java b/double-buffer/src/main/java/com/iluwatar/doublebuffer/FrameBuffer.java
new file mode 100644
index 000000000000..2730140a3d99
--- /dev/null
+++ b/double-buffer/src/main/java/com/iluwatar/doublebuffer/FrameBuffer.java
@@ -0,0 +1,68 @@
+/**
+ * The MIT License
+ * Copyright (c) 2014-2016 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+package com.iluwatar.doublebuffer;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * FrameBuffer implementation class.
+ */
+public class FrameBuffer implements Buffer {
+
+ public static final int WIDTH = 10;
+ public static final int HEIGHT = 8;
+
+ private Pixel[] pixels = new Pixel[WIDTH * HEIGHT];
+
+ public FrameBuffer() {
+ clearAll();
+ }
+
+ @Override
+ public void clear(int x, int y) {
+ pixels[getIndex(x, y)] = Pixel.WHITE;
+ }
+
+ @Override
+ public void draw(int x, int y) {
+ pixels[getIndex(x, y)] = Pixel.BLACK;
+ }
+
+ @Override
+ public void clearAll() {
+ for (var i = 0; i < pixels.length; ++i) {
+ pixels[i] = Pixel.WHITE;
+ }
+ }
+
+ @Override
+ public Pixel[] getPixels() {
+ return pixels;
+ }
+
+ private int getIndex(int x, int y) {
+ return x + WIDTH * y;
+ }
+}
diff --git a/double-buffer/src/main/java/com/iluwatar/doublebuffer/Pixel.java b/double-buffer/src/main/java/com/iluwatar/doublebuffer/Pixel.java
new file mode 100644
index 000000000000..d693f4e2801c
--- /dev/null
+++ b/double-buffer/src/main/java/com/iluwatar/doublebuffer/Pixel.java
@@ -0,0 +1,39 @@
+/**
+ * The MIT License
+ * Copyright (c) 2014-2016 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+package com.iluwatar.doublebuffer;
+
+/**
+ * Pixel enum. Each pixel can be white (not drawn) or black (drawn).
+ */
+public enum Pixel {
+
+ WHITE(0),
+ BLACK(1);
+
+ private int color;
+
+ Pixel(int color) {
+ this.color = color;
+ }
+}
diff --git a/double-buffer/src/main/java/com/iluwatar/doublebuffer/Scene.java b/double-buffer/src/main/java/com/iluwatar/doublebuffer/Scene.java
new file mode 100644
index 000000000000..2f5b668e2ac0
--- /dev/null
+++ b/double-buffer/src/main/java/com/iluwatar/doublebuffer/Scene.java
@@ -0,0 +1,86 @@
+/**
+ * The MIT License
+ * Copyright (c) 2014-2016 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+package com.iluwatar.doublebuffer;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+/**
+ * Scene class. Render the output frame.
+ */
+public class Scene {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(Scene.class);
+
+ private Buffer[] frameBuffers;
+
+ private int current;
+
+ private int next;
+
+ /**
+ * Constructor of Scene.
+ */
+ public Scene() {
+ frameBuffers = new FrameBuffer[2];
+ frameBuffers[0] = new FrameBuffer();
+ frameBuffers[1] = new FrameBuffer();
+ current = 0;
+ next = 1;
+ }
+
+ /**
+ * Draw the next frame.
+ * @param coordinateList list of pixels of which the color should be black
+ */
+ public void draw(List> coordinateList) {
+ LOGGER.info("Start drawing next frame");
+ LOGGER.info("Current buffer: " + current + " Next buffer: " + next);
+ frameBuffers[next].clearAll();
+ for (Pair coordinate : coordinateList) {
+ var x = coordinate.getKey();
+ var y = coordinate.getValue();
+ frameBuffers[next].draw(x, y);
+ }
+ LOGGER.info("Swap current and next buffer");
+ swap();
+ LOGGER.info("Finish swapping");
+ LOGGER.info("Current buffer: " + current + " Next buffer: " + next);
+ }
+
+ public Buffer getBuffer() {
+ LOGGER.info("Get current buffer: " + current);
+ return frameBuffers[current];
+ }
+
+ private void swap() {
+ current = current ^ next;
+ next = current ^ next;
+ current = current ^ next;
+ }
+
+}
diff --git a/double-buffer/src/test/java/com/iluwatar/doublebuffer/AppTest.java b/double-buffer/src/test/java/com/iluwatar/doublebuffer/AppTest.java
new file mode 100644
index 000000000000..ebbabf980476
--- /dev/null
+++ b/double-buffer/src/test/java/com/iluwatar/doublebuffer/AppTest.java
@@ -0,0 +1,39 @@
+/**
+ * The MIT License
+ * Copyright (c) 2014-2016 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+package com.iluwatar.doublebuffer;
+
+import org.junit.Test;
+
+/**
+ * App unit test.
+ */
+public class AppTest {
+
+ @Test
+ public void testMain() {
+ String[] args = {};
+ App.main(args);
+ }
+
+}
diff --git a/double-buffer/src/test/java/com/iluwatar/doublebuffer/FrameBufferTest.java b/double-buffer/src/test/java/com/iluwatar/doublebuffer/FrameBufferTest.java
new file mode 100644
index 000000000000..689af2919e9f
--- /dev/null
+++ b/double-buffer/src/test/java/com/iluwatar/doublebuffer/FrameBufferTest.java
@@ -0,0 +1,97 @@
+/**
+ * The MIT License
+ * Copyright (c) 2014-2016 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+package com.iluwatar.doublebuffer;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * FrameBuffer unit test.
+ */
+public class FrameBufferTest {
+
+ @Test
+ public void testClearAll() {
+ try {
+ var field = FrameBuffer.class.getDeclaredField("pixels");
+ Pixel[] pixels = new Pixel[FrameBuffer.HEIGHT * FrameBuffer.WIDTH];
+ for (int i = 0; i < pixels.length; ++i) {
+ pixels[i] = Pixel.WHITE;
+ }
+ pixels[0] = Pixel.BLACK;
+ var frameBuffer = new FrameBuffer();
+ field.setAccessible(true);
+ field.set(frameBuffer, pixels);
+ frameBuffer.clearAll();
+ Assert.assertEquals(Pixel.WHITE, frameBuffer.getPixels()[0]);
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ Assert.fail("Fail to modify field access.");
+ }
+ }
+
+ @Test
+ public void testClear() {
+ try {
+ var field = FrameBuffer.class.getDeclaredField("pixels");
+ Pixel[] pixels = new Pixel[FrameBuffer.HEIGHT * FrameBuffer.WIDTH];
+ for (int i = 0; i < pixels.length; ++i) {
+ pixels[i] = Pixel.WHITE;
+ }
+ pixels[0] = Pixel.BLACK;
+ var frameBuffer = new FrameBuffer();
+ field.setAccessible(true);
+ field.set(frameBuffer, pixels);
+ frameBuffer.clear(0, 0);
+ Assert.assertEquals(Pixel.WHITE, frameBuffer.getPixels()[0]);
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ Assert.fail("Fail to modify field access.");
+ }
+ }
+
+ @Test
+ public void testDraw() {
+ var frameBuffer = new FrameBuffer();
+ frameBuffer.draw(0, 0);
+ Assert.assertEquals(Pixel.BLACK, frameBuffer.getPixels()[0]);
+ }
+
+ @Test
+ public void testGetPixels() {
+ try {
+ var field = FrameBuffer.class.getDeclaredField("pixels");
+ Pixel[] pixels = new Pixel[FrameBuffer.HEIGHT * FrameBuffer.WIDTH];
+ for (int i = 0; i < pixels.length; ++i) {
+ pixels[i] = Pixel.WHITE;
+ }
+ pixels[0] = Pixel.BLACK;
+ var frameBuffer = new FrameBuffer();
+ field.setAccessible(true);
+ field.set(frameBuffer, pixels);
+ Assert.assertEquals(pixels, frameBuffer.getPixels());
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ Assert.fail("Fail to modify field access.");
+ }
+ }
+
+}
diff --git a/double-buffer/src/test/java/com/iluwatar/doublebuffer/SceneTest.java b/double-buffer/src/test/java/com/iluwatar/doublebuffer/SceneTest.java
new file mode 100644
index 000000000000..ab332aa22360
--- /dev/null
+++ b/double-buffer/src/test/java/com/iluwatar/doublebuffer/SceneTest.java
@@ -0,0 +1,74 @@
+/**
+ * The MIT License
+ * Copyright (c) 2014-2016 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+package com.iluwatar.doublebuffer;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+
+/**
+ * Scene unit tests.
+ */
+public class SceneTest {
+
+ @Test
+ public void testGetBuffer() {
+ try {
+ var scene = new Scene();
+ var field1 = Scene.class.getDeclaredField("current");
+ field1.setAccessible(true);
+ field1.set(scene, 0);
+ FrameBuffer[] frameBuffers = new FrameBuffer[2];
+ FrameBuffer frameBuffer = new FrameBuffer();
+ frameBuffer.draw(0, 0);
+ frameBuffers[0] = frameBuffer;
+ var field2 = Scene.class.getDeclaredField("frameBuffers");
+ field2.setAccessible(true);
+ field2.set(scene, frameBuffers);
+ Assert.assertEquals(frameBuffer, scene.getBuffer());
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ Assert.fail("Fail to access private field.");
+ }
+ }
+
+ @Test
+ public void testDraw() {
+ try {
+ var scene = new Scene();
+ var field1 = Scene.class.getDeclaredField("current");
+ var field2 = Scene.class.getDeclaredField("next");
+ field1.setAccessible(true);
+ field1.set(scene, 0);
+ field2.setAccessible(true);
+ field2.set(scene, 1);
+ scene.draw(new ArrayList<>());
+ Assert.assertEquals(1, field1.get(scene));
+ Assert.assertEquals(0, field2.get(scene));
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ Assert.fail("Fail to access private field");
+ }
+ }
+}
diff --git a/pom.xml b/pom.xml
index 0ef4890ca065..913f600e7aff 100644
--- a/pom.xml
+++ b/pom.xml
@@ -183,6 +183,7 @@
data-locality
subclass-sandbox
circuit-breaker
+ double-buffer
From f00ebe1a8de224dadcc1526e1771825522be8c15 Mon Sep 17 00:00:00 2001
From: Anurag870
Date: Sun, 20 Oct 2019 21:31:02 +0530
Subject: [PATCH 10/16] #984 local variable inference changes (#1025)
* #984 Fix for abstract-document, abstract-factory, acyclic-visitor, adapter, aggregator-microservices
* #984 Fix for abstract-document, abstract-factory, acyclic-visitor, adapter, aggregator-microservices
---
.../iluwatar/abstractdocument/AbstractDocument.java | 2 +-
.../java/com/iluwatar/abstractdocument/App.java | 9 ++++-----
.../main/java/com/iluwatar/acyclicvisitor/App.java | 8 ++++----
.../acyclicvisitor/ConfigureForDosVisitorTest.java | 12 ++----------
.../acyclicvisitor/ConfigureForUnixVisitorTest.java | 4 ++--
.../java/com/iluwatar/acyclicvisitor/HayesTest.java | 13 ++++---------
.../java/com/iluwatar/acyclicvisitor/ZoomTest.java | 13 ++++---------
adapter/src/main/java/com/iluwatar/adapter/App.java | 2 +-
.../com/iluwatar/adapter/AdapterPatternTest.java | 6 +++---
.../aggregator/microservices/Aggregator.java | 2 +-
.../microservices/ProductInformationClientImpl.java | 6 +++---
.../microservices/ProductInventoryClientImpl.java | 8 ++++----
12 files changed, 33 insertions(+), 52 deletions(-)
diff --git a/abstract-document/src/main/java/com/iluwatar/abstractdocument/AbstractDocument.java b/abstract-document/src/main/java/com/iluwatar/abstractdocument/AbstractDocument.java
index ec5ff822ec7d..dee8302c1539 100644
--- a/abstract-document/src/main/java/com/iluwatar/abstractdocument/AbstractDocument.java
+++ b/abstract-document/src/main/java/com/iluwatar/abstractdocument/AbstractDocument.java
@@ -61,7 +61,7 @@ public Stream children(String key, Function