Skip to content

Commit 0b160cb

Browse files
authored
Merge branch 'master' into master
2 parents 4e0c2c2 + 3013175 commit 0b160cb

File tree

62 files changed

+6183
-60
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+6183
-60
lines changed

.all-contributorsrc

+92
Original file line numberDiff line numberDiff line change
@@ -2742,6 +2742,98 @@
27422742
"contributions": [
27432743
"translation"
27442744
]
2745+
},
2746+
{
2747+
"login": "Yenniferh",
2748+
"name": "Yennifer Herrera",
2749+
"avatar_url": "https://avatars.githubusercontent.com/u/19285706?v=4",
2750+
"profile": "http://www.linkedin.com/in/yenniferh",
2751+
"contributions": [
2752+
"translation",
2753+
"review"
2754+
]
2755+
},
2756+
{
2757+
"login": "jnniu-n",
2758+
"name": "jnniu-n",
2759+
"avatar_url": "https://avatars.githubusercontent.com/u/141746282?v=4",
2760+
"profile": "https://github.com/jnniu-n",
2761+
"contributions": [
2762+
"translation"
2763+
]
2764+
},
2765+
{
2766+
"login": "miguel-is-coding",
2767+
"name": "Miguel Angel Perez Garcia",
2768+
"avatar_url": "https://avatars.githubusercontent.com/u/48435117?v=4",
2769+
"profile": "https://github.com/miguel-is-coding",
2770+
"contributions": [
2771+
"review",
2772+
"translation"
2773+
]
2774+
},
2775+
{
2776+
"login": "SuwanSankaja",
2777+
"name": "Suwan Sankaja",
2778+
"avatar_url": "https://avatars.githubusercontent.com/u/86839778?v=4",
2779+
"profile": "https://github.com/SuwanSankaja",
2780+
"contributions": [
2781+
"translation"
2782+
]
2783+
},
2784+
{
2785+
"login": "Iamalok007",
2786+
"name": "alok",
2787+
"avatar_url": "https://avatars.githubusercontent.com/u/97467835?v=4",
2788+
"profile": "https://github.com/Iamalok007",
2789+
"contributions": [
2790+
"doc"
2791+
]
2792+
},
2793+
{
2794+
"login": "dlvideira",
2795+
"name": "Daniel Lisboa",
2796+
"avatar_url": "https://avatars.githubusercontent.com/u/53951425?v=4",
2797+
"profile": "https://github.com/dlvideira",
2798+
"contributions": [
2799+
"translation"
2800+
]
2801+
},
2802+
{
2803+
"login": "ComplexOW",
2804+
"name": "Sam Powell",
2805+
"avatar_url": "https://avatars.githubusercontent.com/u/105279107?v=4",
2806+
"profile": "https://github.com/ComplexOW",
2807+
"contributions": [
2808+
"doc"
2809+
]
2810+
},
2811+
{
2812+
"login": "jppf22",
2813+
"name": "João Fernandes",
2814+
"avatar_url": "https://avatars.githubusercontent.com/u/104360594?v=4",
2815+
"profile": "https://github.com/jppf22",
2816+
"contributions": [
2817+
"translation"
2818+
]
2819+
},
2820+
{
2821+
"login": "HongGeonUi",
2822+
"name": "Hong Geon-ui",
2823+
"avatar_url": "https://avatars.githubusercontent.com/u/89684956?v=4",
2824+
"profile": "https://github.com/HongGeonUi",
2825+
"contributions": [
2826+
"translation"
2827+
]
2828+
},
2829+
{
2830+
"login": "ylcn91",
2831+
"name": "Doksanbir",
2832+
"avatar_url": "https://avatars.githubusercontent.com/u/7249450?v=4",
2833+
"profile": "https://github.com/ylcn91",
2834+
"contributions": [
2835+
"code"
2836+
]
27452837
}
27462838
],
27472839
"contributorsPerLine": 7,

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
1111
[![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
1212
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
13-
[![All Contributors](https://img.shields.io/badge/all_contributors-301-orange.svg?style=flat-square)](#contributors-)
13+
[![All Contributors](https://img.shields.io/badge/all_contributors-311-orange.svg?style=flat-square)](#contributors-)
1414
<!-- ALL-CONTRIBUTORS-BADGE:END -->
1515

1616
<br/>
@@ -462,6 +462,20 @@ This project is licensed under the terms of the MIT license.
462462
<td align="center" valign="top" width="14.28%"><a href="https://github.com/JerryZhao275"><img src="https://avatars.githubusercontent.com/u/132061401?v=4?s=100" width="100px;" alt="JerryZhao275"/><br /><sub><b>JerryZhao275</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=JerryZhao275" title="Documentation">📖</a></td>
463463
<td align="center" valign="top" width="14.28%"><a href="https://twitter.com/llisant"><img src="https://avatars.githubusercontent.com/u/1445890?v=4?s=100" width="100px;" alt="Leonardo Lisanti"/><br /><sub><b>Leonardo Lisanti</b></sub></a><br /><a href="#translation-leonardol" title="Translation">🌍</a></td>
464464
</tr>
465+
<tr>
466+
<td align="center" valign="top" width="14.28%"><a href="http://www.linkedin.com/in/yenniferh"><img src="https://avatars.githubusercontent.com/u/19285706?v=4?s=100" width="100px;" alt="Yennifer Herrera"/><br /><sub><b>Yennifer Herrera</b></sub></a><br /><a href="#translation-Yenniferh" title="Translation">🌍</a> <a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3AYenniferh" title="Reviewed Pull Requests">👀</a></td>
467+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/jnniu-n"><img src="https://avatars.githubusercontent.com/u/141746282?v=4?s=100" width="100px;" alt="jnniu-n"/><br /><sub><b>jnniu-n</b></sub></a><br /><a href="#translation-jnniu-n" title="Translation">🌍</a></td>
468+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/miguel-is-coding"><img src="https://avatars.githubusercontent.com/u/48435117?v=4?s=100" width="100px;" alt="Miguel Angel Perez Garcia"/><br /><sub><b>Miguel Angel Perez Garcia</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3Amiguel-is-coding" title="Reviewed Pull Requests">👀</a> <a href="#translation-miguel-is-coding" title="Translation">🌍</a></td>
469+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/SuwanSankaja"><img src="https://avatars.githubusercontent.com/u/86839778?v=4?s=100" width="100px;" alt="Suwan Sankaja"/><br /><sub><b>Suwan Sankaja</b></sub></a><br /><a href="#translation-SuwanSankaja" title="Translation">🌍</a></td>
470+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Iamalok007"><img src="https://avatars.githubusercontent.com/u/97467835?v=4?s=100" width="100px;" alt="alok"/><br /><sub><b>alok</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=Iamalok007" title="Documentation">📖</a></td>
471+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/dlvideira"><img src="https://avatars.githubusercontent.com/u/53951425?v=4?s=100" width="100px;" alt="Daniel Lisboa"/><br /><sub><b>Daniel Lisboa</b></sub></a><br /><a href="#translation-dlvideira" title="Translation">🌍</a></td>
472+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/ComplexOW"><img src="https://avatars.githubusercontent.com/u/105279107?v=4?s=100" width="100px;" alt="Sam Powell"/><br /><sub><b>Sam Powell</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ComplexOW" title="Documentation">📖</a></td>
473+
</tr>
474+
<tr>
475+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/jppf22"><img src="https://avatars.githubusercontent.com/u/104360594?v=4?s=100" width="100px;" alt="João Fernandes"/><br /><sub><b>João Fernandes</b></sub></a><br /><a href="#translation-jppf22" title="Translation">🌍</a></td>
476+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/HongGeonUi"><img src="https://avatars.githubusercontent.com/u/89684956?v=4?s=100" width="100px;" alt="Hong Geon-ui"/><br /><sub><b>Hong Geon-ui</b></sub></a><br /><a href="#translation-HongGeonUi" title="Translation">🌍</a></td>
477+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/ylcn91"><img src="https://avatars.githubusercontent.com/u/7249450?v=4?s=100" width="100px;" alt="Doksanbir"/><br /><sub><b>Doksanbir</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ylcn91" title="Code">💻</a></td>
478+
</tr>
465479
</tbody>
466480
</table>
467481

commander/src/main/java/com/iluwatar/commander/Commander.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,8 @@ private void handlePaymentPossibleErrorMsgErrorIssue(Order order, Order o) {
477477
&& System.currentTimeMillis() - o.createdTime < messageTime) {
478478
var qt = new QueueTask(order, TaskType.MESSAGING, 1);
479479
updateQueue(qt);
480-
LOG.warn("Order " + order.id + ": Error in sending Payment Error message, "
481-
+ "trying to queue task and add to employee handle..");
480+
LOG.warn("Order {}: Error in sending Payment Error message, trying to queue task and add to employee handle..",
481+
order.id);
482482
employeeHandleIssue(o);
483483
}
484484
}

double-buffer/README.md

+232-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,243 @@
1-
---
1+
---
22
title: Double Buffer
33
category: Behavioral
44
language: en
55
tag:
66
- Performance
77
- Game programming
8-
---
8+
---
99

1010
## Intent
11-
Double buffering is a term used to describe a device that has two buffers. The usage of multiple buffers increases the overall throughput of a device and helps prevents bottlenecks. This example shows using double buffer pattern on graphics. It is used to show one image or frame while a separate frame is being buffered to be shown next. This method makes animations and games look more realistic than the same done in a single buffer mode.
11+
Double buffering is a term used to describe a device that has two buffers. The usage of multiple
12+
buffers increases the overall throughput of a device and helps prevents bottlenecks. This example
13+
shows using double buffer pattern on graphics. It is used to show one image or frame while a separate
14+
frame is being buffered to be shown next. This method makes animations and games look more realistic
15+
than the same done in a single buffer mode.
16+
17+
## Explanation
18+
19+
Real world example
20+
> A typical example, and one that every game engine must address, is rendering. When the game draws
21+
> the world the users see, it does so one piece at a time -- the mountains in the distance,
22+
> the rolling hills, the trees, each in its turn. If the user watched the view draw incrementally
23+
> like that, the illusion of a coherent world would be shattered. The scene must update smoothly
24+
> and quickly, displaying a series of complete frames, each appearing instantly. Double buffering solves
25+
> the problem.
26+
27+
In plain words
28+
> It ensures a state that is being rendered correctly while that state is modifying incrementally. It is
29+
> widely used in computer graphics.
30+
31+
Wikipedia says
32+
> In computer science, multiple buffering is the use of more than one buffer to hold a block of data,
33+
> so that a "reader" will see a complete (though perhaps old) version of the data, rather than a
34+
> partially updated version of the data being created by a "writer". It is very commonly used for
35+
> computer display images.
36+
37+
**Programmatic Example**
38+
39+
Buffer interface that assures basic functionalities of a buffer.
40+
41+
```java
42+
/**
43+
* Buffer interface.
44+
*/
45+
public interface Buffer {
46+
47+
/**
48+
* Clear the pixel in (x, y).
49+
*
50+
* @param x X coordinate
51+
* @param y Y coordinate
52+
*/
53+
void clear(int x, int y);
54+
55+
/**
56+
* Draw the pixel in (x, y).
57+
*
58+
* @param x X coordinate
59+
* @param y Y coordinate
60+
*/
61+
void draw(int x, int y);
62+
63+
/**
64+
* Clear all the pixels.
65+
*/
66+
void clearAll();
67+
68+
/**
69+
* Get all the pixels.
70+
*
71+
* @return pixel list
72+
*/
73+
Pixel[] getPixels();
74+
75+
}
76+
```
77+
78+
One of the implementation of Buffer interface.
79+
```java
80+
/**
81+
* FrameBuffer implementation class.
82+
*/
83+
public class FrameBuffer implements Buffer {
84+
85+
public static final int WIDTH = 10;
86+
public static final int HEIGHT = 8;
87+
88+
private final Pixel[] pixels = new Pixel[WIDTH * HEIGHT];
89+
90+
public FrameBuffer() {
91+
clearAll();
92+
}
93+
94+
@Override
95+
public void clear(int x, int y) {
96+
pixels[getIndex(x, y)] = Pixel.WHITE;
97+
}
98+
99+
@Override
100+
public void draw(int x, int y) {
101+
pixels[getIndex(x, y)] = Pixel.BLACK;
102+
}
103+
104+
@Override
105+
public void clearAll() {
106+
Arrays.fill(pixels, Pixel.WHITE);
107+
}
108+
109+
@Override
110+
public Pixel[] getPixels() {
111+
return pixels;
112+
}
113+
114+
private int getIndex(int x, int y) {
115+
return x + WIDTH * y;
116+
}
117+
}
118+
```
119+
120+
```java
121+
/**
122+
* Pixel enum. Each pixel can be white (not drawn) or black (drawn).
123+
*/
124+
public enum Pixel {
125+
126+
WHITE,
127+
BLACK;
128+
}
129+
```
130+
Scene represents the game scene where current buffer has already been rendered.
131+
```java
132+
/**
133+
* Scene class. Render the output frame.
134+
*/
135+
@Slf4j
136+
public class Scene {
137+
138+
private final Buffer[] frameBuffers;
139+
140+
private int current;
141+
142+
private int next;
143+
144+
/**
145+
* Constructor of Scene.
146+
*/
147+
public Scene() {
148+
frameBuffers = new FrameBuffer[2];
149+
frameBuffers[0] = new FrameBuffer();
150+
frameBuffers[1] = new FrameBuffer();
151+
current = 0;
152+
next = 1;
153+
}
154+
155+
/**
156+
* Draw the next frame.
157+
*
158+
* @param coordinateList list of pixels of which the color should be black
159+
*/
160+
public void draw(List<? extends Pair<Integer, Integer>> coordinateList) {
161+
LOGGER.info("Start drawing next frame");
162+
LOGGER.info("Current buffer: " + current + " Next buffer: " + next);
163+
frameBuffers[next].clearAll();
164+
coordinateList.forEach(coordinate -> {
165+
var x = coordinate.getKey();
166+
var y = coordinate.getValue();
167+
frameBuffers[next].draw(x, y);
168+
});
169+
LOGGER.info("Swap current and next buffer");
170+
swap();
171+
LOGGER.info("Finish swapping");
172+
LOGGER.info("Current buffer: " + current + " Next buffer: " + next);
173+
}
174+
175+
public Buffer getBuffer() {
176+
LOGGER.info("Get current buffer: " + current);
177+
return frameBuffers[current];
178+
}
179+
180+
private void swap() {
181+
current = current ^ next;
182+
next = current ^ next;
183+
current = current ^ next;
184+
}
185+
186+
}
187+
```
188+
189+
```java
190+
public static void main(String[] args) {
191+
final var scene = new Scene();
192+
var drawPixels1 = List.of(
193+
new MutablePair<>(1, 1),
194+
new MutablePair<>(5, 6),
195+
new MutablePair<>(3, 2)
196+
);
197+
scene.draw(drawPixels1);
198+
var buffer1 = scene.getBuffer();
199+
printBlackPixelCoordinate(buffer1);
200+
201+
var drawPixels2 = List.of(
202+
new MutablePair<>(3, 7),
203+
new MutablePair<>(6, 1)
204+
);
205+
scene.draw(drawPixels2);
206+
var buffer2 = scene.getBuffer();
207+
printBlackPixelCoordinate(buffer2);
208+
}
209+
210+
private static void printBlackPixelCoordinate(Buffer buffer) {
211+
StringBuilder log = new StringBuilder("Black Pixels: ");
212+
var pixels = buffer.getPixels();
213+
for (var i = 0; i < pixels.length; ++i) {
214+
if (pixels[i] == Pixel.BLACK) {
215+
var y = i / FrameBuffer.WIDTH;
216+
var x = i % FrameBuffer.WIDTH;
217+
log.append(" (").append(x).append(", ").append(y).append(")");
218+
}
219+
}
220+
LOGGER.info(log.toString());
221+
}
222+
```
223+
224+
The console output
225+
```text
226+
[main] INFO com.iluwatar.doublebuffer.Scene - Start drawing next frame
227+
[main] INFO com.iluwatar.doublebuffer.Scene - Current buffer: 0 Next buffer: 1
228+
[main] INFO com.iluwatar.doublebuffer.Scene - Swap current and next buffer
229+
[main] INFO com.iluwatar.doublebuffer.Scene - Finish swapping
230+
[main] INFO com.iluwatar.doublebuffer.Scene - Current buffer: 1 Next buffer: 0
231+
[main] INFO com.iluwatar.doublebuffer.Scene - Get current buffer: 1
232+
[main] INFO com.iluwatar.doublebuffer.App - Black Pixels: (1, 1) (3, 2) (5, 6)
233+
[main] INFO com.iluwatar.doublebuffer.Scene - Start drawing next frame
234+
[main] INFO com.iluwatar.doublebuffer.Scene - Current buffer: 1 Next buffer: 0
235+
[main] INFO com.iluwatar.doublebuffer.Scene - Swap current and next buffer
236+
[main] INFO com.iluwatar.doublebuffer.Scene - Finish swapping
237+
[main] INFO com.iluwatar.doublebuffer.Scene - Current buffer: 0 Next buffer: 1
238+
[main] INFO com.iluwatar.doublebuffer.Scene - Get current buffer: 0
239+
[main] INFO com.iluwatar.doublebuffer.App - Black Pixels: (6, 1) (3, 7)
240+
```
12241

13242
## Class diagram
14243
![alt text](./etc/double-buffer.urm.png "Double Buffer pattern class diagram")

extension-objects/src/main/java/App.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static void checkExtensionsForUnit(Unit unit) {
6262
final var logger = LoggerFactory.getLogger(App.class);
6363

6464
var name = unit.getName();
65-
Function<String, Runnable> func = (e) -> () -> logger.info(name + " without " + e);
65+
Function<String, Runnable> func = e -> () -> logger.info("{} without {}", name, e);
6666

6767
var extension = "SoldierExtension";
6868
Optional.ofNullable(unit.getUnitExtension(extension))

0 commit comments

Comments
 (0)