Skip to content

Commit b3eb2f8

Browse files
committed
adaptations to make a more developer friendly AndroidASAP lib
1 parent 49b6064 commit b3eb2f8

File tree

10 files changed

+109
-59
lines changed

10 files changed

+109
-59
lines changed

src/net/sharksystem/asap/ASAPChannel.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.sharksystem.asap;
22

3+
import net.sharksystem.asap.apps.ASAPMessages;
4+
35
import java.io.IOException;
46
import java.util.HashMap;
57
import java.util.Set;
@@ -9,6 +11,6 @@ public interface ASAPChannel {
911
CharSequence getUri() throws IOException;
1012
Set<CharSequence> getRecipients() throws IOException;
1113
HashMap<String, String> getExtraData() throws IOException;
12-
ASAPChannelMessages getMessages() throws IOException;
14+
ASAPMessages getMessages() throws IOException;
1315
void addMessage(byte[] message) throws IOException;
1416
}

src/net/sharksystem/asap/ASAPChannelImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.sharksystem.asap;
22

3+
import net.sharksystem.asap.apps.ASAPMessages;
4+
35
import java.io.IOException;
46
import java.util.HashMap;
57
import java.util.Set;
@@ -35,7 +37,7 @@ public HashMap<String, String> getExtraData() throws IOException {
3537
}
3638

3739
@Override
38-
public ASAPChannelMessages getMessages() throws IOException {
40+
public ASAPMessages getMessages() throws IOException {
3941
return this.asapEngine.getChunkStorage().getASAPChunkCache(this.getUri(), this.asapEngine.getEra());
4042
}
4143

src/net/sharksystem/asap/ASAPChunkStorage.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.sharksystem.asap;
22

3+
import net.sharksystem.asap.apps.ASAPMessages;
4+
35
import java.io.IOException;
46
import java.util.List;
57

@@ -30,5 +32,5 @@ public interface ASAPChunkStorage {
3032
* @return a chunk cache which hides details of era
3133
* @throws IOException
3234
*/
33-
public ASAPChannelMessages getASAPChunkCache(CharSequence uri, int toEra) throws IOException;
35+
public ASAPMessages getASAPChunkCache(CharSequence uri, int toEra) throws IOException;
3436
}

src/net/sharksystem/asap/ASAPChunkStorageFS.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.sharksystem.asap;
22

33
import net.sharksystem.Utils;
4+
import net.sharksystem.asap.apps.ASAPMessages;
45

56
import java.io.File;
67
import java.io.FilenameFilter;
@@ -114,14 +115,14 @@ public void dropChunks(int era) throws IOException {
114115
}
115116

116117
@Override
117-
public ASAPChannelMessages getASAPChunkCache(CharSequence uri, int toEra) throws IOException {
118+
public ASAPMessages getASAPChunkCache(CharSequence uri, int toEra) throws IOException {
118119
// go back 1000 eras
119120
int fromEra = toEra;
120121
for(int i = 0; i < 1000; i++) {
121122
fromEra = ASAPEngine.previousEra(fromEra);
122123
}
123124

124-
return new ASAPInMemoChannelMessages(this,
125+
return new ASAPInMemoMessages(this,
125126
uri,
126127
fromEra, // set starting era
127128
toEra // anything before

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.sharksystem.asap;
22

3+
import net.sharksystem.asap.apps.ASAPMessages;
34
import net.sharksystem.asap.management.ASAPManagementStorage;
45
import net.sharksystem.asap.management.ASAPManagementStorageImpl;
56
import net.sharksystem.asap.protocol.*;
@@ -225,19 +226,19 @@ public void removeChannel(CharSequence uri) throws IOException {
225226
chunk.drop();
226227
}
227228

228-
public ASAPChannelMessages getChunkChain(int position) throws IOException, ASAPException {
229+
public ASAPMessages getChunkChain(int position) throws IOException, ASAPException {
229230
return this.getChunkChain(position, this.era);
230231
}
231232

232-
public ASAPChannelMessages getChunkChain(CharSequence uri, int toEra) throws IOException {
233+
public ASAPMessages getChunkChain(CharSequence uri, int toEra) throws IOException {
233234
return this.chunkStorage.getASAPChunkCache(uri, toEra);
234235
}
235236

236-
public ASAPChannelMessages getChunkChain(CharSequence uri) throws IOException {
237+
public ASAPMessages getChunkChain(CharSequence uri) throws IOException {
237238
return this.getChunkChain(uri, this.getEra());
238239
}
239240

240-
public ASAPChannelMessages getChunkChain(int position, int toEra)
241+
public ASAPMessages getChunkChain(int position, int toEra)
241242
throws IOException, ASAPException {
242243

243244
List<CharSequence> channelURIs = this.getChannelURIs();

src/net/sharksystem/asap/ASAPInMemoChannelMessages.java renamed to src/net/sharksystem/asap/ASAPInMemoMessages.java

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.sharksystem.asap;
22

3+
import net.sharksystem.asap.apps.ASAPMessages;
4+
35
import java.io.IOException;
46
import java.util.ArrayList;
57
import java.util.Iterator;
@@ -10,7 +12,7 @@
1012
*
1113
* @author thsc
1214
*/
13-
class ASAPInMemoChannelMessages implements ASAPChannelMessages {
15+
class ASAPInMemoMessages implements ASAPMessages {
1416
public static final int DEFAULT_MAX_CACHE_SIZE = 1000;
1517
private final CharSequence uri;
1618
private final ASAPChunkStorageFS chunkStorage;
@@ -27,8 +29,8 @@ class ASAPInMemoChannelMessages implements ASAPChannelMessages {
2729

2830
private int numberOfMessages = 0;
2931

30-
public ASAPInMemoChannelMessages(ASAPChunkStorageFS chunkStorage,
31-
CharSequence uri, int fromEra, int toEra, int maxCacheLen) {
32+
public ASAPInMemoMessages(ASAPChunkStorageFS chunkStorage,
33+
CharSequence uri, int fromEra, int toEra, int maxCacheLen) {
3234

3335
this.uri = uri;
3436
this.chunkStorage = chunkStorage;
@@ -37,8 +39,8 @@ public ASAPInMemoChannelMessages(ASAPChunkStorageFS chunkStorage,
3739
this.maxCacheLen = maxCacheLen;
3840
}
3941

40-
public ASAPInMemoChannelMessages(ASAPChunkStorageFS chunkStorage,
41-
CharSequence uri, int fromEra, int toEra) {
42+
public ASAPInMemoMessages(ASAPChunkStorageFS chunkStorage,
43+
CharSequence uri, int fromEra, int toEra) {
4244

4345
this(chunkStorage, uri, fromEra, toEra, DEFAULT_MAX_CACHE_SIZE);
4446
}
@@ -86,9 +88,8 @@ private void syncChunkList() throws IOException {
8688
}
8789
} while (anotherLoop);
8890
}
89-
90-
@Override
91-
public int getNumberMessage() throws IOException {
91+
92+
public int size() throws IOException {
9293
this.initialize();
9394
return this.numberOfMessages;
9495
}
@@ -99,12 +100,24 @@ public CharSequence getURI() {
99100
}
100101

101102
@Override
102-
public Iterator<CharSequence> getMessages() throws IOException {
103+
public CharSequence getFormat() {
104+
return null;
105+
}
106+
107+
@Override
108+
public Iterator<CharSequence> getMessagesAsCharSequence() throws IOException {
103109
this.initialize();
104110

105111
return new ChunkListMessageIterator(this.chunkList);
106112
}
107113

114+
@Override
115+
public Iterator<byte[]> getMessages() throws IOException {
116+
this.initialize();
117+
118+
return new ChunkListByteMessageIterator(this.chunkList);
119+
}
120+
108121
@Override
109122
public CharSequence getMessage(int position, boolean chronologically)
110123
throws ASAPException, IOException {
@@ -219,15 +232,14 @@ public void sync() throws IOException {
219232
// helper: message iterator implementation //
220233
//////////////////////////////////////////////////////////////////////////////////////////
221234

222-
private class ChunkListMessageIterator implements Iterator<CharSequence> {
223-
235+
private abstract class ChunkListIterator<T> {
224236
private final List<ASAPChunk> chunkList;
225237
private ASAPChunk currentChunk;
226238
private int nextIndex;
227-
private Iterator<CharSequence> currentIterator;
228-
private CharSequence messageAhead;
239+
private Iterator<T> currentIterator;
240+
private T messageAhead;
229241

230-
public ChunkListMessageIterator(List<ASAPChunk> chunkList) throws IOException {
242+
public ChunkListIterator(List<ASAPChunk> chunkList) throws IOException {
231243
this.chunkList = chunkList;
232244
this.currentChunk = null;
233245
this.nextIndex = 0;
@@ -254,38 +266,62 @@ private void readAhead() {
254266
// open next chunk / iterator
255267
this.currentChunk = this.chunkList.get(this.nextIndex++);
256268
try {
257-
this.currentIterator = this.currentChunk.getMessages();
269+
this.currentIterator = this.getMessageIterator(currentChunk);
258270
this.readAhead(); // next try
259271
} catch (IOException e) {
260272
// cannot recover from that problem
261273
return;
262274
}
263275
}
264276

265-
@Override
277+
266278
public boolean hasNext() {
267279
return this.messageAhead != null;
268280
}
269281

270-
@Override
271-
public CharSequence next() {
282+
public T next() {
272283
if(this.messageAhead == null) {
273284
throw new NoSuchElementException("list empty or already reached end");
274285
}
275286

276287
// remove that single message cache
277-
CharSequence retMessage = this.messageAhead;
288+
T retMessage = this.messageAhead;
278289
this.messageAhead = null;
279290

280291
// read ahead - if possible
281292
this.readAhead();
282293

283294
return retMessage;
284295
}
296+
297+
abstract Iterator<T> getMessageIterator(ASAPChunk chunk) throws IOException;
285298
}
286299

287-
public int size() throws IOException {
288-
this.initialize();
289-
return this.numberOfMessages;
300+
private class ChunkListMessageIterator extends ChunkListIterator<CharSequence> implements Iterator<CharSequence> {
301+
private Iterator<CharSequence> currentIterator;
302+
private CharSequence messageAhead;
303+
304+
public ChunkListMessageIterator(List<ASAPChunk> chunkList) throws IOException {
305+
super(chunkList);
306+
}
307+
308+
@Override
309+
Iterator<CharSequence> getMessageIterator(ASAPChunk chunk) throws IOException {
310+
return chunk.getMessages();
311+
}
312+
}
313+
314+
private class ChunkListByteMessageIterator extends ChunkListIterator<byte[]> implements Iterator<byte[]> {
315+
private Iterator<CharSequence> currentIterator;
316+
private CharSequence messageAhead;
317+
318+
public ChunkListByteMessageIterator(List<ASAPChunk> chunkList) throws IOException {
319+
super(chunkList);
320+
}
321+
322+
@Override
323+
Iterator<byte[]> getMessageIterator(ASAPChunk chunk) throws IOException {
324+
return chunk.getMessagesAsBytes();
325+
}
290326
}
291327
}

src/net/sharksystem/asap/ASAPStorage.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.sharksystem.asap;
22

3+
import net.sharksystem.asap.apps.ASAPMessages;
34
import net.sharksystem.asap.management.ASAPManagementStorage;
45

56
import java.io.IOException;
@@ -244,13 +245,13 @@ public interface ASAPStorage {
244245
*/
245246
ASAPChunkStorage getChunkStorage();
246247

247-
ASAPChannelMessages getChunkChain(int position) throws IOException, ASAPException;
248+
ASAPMessages getChunkChain(int position) throws IOException, ASAPException;
248249

249-
ASAPChannelMessages getChunkChain(int position, int toEra) throws IOException, ASAPException;
250+
ASAPMessages getChunkChain(int position, int toEra) throws IOException, ASAPException;
250251

251-
ASAPChannelMessages getChunkChain(CharSequence uri, int toEra) throws IOException;
252+
ASAPMessages getChunkChain(CharSequence uri, int toEra) throws IOException;
252253

253-
ASAPChannelMessages getChunkChain(CharSequence uri) throws IOException;
254+
ASAPMessages getChunkChain(CharSequence uri) throws IOException;
254255

255256
/**
256257
* Refresh with external system - re-read files, or whatever.
Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package net.sharksystem.asap;
1+
package net.sharksystem.asap.apps;
2+
3+
import net.sharksystem.asap.ASAPException;
24

35
import java.io.IOException;
46
import java.util.Iterator;
@@ -19,27 +21,38 @@
1921
*
2022
* @author thsc
2123
*/
22-
public interface ASAPChannelMessages {
24+
public interface ASAPMessages {
2325
/**
24-
*
25-
* @return number of messages fitting to that uri regardless of era
26+
* @return number of messages
2627
*/
27-
int getNumberMessage() throws IOException;
2828
int size() throws IOException;
2929

3030
/**
3131
* @return channel uri
3232
*/
3333
CharSequence getURI();
34-
34+
35+
/**
36+
* @return format which - in most cases - matches with an application name
37+
*/
38+
CharSequence getFormat();
39+
3540
/**
36-
*
41+
*
3742
* first, false: newest message comes first
3843
* @return iterator of all messages in that chunk cache
39-
* @throws IOException
44+
* @throws IOException
4045
*/
41-
Iterator<CharSequence> getMessages() throws IOException;
42-
46+
Iterator<CharSequence> getMessagesAsCharSequence() throws IOException;
47+
48+
/**
49+
*
50+
* first, false: newest message comes first
51+
* @return iterator of all messages in that chunk cache
52+
* @throws IOException
53+
*/
54+
Iterator<byte[]> getMessages() throws IOException;
55+
4356
/**
4457
* Returns a message with a given position
4558
* @param position
@@ -50,17 +63,8 @@ public interface ASAPChannelMessages {
5063
* not exist
5164
* @throws IOException couldn't read from storage
5265
*/
53-
CharSequence getMessage(int position, boolean chronologically)
66+
CharSequence getMessage(int position, boolean chronologically)
5467
throws ASAPException, IOException;
55-
56-
/**
57-
* Synchronizes cache. It is up to real implemenations what really
58-
* happens. That method is meant to be called whenever the underlying
59-
* chunk storage has been changed. After that call, cache and storage are
60-
* in sync agin. Implemenations whoch don't have an internal cache don't
61-
* have to do anything.
62-
*
63-
* @throws IOException
64-
*/
65-
void sync() throws IOException;
68+
69+
6670
}

src/net/sharksystem/cmdline/CmdLineUI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ private void printChannelInfo(ASAPStorage asapStorage, CharSequence uri, CharSeq
595595
Set<CharSequence> recipients = channel.getRecipients();
596596

597597
System.out.println("Owner:App:Channel == " + channel.getOwner() + ":" + appName + ":" + channel.getUri());
598-
System.out.println("#Messages == " + channel.getMessages().getNumberMessage());
598+
System.out.println("#Messages == " + channel.getMessages().size());
599599
System.out.println("#Recipients == " + recipients.size());
600600
for(CharSequence recipient : recipients) {
601601
System.out.println(recipient);

test/net/sharksystem/asap/ChunkCacheTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.sharksystem.asap;
22

3+
import net.sharksystem.asap.apps.ASAPMessages;
34
import org.junit.Assert;
45
import org.junit.Test;
56

@@ -50,7 +51,7 @@ public void chunkTest0() throws IOException, ASAPException {
5051
aliceStorage.add(TEST_URI, message[0xF]);
5152

5253
// now get message chain
53-
ASAPChannelMessages chunkChain = aliceStorage.getChunkChain(TEST_URI);
54+
ASAPMessages chunkChain = aliceStorage.getChunkChain(TEST_URI);
5455
for(int i = 0; i < 0x10; i++) {
5556
Assert.assertTrue(chunkChain.getMessage(i, true).toString()
5657
.equalsIgnoreCase(message[i]));

0 commit comments

Comments
 (0)