/*
 * MIT License
 *
 * Copyright (c) 2002-2023 Mikko Tommila
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.apfloat.spi;

import org.apfloat.ApfloatContext;

/**
 * Class for generating filenames for temporary files.
 *
 * @version 1.0
 * @author Mikko Tommila
 */

public class FilenameGenerator
{
    /**
     * Create a new filename generator. The generated filenames will
     * point to the specified directory path. The base file name is a
     * sequential number. The specified suffix is appended to the
     * final file name.
     *
     * @param path The path where the file are created. If <code>null</code>, the default file path from the {@link ApfloatContext} is used.
     * @param initialValue The initial value for the numbers. If <code>null</code>, the default file initial value from the {@link ApfloatContext} is used.
     * @param suffix The suffix for file names. If <code>null</code>, the default file suffix from the {@link ApfloatContext} is used.
     *
     * @exception NumberFormatException If initialValue is not a valid integer number.
     */

    public FilenameGenerator(String path, String initialValue, String suffix)
        throws NumberFormatException
    {
        if (path == null)
        {
            ApfloatContext ctx = ApfloatContext.getContext();
            path = ctx.getProperty(ApfloatContext.FILE_PATH);
        }

        if (initialValue == null)
        {
            ApfloatContext ctx = ApfloatContext.getContext();
            initialValue = ctx.getProperty(ApfloatContext.FILE_INITIAL_VALUE);
        }

        if (suffix == null)
        {
            ApfloatContext ctx = ApfloatContext.getContext();
            suffix = ctx.getProperty(ApfloatContext.FILE_SUFFIX);
        }

        this.path = path;
        this.value = Long.parseLong(initialValue);
        this.suffix = suffix;
    }

    /**
     * Generate a filename. The returned filename is unique among
     * those generated by this filename generator.
     *
     * @return A generated file name.
     */

    public synchronized String generateFilename()
    {
        return this.path + (this.value++) + this.suffix;
    }

    /**
     * Returns the path setting of this filename generator.
     *
     * @return The path setting of this filename generator.
     */

    public String getPath()
    {
        return this.path;
    }

    /**
     * Returns the current initial value of the base file names generated.
     *
     * @return The current initial value of the base file names generated.
     */

    public synchronized String getInitialValue()
    {
        return String.valueOf(this.value);
    }

    /**
     * Returns the suffix setting of this filename generator.
     *
     * @return The suffix setting of this filename generator.
     */

    public String getSuffix()
    {
        return this.suffix;
    }

    private String path;
    private long value;
    private String suffix;
}
