All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.arp.javautil.test.ExpectedSetOfStringsReader Maven / Gradle / Ivy

Go to download

JavaUtil is a utility library to speed the development of Java software. We developed it over multiple years during our internal development efforts, and it has reached a point of stability where we have decided to make it available to the outside world. The JavaUtil package aims to fill in the gaps of the Apache Commons and similar utility libraries out there. Features include convenience classes for string, collections, arrays, dates, iterators, colors, logging, unit testing, a little bit of basic statistics, database queries, caching and more.

The newest version!
package org.arp.javautil.test;

/*
 * #%L
 * JavaUtil
 * %%
 * Copyright (C) 2012 - 2015 Emory University
 * %%
 * 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.
 * #L%
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

/**
 * Reads a file of strings, one per line, into a {@link Set}. Optionally, it 
 * allows specifying a comment character. On lines that contain the specified
 * character, the character and the remainder of the line are ignored.
 * 
 * @author Andrew Post
 */
public final class ExpectedSetOfStringsReader {

    /**
     * Reads the file with the specifies resource name, calling 
     * {@link Class#getResourceAsStream(java.lang.String) } to access the file.
     * 
     * @param resource the name of the file resource. Cannot be 
     * null.
     * @param cls the class to use. If null, the instance of this
     * class will be used.
     * @return a {@link Set} of strings, guaranteed not null.
     * 
     * @throws IOException if an error occurs reading the file.
     */
    public Set readAsSet(String resource, Class cls) throws IOException {
        return readAsSet(resource, cls, null);
    }
    
    /**
     * Reads the file with the specifies resource name, calling 
     * {@link Class#getResourceAsStream(java.lang.String) } to access the file.
     * 
     * @param resource the name of the file resource. Cannot be 
     * null.
     * @param cls the class to use. If null, the instance of this
     * class will be used.
     * @param commentCharacter the comment character to use. Set to
     * null if nothing should be interpreted as a comment.
     * @return a {@link Set} of strings, guaranteed not null.
     * 
     * @throws IOException if an error occurs reading the file.
     */
    public Set readAsSet(String resource, Class cls, Character commentCharacter) throws IOException {
        if (resource == null) {
            throw new IllegalArgumentException("resource cannot be null");
        }
        if (cls == null) {
            cls = getClass();
        }
        Set expected = new HashSet<>();
        InputStream is = cls.getResourceAsStream(resource);
        if (is == null) {
            throw new IOException("resource '" + resource + "' not found");
        }
        try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
            String line;
            while ((line = br.readLine()) != null) {
                int indexOfCommentChar;
                if (commentCharacter == null) {
                    indexOfCommentChar = -1;
                } else {
                    indexOfCommentChar = line.indexOf(commentCharacter);
                }
                String lineMinusComment;
                if (indexOfCommentChar > -1) {
                    lineMinusComment = line.substring(0, indexOfCommentChar);
                } else {
                    lineMinusComment = line;
                }
                String lineTrimmed = lineMinusComment.trim();
                if (lineTrimmed.length() > 0) {
                    expected.add(lineTrimmed);
                }
            }
        }
        return expected;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy