Project

General

Profile

Download (1.83 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.stream;
10

    
11
import java.util.Queue;
12
import java.util.concurrent.LinkedBlockingQueue;
13

    
14
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
15

    
16
import eu.etaxonomy.cdm.io.stream.terms.TermUri;
17

    
18

    
19
/**
20
 * @author a.mueller
21
 *
22
 */
23
public class LookAheadStream<ITEM> implements INamespaceReader<ITEM>{
24
	@SuppressWarnings("unused")
25
	private static final Logger logger = LogManager.getLogger(LookAheadStream.class);
26

    
27
	private final Queue<ITEM> fifo = new LinkedBlockingQueue<>();
28

    
29
	private final INamespaceReader<ITEM> stream;
30

    
31
	public LookAheadStream(INamespaceReader<ITEM> stream) {
32
		super();
33
		this.stream = stream;
34
		if (stream == null){
35
			throw new RuntimeException("Stream may not be null.");
36
		}
37
	}
38

    
39
	@Override
40
    public ITEM read(){
41
		if (! fifo.isEmpty()){
42
			return fifo.remove();
43
		}else{
44
			return stream.read();
45
		}
46
	}
47

    
48
	public ITEM readLookAhead(int max){
49
	    if (max > fifo.size()){
50
	        ITEM result = stream.read();
51
	        fifo.add(result);
52
	        return result;
53
	    }else{
54
	        return null;
55
	    }
56
	}
57

    
58
	public ITEM readLookAhead(){
59
		ITEM result = stream.read();
60
	    fifo.add(result);
61
	    return result;
62
	}
63

    
64
	public boolean hasNextLookAhead(int max){
65
		if (fifo.size() < max && stream.hasNext()){
66
			return true;
67
		}else{
68
			return false;
69
		}
70
	}
71

    
72
	public int sizeLookAhead(){
73
		return fifo.size();
74
	}
75

    
76
	public boolean isLookingAhead(){
77
		return ! fifo.isEmpty();
78
	}
79

    
80
	@Override
81
	public boolean hasNext() {
82
		return ! fifo.isEmpty() || stream.hasNext();
83
	}
84

    
85
	@Override
86
	public TermUri getTerm() {
87
		return stream.getTerm();
88
	}
89

    
90
}
(14-14/21)