1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.utgenome.format;
24
25 import java.io.IOException;
26 import java.io.Reader;
27 import java.util.NoSuchElementException;
28 import java.util.concurrent.Callable;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31
32 import org.xerial.lens.ObjectStreamHandler;
33 import org.xerial.util.ArrayDeque;
34 import org.xerial.util.Deque;
35
36
37
38
39
40
41
42 public class ObjectStreamReader {
43
44 public static interface TextFormatToObjectMapper {
45 public void map(Reader input, ObjectStreamHandler out);
46 }
47
48 private final TextFormatToObjectMapper mapper;
49 private final Deque<Object> objectQueue = new ArrayDeque<Object>();
50 private final ExecutorService threadPool;
51 private final TextReader reader;
52
53 public ObjectStreamReader(Reader in, TextFormatToObjectMapper mapper) {
54 this.mapper = mapper;
55 this.threadPool = Executors.newFixedThreadPool(1);
56 this.reader = new TextReader(in);
57
58 }
59
60 public class TextReader implements Callable<Void>, ObjectStreamHandler {
61
62 private final Reader in;
63 private final Deque<Object> queue = new ArrayDeque<Object>();
64 private final int NUM_OBJECTS_TO_CACHE = 10000;
65
66 public TextReader(Reader in) {
67 this.in = in;
68 }
69
70 public void close() throws IOException {
71 in.close();
72 }
73
74 public Void call() throws Exception {
75 mapper.map(in, this);
76 return null;
77 }
78
79
80
81
82
83
84 public void fillQueue(Deque<Object> dest) {
85
86 }
87
88 public <T> void add(T obj) throws Exception {
89 queue.add(obj);
90 while (queue.size() > NUM_OBJECTS_TO_CACHE) {
91 wait(1000);
92 }
93 }
94
95 public <T> void add(String name, T obj) throws Exception {
96
97
98 }
99
100 public <T, U> void connect(T parent, String name, U obj) throws Exception {
101
102
103 }
104
105 public <T, U> void append(T parent, String name, U obj) throws Exception {
106
107
108 }
109 }
110
111 public boolean hasNext() throws IOException {
112 if (!objectQueue.isEmpty())
113 return true;
114
115
116
117 return false;
118 }
119
120 public Object next() throws IOException {
121 if (hasNext())
122 return objectQueue.pollFirst();
123
124 throw new NoSuchElementException();
125 }
126
127 }