/*
   Copyright 2014-now by Alain Stalder. Made in Switzerland.

   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

       https://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 ch.grengine.source;

import java.io.File;

import static java.util.Objects.requireNonNull;


/**
 * File-based script source, default implementation of the {@link FileSource} interface.
 * <p>
 * The source ID is {@literal "ScriptFile_<canonical-or-absolute-file-path>"}.
 * 
 * @since 1.0
 * 
 * @author Alain Stalder
 * @author Made in Switzerland.
 */
public class DefaultFileSource extends BaseSource implements FileSource {
    
    private final File file;
    
    /**
     * constructor from script file.
     * <p>
     * The file is always converted to the canonical file, with fallback to the absolute file.
     * <p>
     * The ID is the canonical file path, with fallback to the absolute file path.
     *
     * @param file script file
     * 
     * @throws NullPointerException if file is null
     * 
     * @since 1.0
     */
    public DefaultFileSource(final File file) {
        requireNonNull(file, "File is null.");
        this.file = SourceUtil.toCanonicalOrAbsoluteFile(file);
        id = this.file.getPath();
    }
    
    @Override
    public long getLastModified() {
        return getFile().lastModified();
    }
    
    @Override
    public File getFile() {
        return file;
    }

}
