001    package calhoun.analysis.crf.io;
002    
003    import java.io.BufferedReader;
004    import java.io.IOException;
005    import java.io.Writer;
006    import java.util.Map;
007    
008    import calhoun.util.Assert;
009    
010    /** reads in an input consisting of a string of 1's and 0's that correspond to binary values.  Can be used as a standalone
011     * input component or part of an interleaved input.
012     */
013    public class BooleanInput extends InterleavedInputComponentBase {
014            private static final long serialVersionUID = 4922000330136279956L;
015            
016            public boolean read(BufferedReader r, Map<String, InputSequence<?>> output) throws IOException {
017                    String str = r.readLine();
018                    if(str == null) {
019                            return false;
020                    }
021                    boolean[] data = new boolean[str.length()];
022                    for (int i = 0; i < str.length(); ++i) {
023                            char c = str.charAt(i);
024                            if(c == '1')
025                                    data[i] = true;
026                            else {
027                                    Assert.a(c=='0');
028                            }
029                    }
030                    output.put(name, new InputSequenceBoolean(data));
031                    return true;
032            }
033            
034            public void write(Writer w, Map<String, ? extends InputSequence<?>> data) throws IOException {
035                    boolean[] boolData = ((InputSequenceBoolean) data.get(name)).getData();
036                    for(boolean i: boolData) {
037                            w.write(i ? '1':'0');
038                    }
039                    w.write('\n');
040            }
041    }