001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.scan;
023
024import java.io.File;
025import java.io.FileInputStream;
026import java.io.IOException;
027import java.io.InputStream;
028
029/**
030 * @author Franck WOLFF
031 */
032public class FileScannedItem extends AbstractScannedItem {
033
034    private final File root;
035    private final File file;
036
037    private String relativePath = null;
038
039    public FileScannedItem(Scanner scanner, FileScannedItem marker, File root, File file) {
040        super(scanner, marker);
041
042        this.root = root;
043        this.file = file;
044    }
045
046    public File getRoot() {
047        return root;
048    }
049
050    public File getFile() {
051        return file;
052    }
053
054    public long getSize() {
055        return file.length();
056    }
057
058    public InputStream getInputStream() throws IOException {
059        return new FileInputStream(file);
060    }
061
062    public String getName() {
063        return file.getName();
064    }
065
066    public String getRelativePath() {
067        if (relativePath == null) {
068            StringBuffer sb = new StringBuffer();
069            for (File f = file; f != null && !root.equals(f); f = f.getParentFile()) {
070                if (sb.length() > 0)
071                    sb.insert(0, '/');
072                sb.insert(0, f.getName());
073            }
074            relativePath = sb.toString();
075        }
076        return relativePath;
077    }
078
079    public String getAbsolutePath() {
080        return file.getAbsolutePath();
081    }
082
083    @Override
084    public boolean equals(Object obj) {
085        if (obj == this)
086            return true;
087        if (!(obj instanceof FileScannedItem))
088            return false;
089        return file.equals(((FileScannedItem)obj).file) && root.equals(((FileScannedItem)obj).root);
090    }
091
092    @Override
093    public int hashCode() {
094        return root.hashCode() + (31 * file.hashCode());
095    }
096}