# ExplicitFileList.java

    /*
    ** Module   : ExplicitFileList.java
    ** Abstract : creates a file list based on a user-defined explicit list
    **
    ** Copyright (c) 2005-2023, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- JPRM-- ----------------------------------Description-------------------------------------
    ** 001 GES 20050125 @22975 Created initial version supporting a  user-defined list of filenames
    **                         (absolute or relative) to convert into an array of File objects.
    ** 002 GES 20060129 @24129 Reimplemented with a new base class.
    ** 003 ECF 20190218        Implemented generics.
    ** 004 CA  20190703        Allow the files to be sorted or not (useful when legacy classes need to
    **                         be processed in a predetermined order).
    ** 005 CA  20200116        Javadoc fixes.
    ** 006 CA  20220412        Added file-set support (either -Z command-line option or p2j.cfg.xml configuration).
    ** 007 GBB 20230512        Logging methods replaced by CentralLogger/ConversionStatus.
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program 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 Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.util;

    import com.goldencode.p2j.util.logging.*;

    import java.io.*;
    import java.util.*;

    /**
     * Creates a sorted list of files based on a user-defined list of filenames
     * (absolute or relative).  The list is sorted lexicographically.
     *
     * @author    GES
     */
    public class ExplicitFileList
    extends FileList
    {
       /** Logger */
       private static final CentralLogger LOG = CentralLogger.get(ExplicitFileList.class);

       /** The list of explicit filenames. */
       private LinkedHashSet<String> filenames = null;

       /** Flag indicating if the file list needs to be sorted or not. */
       private boolean sort = true;

       /**
        * Constructor which sets the case-sensitivity of the legacy operating system.
        *
        * @param    caseSens
        *           Sets the case-sensitivity of the sorting algorithm and legacy operating system.
        */
       public ExplicitFileList(boolean caseSens)
       {
          super(caseSens);
          filenames = new LinkedHashSet<>();
       }

       /**
        * Base constructor to handle all possible cases.
        *
        * @param    filenames
        *           The array of absolute or relative file names.
        * @param    caseSens
        *           Sets the case-sensitivity of the sorting algorithm and legacy operating system.
        */
       public ExplicitFileList(String[] filenames, boolean caseSens)
       {
          super(caseSens);
          this.filenames = new LinkedHashSet<>(Arrays.asList(filenames));
       }

       /**
        * Common constructor which uses a case-insensitive sorting algorithm.
        *
        * @param    filenames
        *           The array of absolute or relative file names.
        */
       public ExplicitFileList(String[] filenames)
       {
          super(false);
          this.filenames = new LinkedHashSet<>(Arrays.asList(filenames));
       }

       /**
        * Create a copy of the specified file list.
        *
        * @param    ref
        *           The reference instance.
        */
       public ExplicitFileList(ExplicitFileList ref)
       {
          super(ref.isCaseSensitive());

          this.filenames = ref.filenames == null ? null : new LinkedHashSet<>(ref.filenames);
          this.sort = ref.sort;
       }

       /**
        * Set the {@link #sort} flag.
        *
        * @param    sort
        *           Flag indicating if the file list needs to be sorted.
        */
       public void setSort(boolean sort)
       {
          this.sort = sort;
       }

       /**
        * Add the specified files to the {@link #filenames file list}.
        *
        * @param    files
        *           The files to add.
        */
       public void addAll(String[] files)
       {
          filenames.addAll(Arrays.asList(files));
       }

       /**
        * Add a single file to the {@link #filenames file list}.
        *
        * @param    file
        *           The file to add.
        */
       public void add(String file)
       {
          filenames.add(file);
       }

       /**
        * Remove the specified files from the {@link #filenames file list}.
        *
        * @param    files
        *           The files to remove.
        */
       public void removeAll(String[] files)
       {
          filenames.removeAll(Arrays.asList(files));
       }

       /**
        * Core implementation of the listing algorithm which uses the
        * <code>File</code> class to create and return the list of files based
        * on user input specified during construction.  No directories will be
        * returned in the list and only files that actually exist will be
        * returned. The resulting list will be sorted lexicographically.
        *
        * @param    results
        *           The list to which all matching files must be appended.
        */
       @Override
       protected void listImpl(List<File> results)
       {
          // separate file and directory results
          for (String filename : filenames)
          {
             File next = new File(filename);

             if (next.exists() && next.isFile())
             {
                // add the file to the results list
                results.add(next);
             }
          }
       }

       /**
        * Sort the files using a comparator which is aware of the {@link #caseSens case sensitivity}.
        * <p>
        * This will be done only if {@link #sort} is <code>true</code>
        * @param     files
        *            The file list to sort.
        */
       @Override
       protected void sortFiles(List<File> files)
       {
          if (sort)
          {
             super.sortFiles(files);
          }
       }

       /**
        * Provides a command line interface for an end user to drive and/or test
        * the ExplicitFileList class.  The list of resulting filenames is
        * displayed to the user on <code>stdout</code>.
        * <p>
        * Syntax:
        * <pre>
        *    java ExplcitFileList &lt;filename&gt; ...
        * </pre>
        * Where:
        * <ul>
        *    <li> &lt;filename&gt; is one or more relative or absolute file names
        * </ul>
        *
        * @param   args
        *          List of command line arguments.
        */
       public static void main(String[] args)
       {
          String syntax = "Syntax: java ExplicitFileList <filename> ... ";

          if (args.length < 1)
          {
             LOG.severe(syntax);
             System.exit(-1);
          }

          try
          {
             FileList flist  = new ExplicitFileList(args);
             String   list[] = flist.listFilenames();

             for ( int i = 0; i < list.length; i++ )
             {
                System.out.println(list[i]);
             }
          }

          catch (Exception e)
          {
             LOG.severe("", e);
          }
       }
    }