View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2010 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // ObjectStreamReader.java
20  // Since: 2010/12/12
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   * Text Format -> Object -> Iterator
38   * 
39   * @author leo
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  		 * Fill the destination queue
81  		 * 
82  		 * @param dest
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  			// TODO Auto-generated method stub
97  
98  		}
99  
100 		public <T, U> void connect(T parent, String name, U obj) throws Exception {
101 			// TODO Auto-generated method stub
102 
103 		}
104 
105 		public <T, U> void append(T parent, String name, U obj) throws Exception {
106 			// TODO Auto-generated method stub
107 
108 		}
109 	}
110 
111 	public boolean hasNext() throws IOException {
112 		if (!objectQueue.isEmpty())
113 			return true;
114 
115 		// Fill the queue with the objects retrieved from the text reader thread
116 		// TODO
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 }