Project

General

Profile

Download (1.92 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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.Set;
12

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

    
15
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
16
import eu.etaxonomy.cdm.io.stream.terms.TermUri;
17

    
18
/**
19
 * FilteredReader filters a stream. Only a sub-stream of the input items
20
 * is in the output stream.
21
 * @author a.mueller
22
 * @since 23.11.2011
23
 *
24
 */
25
public class FilteredStream implements IItemStream {
26
	@SuppressWarnings("unused")
27
	private static Logger logger = LogManager.getLogger(FilteredStream.class);
28

    
29
	private final IItemStream reader;
30
	private final ItemFilter<StreamItem> filter;
31
	private StreamItem next;
32

    
33
	/**
34
	 * @param list
35
	 */
36
	public FilteredStream(IItemStream reader, ItemFilter<StreamItem> filter) {
37
		this.reader = reader;
38
		this.filter = filter;
39
	}
40

    
41
	@Override
42
	public StreamItem read() {
43
	    StreamItem result;
44
	    if (hasNext()){
45
		    result = next;
46
		    next = null;
47
		}else{
48
		    result = null;
49
		}
50
	    return result;
51
	}
52

    
53
	@Override
54
	public boolean hasNext(){
55
	    if (next != null){
56
	        return true;
57
	    }
58
		while (reader.hasNext()){
59
		    next = reader.read();
60
		    if (filter == null || ! filter.toBeRemovedFromStream(next)){
61
		        return true;
62
		    }
63
		}
64
		next = null;
65
	    return false;
66
	}
67

    
68
    @Override
69
    public TermUri getTerm() {
70
        return reader.getTerm();
71
    }
72

    
73
    @Override
74
    public String getItemLocation() {
75
        return reader.getItemLocation();
76
    }
77

    
78
    @Override
79
    public String getStreamLocation() {
80
        return reader.getStreamLocation();
81
    }
82

    
83
    @Override
84
    public void addObservers(Set<IIoObserver> observers) {
85
        reader.addObservers(observers);
86
    }
87

    
88
}
(3-3/21)