org.kawanfw.sql.jdbc.util.FileBackedList.java Source code

Java tutorial

Introduction

Here is the source code for org.kawanfw.sql.jdbc.util.FileBackedList.java

Source

/*
 * This file is part of AceQL. 
 * AceQL: Remote JDBC access over HTTP.                                     
 * Copyright (C) 2015,  KawanSoft SAS
 * (http://www.kawansoft.com). All rights reserved.                                
 *                                                                               
 * AceQL is free software; you can redistribute it and/or                 
 * modify it under the terms of the GNU Lesser General Public                    
 * License as published by the Free Software Foundation; either                  
 * version 2.1 of the License, or (at your option) any later version.            
 *                                                                               
 * AceQL is distributed in the hope that it will be useful,               
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             
 * Lesser General Public License for more details.                               
 *                                                                               
 * You should have received a copy of the GNU Lesser General Public              
 * License along with this library; if not, write to the Free Software           
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */
package org.kawanfw.sql.jdbc.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.sql.SQLException;
import java.util.AbstractList;
import java.util.logging.Level;

import org.apache.commons.io.IOUtils;
import org.kawanfw.commons.util.ClientLogger;
import org.kawanfw.commons.util.FrameworkDebug;
import org.kawanfw.commons.util.Tag;

/**
 * @author Nicolas de Pomereu
 * 
 *         a concrete List of objects backed on a file
 * 
 * @param <E>
 *            the type of object to bakc in a List
 */
public class FileBackedList<E> extends AbstractList<E> {

    /** Debug flag */
    private static boolean DEBUG = FrameworkDebug.isSet(FileBackedList.class);

    /** The file that backups the List */
    private LineNumberReader lineNumberReader = null;

    /** The file that contains the list */
    private File file = null;

    /** The sise of the list */
    private int size = -1;

    /** The current line to read */
    private String currentLine = "";

    /**
     * Constructor
     * 
     * @param fileIn
     *            the file to read as a list of lines
     */
    public FileBackedList(File fileIn) throws SQLException {

        if (fileIn == null) {
            throw new IllegalArgumentException("fileIn can not be null!");
        }

        if (!fileIn.exists()) {
            String message = Tag.PRODUCT_PRODUCT_FAIL + "Internal File does not exists: " + file;
            throw new SQLException(message, new IOException(message));
        }

        try {
            this.file = fileIn;
            lineNumberReader = new LineNumberReader(new FileReader(file));

            // Call size to initialize size field:
            this.size();

        } catch (FileNotFoundException e) {
            String message = Tag.PRODUCT_PRODUCT_FAIL + "Internal File does not exists: " + file;
            throw new SQLException(message, new IOException(message));
        }

    }

    @Override
    public E set(int index, E element) {
        throw new UnsupportedOperationException(Tag.PRODUCT + "Method not implemented.");
    }

    @SuppressWarnings("unchecked")
    @Override
    public E get(int index) {

        if (lineNumberReader == null) {
            throw new IllegalStateException("FileBaskList has been cleared. Can not be used anymore.");
        }

        try {
            int currPos = lineNumberReader.getLineNumber();

            // if the index + 1 < currPos ==> We can go to the line specified

            if (index >= size) {
                throw new IndexOutOfBoundsException("Passed end of ResultSet: " + index);
            }

            index += 1;

            if (currPos - 1 > index) {
                debug("RESET");
                IOUtils.closeQuietly(lineNumberReader);
                lineNumberReader = new LineNumberReader(new FileReader(file));
                currPos = lineNumberReader.getLineNumber();
            }

            if (index == currPos - 1) {
                debug("the line: " + currentLine);
                return (E) currentLine;
            }

            while (currPos - 1 < index) {
                debug("looping...");
                currentLine = lineNumberReader.readLine();
                currPos++;
            }

            // debug("the line: " + currentLine);
            return (E) currentLine;

        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }

    }

    @Override
    public boolean isEmpty() {
        if (lineNumberReader == null) {
            throw new IllegalStateException("FileBaskList has been cleared. Can not be used anymore.");
        }
        return size() == 0;
    }

    @Override
    public int size() {

        if (lineNumberReader == null) {
            throw new IllegalStateException("FileBaskList has been cleared. Can not be used anymore.");
        }

        // Return the size only if it has not been computed
        if (size != -1) {
            return size;
        }

        IOUtils.closeQuietly(lineNumberReader);

        LineNumberReader localLineNumberReader = null;
        size = 0;

        try {
            localLineNumberReader = new LineNumberReader(new FileReader(file));

            @SuppressWarnings("unused")
            String line = "";
            while ((line = localLineNumberReader.readLine()) != null) {
                size++;
            }

            size = size - 1;
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        } finally {
            IOUtils.closeQuietly(localLineNumberReader);
        }

        try {
            lineNumberReader = new LineNumberReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException(e);
        }

        return size;

    }

    /**
     * Removes all of the elements from this list (optional operation). The list
     * will be empty after this call returns.
     * 
     * @throws UnsupportedOperationException
     *             if the <tt>clear</tt> operation is not supported by this list
     */
    @Override
    public void clear() {
        debug("clear called");
        IOUtils.closeQuietly(lineNumberReader);

        lineNumberReader = null;

        debug("file: " + file);
        boolean deleted = file.delete(); // To delete the tempfile

        if (!deleted) {
            debug("this.file not deleted: " + this.file);
        }
    }

    /**
     * Displays the given message if DEBUG is set.
     * 
     * @param s
     *            the debug message
     */

    private void debug(String s) {
        if (DEBUG) {
            ClientLogger.getLogger().log(Level.WARNING, s);
        }
    }

}