Skip to content

Commit dde7817

Browse files
committed
remove some duplicated blocks
1 parent bd225e1 commit dde7817

File tree

14 files changed

+142
-261
lines changed

14 files changed

+142
-261
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/application/SplashScreen.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.lecousin.framework.concurrent.async.CancelException;
2424
import net.lecousin.framework.concurrent.async.IAsync;
2525
import net.lecousin.framework.event.AsyncEvent;
26+
import net.lecousin.framework.event.SimpleListenableContainer;
2627
import net.lecousin.framework.progress.WorkProgress;
2728
import net.lecousin.framework.util.ThreadUtil;
2829

@@ -31,7 +32,7 @@
3132
* It starts a new thread so the loading process can continue while the window is initializing,
3233
* and avoid slowing down the loading process.
3334
*/
34-
public class SplashScreen implements WorkProgress {
35+
public class SplashScreen extends SimpleListenableContainer<AsyncEvent> implements WorkProgress {
3536

3637
/** Constructor.
3738
* @param devMode if true the screen is automatically closed after 20 seconds if the application is still loading.
@@ -66,7 +67,11 @@ public SplashScreen(boolean devMode) {
6667
private long amount = 10000;
6768
private long worked = 0;
6869
private Async<Exception> synch = new Async<>();
69-
private AsyncEvent event = null;
70+
71+
@Override
72+
protected AsyncEvent createEvent() {
73+
return new AsyncEvent();
74+
}
7075

7176
/** Close this window. */
7277
public void close() {
@@ -378,22 +383,6 @@ public IAsync<Exception> getSynch() {
378383
return synch;
379384
}
380385

381-
@Override
382-
public void listen(Runnable onchange) {
383-
synchronized (this) {
384-
if (event == null) event = new AsyncEvent();
385-
event.addListener(onchange);
386-
}
387-
}
388-
389-
@Override
390-
public void unlisten(Runnable onchange) {
391-
synchronized (this) {
392-
if (event == null) return;
393-
event.removeListener(onchange);
394-
}
395-
}
396-
397386
private boolean eventInterrupted;
398387

399388
@Override
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package net.lecousin.framework.event;
2+
3+
/** Class that implements SimpleListenable and delegate to an event of type T which is lazily instantiated on its first listener.
4+
* @param <T> type of event
5+
*/
6+
public abstract class SimpleListenableContainer<T extends SimpleListenable> implements SimpleListenable {
7+
8+
protected T event;
9+
10+
protected abstract T createEvent();
11+
12+
@Override
13+
public void addListener(Runnable listener) {
14+
synchronized (this) {
15+
if (event == null) event = createEvent();
16+
event.addListener(listener);
17+
}
18+
}
19+
20+
@Override
21+
public void removeListener(Runnable listener) {
22+
synchronized (this) {
23+
if (event == null) return;
24+
event.removeListener(listener);
25+
}
26+
}
27+
28+
@Override
29+
public boolean hasListeners() {
30+
return event.hasListeners();
31+
}
32+
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net.lecousin.framework.io;
2+
3+
import java.io.IOException;
4+
5+
import net.lecousin.framework.util.ConcurrentCloseable;
6+
7+
/** Utility class as a base for IO implementations. */
8+
public abstract class AbstractIO extends ConcurrentCloseable<IOException> implements IO {
9+
10+
/** Constructor. */
11+
public AbstractIO(String description, byte priority) {
12+
this.description = description;
13+
this.priority = priority;
14+
}
15+
16+
protected String description;
17+
protected byte priority;
18+
19+
@Override
20+
public String getSourceDescription() {
21+
return description;
22+
}
23+
24+
@Override
25+
public byte getPriority() {
26+
return priority;
27+
}
28+
29+
@Override
30+
public void setPriority(byte priority) {
31+
this.priority = priority;
32+
}
33+
34+
}

net.lecousin.core/src/main/java/net/lecousin/framework/io/IOFromInputStream.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,20 @@
1111
import net.lecousin.framework.concurrent.async.AsyncSupplier;
1212
import net.lecousin.framework.concurrent.async.CancelException;
1313
import net.lecousin.framework.concurrent.async.IAsync;
14-
import net.lecousin.framework.util.ConcurrentCloseable;
1514
import net.lecousin.framework.util.Pair;
1615

1716
/** Implements Readable from an InputStream. */
18-
public class IOFromInputStream extends ConcurrentCloseable<IOException> implements IO.Readable {
17+
public class IOFromInputStream extends AbstractIO implements IO.Readable {
1918

2019
/** Constructor. */
2120
public IOFromInputStream(InputStream stream, String sourceDescription, TaskManager manager, byte priority) {
21+
super(sourceDescription, priority);
2222
this.stream = stream;
23-
this.sourceDescription = sourceDescription;
2423
this.manager = manager;
25-
this.priority = priority;
2624
}
2725

2826
private InputStream stream;
29-
private String sourceDescription;
3027
private TaskManager manager;
31-
private byte priority;
3228

3329
/** Add the capability to get the size to IOFromInputStream. */
3430
public static class KnownSize extends IOFromInputStream implements IO.KnownSize {
@@ -58,18 +54,9 @@ public IAsync<IOException> canStartReading() {
5854

5955
public InputStream getInputStream() { return stream; }
6056

61-
@Override
62-
public String getSourceDescription() { return sourceDescription; }
63-
6457
@Override
6558
public IO getWrappedIO() { return null; }
6659

67-
@Override
68-
public byte getPriority() { return priority; }
69-
70-
@Override
71-
public void setPriority(byte priority) { this.priority = priority; }
72-
7360
@Override
7461
public TaskManager getTaskManager() {
7562
return manager;

net.lecousin.core/src/main/java/net/lecousin/framework/io/IOFromOutputStream.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,31 @@
99
import net.lecousin.framework.concurrent.async.Async;
1010
import net.lecousin.framework.concurrent.async.AsyncSupplier;
1111
import net.lecousin.framework.concurrent.async.IAsync;
12-
import net.lecousin.framework.util.ConcurrentCloseable;
1312
import net.lecousin.framework.util.Pair;
1413

1514
/**
1615
* Implements Writable from an OutputStream.
1716
*/
18-
public class IOFromOutputStream extends ConcurrentCloseable<IOException> implements IO.Writable {
17+
public class IOFromOutputStream extends AbstractIO implements IO.Writable {
1918

2019
/** Constructor. */
2120
public IOFromOutputStream(OutputStream stream, String sourceDescription, TaskManager manager, byte priority) {
21+
super(sourceDescription, priority);
2222
this.stream = stream;
23-
this.sourceDescription = sourceDescription;
2423
this.manager = manager;
25-
this.priority = priority;
2624
}
2725

2826
private OutputStream stream;
29-
private String sourceDescription;
3027
private TaskManager manager;
31-
private byte priority;
3228

3329
public OutputStream getOutputStream() {
3430
return stream;
3531
}
3632

37-
@Override
38-
public String getSourceDescription() {
39-
return sourceDescription;
40-
}
41-
4233
@Override
4334
public IO getWrappedIO() {
4435
return null;
4536
}
46-
47-
@Override
48-
public byte getPriority() {
49-
return priority;
50-
}
51-
52-
@Override
53-
public void setPriority(byte priority) {
54-
this.priority = priority;
55-
}
5637

5738
@Override
5839
public TaskManager getTaskManager() {

net.lecousin.core/src/main/java/net/lecousin/framework/io/buffering/ByteArrayIO.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
import net.lecousin.framework.concurrent.async.Async;
1313
import net.lecousin.framework.concurrent.async.AsyncSupplier;
1414
import net.lecousin.framework.concurrent.async.IAsync;
15+
import net.lecousin.framework.io.AbstractIO;
1516
import net.lecousin.framework.io.IO;
1617
import net.lecousin.framework.io.IOUtil;
17-
import net.lecousin.framework.util.ConcurrentCloseable;
1818
import net.lecousin.framework.util.Pair;
1919

2020
/**
2121
* IO implemented with a single byte array.
2222
* It supports read, write and resize operations.
2323
*/
24-
public class ByteArrayIO extends ConcurrentCloseable<IOException>
24+
public class ByteArrayIO extends AbstractIO
2525
implements IO.Readable.Buffered, IO.Readable.Seekable, IO.Writable.Seekable, IO.Writable.Buffered, IO.KnownSize, IO.Resizable {
2626

2727
/** Constructor with initial buffer size of 4096. */
@@ -31,48 +31,37 @@ public ByteArrayIO(String description) {
3131

3232
/** Constructor. */
3333
public ByteArrayIO(int initialSize, String description) {
34-
this.description = description;
34+
super(description, Task.PRIORITY_NORMAL);
3535
array = new byte[initialSize];
3636
pos = size = 0;
3737
}
3838

3939
/** Constructor. */
4040
public ByteArrayIO(byte[] data, String description) {
41-
this.description = description;
41+
super(description, Task.PRIORITY_NORMAL);
4242
array = data;
4343
pos = 0;
4444
size = data.length;
4545
}
4646

4747
/** Constructor. */
4848
public ByteArrayIO(byte[] data, int bytesUsed, String description) {
49-
this.description = description;
49+
super(description, Task.PRIORITY_NORMAL);
5050
array = data;
5151
pos = 0;
5252
size = bytesUsed;
5353
}
5454

55-
private String description;
5655
private byte[] array;
5756
private int pos;
5857
private int size;
59-
private byte priority = Task.PRIORITY_NORMAL;
6058

6159
@Override
6260
public TaskManager getTaskManager() { return Threading.getCPUTaskManager(); }
6361

6462
@Override
6563
public IO getWrappedIO() { return null; }
6664

67-
@Override
68-
public String getSourceDescription() { return description; }
69-
70-
@Override
71-
public byte getPriority() { return priority; }
72-
73-
@Override
74-
public void setPriority(byte priority) { this.priority = priority; }
75-
7665
@Override
7766
protected IAsync<IOException> closeUnderlyingResources() {
7867
return null;

net.lecousin.core/src/main/java/net/lecousin/framework/io/buffering/ByteBuffersIO.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,28 @@
1111
import net.lecousin.framework.concurrent.async.Async;
1212
import net.lecousin.framework.concurrent.async.AsyncSupplier;
1313
import net.lecousin.framework.concurrent.async.IAsync;
14+
import net.lecousin.framework.io.AbstractIO;
1415
import net.lecousin.framework.io.IO;
1516
import net.lecousin.framework.io.IOUtil;
16-
import net.lecousin.framework.util.ConcurrentCloseable;
1717
import net.lecousin.framework.util.Pair;
1818
import net.lecousin.framework.util.Triple;
1919

2020
/**
2121
* Implementation of IO using a list of byte array.
2222
*/
23-
public class ByteBuffersIO extends ConcurrentCloseable<IOException> implements IO.Readable.Buffered, IO.Readable.Seekable, IO.KnownSize, IO.Writable {
23+
public class ByteBuffersIO extends AbstractIO implements IO.Readable.Buffered, IO.Readable.Seekable, IO.KnownSize, IO.Writable {
2424

2525
/** Constructor.
2626
* @param copyBuffers if true each written buffer is copied into a new buffer,
2727
* if false the buffer are kept but any modification of them outside of this class may lead to unexpected behavior.
2828
* @param description description
2929
*/
3030
public ByteBuffersIO(boolean copyBuffers, String description, byte priority) {
31+
super(description, priority);
3132
this.copyBuffers = copyBuffers;
32-
this.description = description;
33-
this.priority = priority;
3433
}
3534

3635
private boolean copyBuffers;
37-
private String description;
38-
private byte priority;
3936
private LinkedArrayList<Triple<byte[],Integer,Integer>> buffers = new LinkedArrayList<>(10);
4037
private int pos = 0;
4138
private int bufferIndex = 0;
@@ -258,26 +255,11 @@ public AsyncSupplier<Long, IOException> skipAsync(long n, Consumer<Pair<Long,IOE
258255
return IOUtil.success(Long.valueOf(skipSync(n)), ondone);
259256
}
260257

261-
@Override
262-
public String getSourceDescription() {
263-
return description;
264-
}
265-
266258
@Override
267259
public IO getWrappedIO() {
268260
return null;
269261
}
270262

271-
@Override
272-
public byte getPriority() {
273-
return priority;
274-
}
275-
276-
@Override
277-
public void setPriority(byte priority) {
278-
this.priority = priority;
279-
}
280-
281263
@Override
282264
public TaskManager getTaskManager() {
283265
return Threading.getCPUTaskManager();

0 commit comments

Comments
 (0)