modelinspector.collectors.RequireAllCollector.java Source code

Java tutorial

Introduction

Here is the source code for modelinspector.collectors.RequireAllCollector.java

Source

/*
 * Copyright 2016
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universitt Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package modelinspector.collectors;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;

public class RequireAllCollector extends CollectorBase<Boolean> {
    private String name;
    private Set<String> required;
    private Locale language;
    private boolean caseSensitive;

    public RequireAllCollector(String aName, String aLanguage, boolean aCaseSensitive, String... aTokens) {
        name = aName;
        required = new HashSet<String>();
        language = new Locale(aLanguage);
        caseSensitive = aCaseSensitive;

        for (String t : aTokens) {
            required.add(caseSensitive ? t : t.toLowerCase(language));
        }
    }

    public RequireAllCollector(String aName, String aLanguage, File aFile, String aEncoding,
            boolean aCaseSensitive) {
        name = aName;
        required = new HashSet<>();
        language = new Locale(aLanguage);
        caseSensitive = aCaseSensitive;

        try (InputStream is = new FileInputStream(aFile)) {
            LineIterator i = IOUtils.lineIterator(is, aEncoding);
            while (i.hasNext()) {
                String[] fields = i.nextLine().split("\t");
                String word = caseSensitive ? fields[0] : fields[0].toLowerCase();
                required.add(word);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void collect(String aValue) {
        String value = caseSensitive ? aValue : aValue.toLowerCase(language);
        required.remove(value);
    }

    @Override
    public Boolean getResult() {
        return required.isEmpty();
    }
}