Skip to content

Commit dce767c

Browse files
committed
first commit
1 parent 073d06c commit dce767c

File tree

8 files changed

+277
-0
lines changed

8 files changed

+277
-0
lines changed

event-queue/README.md

Whitespace-only changes.

event-queue/etc/Bass-Drum-1.wav

216 KB
Binary file not shown.

event-queue/etc/Closed-Hi-Hat-1.wav

12.7 KB
Binary file not shown.

event-queue/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2014-2016 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
28+
<modelVersion>4.0.0</modelVersion>
29+
<parent>
30+
<groupId>com.iluwatar</groupId>
31+
<artifactId>java-design-patterns</artifactId>
32+
<version>1.16.0-SNAPSHOT</version>
33+
</parent>
34+
<artifactId>event-queue</artifactId>
35+
<dependencies>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
</dependencies>
42+
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.event.queue;
25+
26+
import java.io.BufferedReader;
27+
import java.io.File;
28+
import java.io.IOException;
29+
import java.io.InputStreamReader;
30+
31+
import javax.sound.sampled.AudioInputStream;
32+
import javax.sound.sampled.AudioSystem;
33+
import javax.sound.sampled.UnsupportedAudioFileException;
34+
35+
/**
36+
* Event or message queues provide an asynchronous communications protocol, meaning that the sender
37+
* and receiver of the message do not need to interact with the message queue at the same time.
38+
* Events or messages placed onto the queue are stored until the recipient retrieves them. Event
39+
* or message queues have implicit or explicit limits on the size of data that may be transmitted
40+
* in a single message and the number of messages that may remain outstanding on the queue.
41+
* A queue stores a series of notifications or requests in first-in, first-out order.
42+
* Sending a notification enqueues the request and returns. The request processor then processes
43+
* items from the queue at a later time.
44+
*/
45+
public class App {
46+
/**
47+
* Program entry point.
48+
*
49+
* @param args command line args
50+
* @throws IOException when there is a problem with the audio file loading
51+
* @throws UnsupportedAudioFileException when the loaded audio file is unsupported
52+
*/
53+
public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
54+
Audio.playSound(getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
55+
Audio.playSound(getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f);
56+
57+
System.out.println("Press Enter key to stop the program...");
58+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
59+
br.read();
60+
Audio.stopService();
61+
}
62+
63+
public static AudioInputStream getAudioStream(String filePath)
64+
throws UnsupportedAudioFileException, IOException {
65+
return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
66+
}
67+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.event.queue;
25+
26+
import java.io.IOException;
27+
28+
import javax.sound.sampled.AudioInputStream;
29+
import javax.sound.sampled.AudioSystem;
30+
import javax.sound.sampled.Clip;
31+
import javax.sound.sampled.LineUnavailableException;
32+
33+
/**
34+
* This class implements the Event Queue pattern.
35+
* @author mkuprivecz
36+
*
37+
*/
38+
public class Audio {
39+
40+
private static final int MAX_PENDING = 16;
41+
42+
private static int headIndex;
43+
44+
private static int tailIndex;
45+
46+
private static Thread updateThread = null;
47+
48+
private static PlayMessage[] pendingAudio = new PlayMessage[MAX_PENDING];
49+
50+
public static boolean isServiceRunning() {
51+
return updateThread.isAlive();
52+
}
53+
54+
/**
55+
* This method stops the Update Method's thread.
56+
*/
57+
public static void stopService() {
58+
if (updateThread != null) {
59+
updateThread.interrupt();
60+
}
61+
}
62+
63+
/**
64+
* Starts the thread for the Update Method pattern if it was not started previously.
65+
* Also when the thread is is ready initializes the indexes of the queue
66+
*/
67+
public static void init() {
68+
if (updateThread == null) {
69+
updateThread = new Thread(new Runnable() {
70+
public void run() {
71+
while (!Thread.currentThread().isInterrupted()) {
72+
Audio.update();
73+
}
74+
}
75+
});
76+
}
77+
if (!updateThread.isAlive()) {
78+
updateThread.start();
79+
headIndex = 0;
80+
tailIndex = 0;
81+
}
82+
}
83+
84+
/**
85+
* This method adds a new audio into the queue.
86+
* @param stream is the AudioInputStream for the method
87+
* @param volume is the level of the audio's volume
88+
*/
89+
public static void playSound(AudioInputStream stream, float volume) {
90+
init();
91+
// Walk the pending requests.
92+
for (int i = headIndex; i != tailIndex; i = (i + 1) % MAX_PENDING) {
93+
if (pendingAudio[i].stream == stream) {
94+
// Use the larger of the two volumes.
95+
pendingAudio[i].volume = Math.max(volume, pendingAudio[i].volume);
96+
97+
// Don't need to enqueue.
98+
return;
99+
}
100+
}
101+
pendingAudio[tailIndex] = new PlayMessage();
102+
pendingAudio[tailIndex].stream = stream;
103+
pendingAudio[tailIndex].volume = volume;
104+
tailIndex = (tailIndex + 1) % MAX_PENDING;
105+
}
106+
107+
/**
108+
* This method uses the Update Method pattern.
109+
* It takes the audio from the queue and plays it
110+
*/
111+
public static void update() {
112+
// If there are no pending requests, do nothing.
113+
if (headIndex == tailIndex) {
114+
return;
115+
}
116+
Clip clip = null;
117+
try {
118+
clip = AudioSystem.getClip();
119+
clip.open(pendingAudio[headIndex].stream);
120+
} catch (LineUnavailableException e) {
121+
System.err.println("Error occoured while loading the audio: The line is unavailable");
122+
e.printStackTrace();
123+
} catch (IOException e) {
124+
System.err.println("Input/Output error while loading the audio");
125+
e.printStackTrace();
126+
}
127+
clip.start();
128+
129+
headIndex++;
130+
}
131+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.event.queue;
25+
26+
import javax.sound.sampled.AudioInputStream;
27+
28+
/**
29+
* The Event Queue's queue will store the instances of this class.
30+
* @author mkuprivecz
31+
*
32+
*/
33+
public class PlayMessage {
34+
AudioInputStream stream;
35+
float volume;
36+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<module>promise</module>
134134
<module>page-object</module>
135135
<module>event-asynchronous</module>
136+
<module>event-queue</module>
136137
<module>queue-load-leveling</module>
137138
<module>object-mother</module>
138139
<module>converter</module>

0 commit comments

Comments
 (0)