Package com.puppycrawl.tadpole

Source Code of com.puppycrawl.tadpole.History

package com.puppycrawl.tadpole;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;

/**
* It is the callers responsiblity to handle threading.
* @author BurnOl
*/
public class History {
    private final File mBacking;
    private final Set<String> mSeenBefore = new HashSet<String>();

    public History(File backingFile) throws IOException {
        mBacking = backingFile;

        if (!mBacking.exists()) {
            mBacking.createNewFile();
        }
        else {
            final Reader r = new FileReader(mBacking);
            try {
                final LineIterator iter = IOUtils.lineIterator(r);
                while (iter.hasNext()) {
                    mSeenBefore.add(iter.nextLine().trim());
                }
            }
            finally {
                IOUtils.closeQuietly(r);
            }
        }
    }

    void record(URL url) {
        if (unseen(url)) {
            final String trimmed = url.toExternalForm().trim();
            PrintWriter pw;
            try {
                pw = new PrintWriter(new FileWriter(mBacking, true));
            }
            catch (final IOException e) {
                System.out.println("Unable to record: " + trimmed);
                return;
            }
            pw.println(trimmed);
            pw.close();

            // finally write out that seen the URL
            mSeenBefore.add(trimmed);
        }
    }

    boolean unseen(URL url) {
        return !mSeenBefore.contains(url.toExternalForm().trim());
    }
}
TOP

Related Classes of com.puppycrawl.tadpole.History

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.