/*::.
==================================================================================================================================
=================================================¦ Copyright © 2003 Allen Baker ¦=================================================
                                                 +------------------------------+
File:       ImageFileName.java
Originator: Allen Baker (2003.02.15)
LayoutRev:  5
================================================================================================================================== */



/*.
==========================================================================================
Package
------------------------------------------------------------------------------------------ */
package cosmicabyss.com.lib;



/*.
==========================================================================================
Imports
------------------------------------------------------------------------------------------ */
import java.io.*;
import java.util.*;



/*::
======================================================================================================================== *//**
Instances of this class are file names in this format:

   Yyyy.mm.dd.author.title.v1.v2.ext

The v1 and v2 can be missing. If any other segment is missing, the constructor will throw an exception.

<P>
   <DL>
      <DT>
         <B>
            Example usage:
         </B>
         <DD>
            <BLOCKQUOTE>
               <PRE id="unindent">
                  no example provided
               </PRE>
            </BLOCKQUOTE>
         </DD>
      </DT>
      <DT>
         <B>
            View Source:
         </B>
         <DD>
            <A href="ImageFileName.java.html">
               ImageFileName.java
            </A>
         </DD>
      </DT>
      <DT>
         <B>
            Author:
         </B>
         <DD>
            <A href="mailto:sourcecode.v01@cosmicabyss.com">
               Allen Baker
            </A>
         </DD>
      </DT>
   </DL>
*//*
======================================================================================================================== */
public class ImageFileName
   {



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method creates an instance of the ImageFileName class.

   <P><B>Implementation: </B><A HREF="ImageFileName.java.html#000">View source</A>

   @param
      pFileName is the name of the image file in this format: yyyy.mm.dd.author.title.v1.v2.ext The v1
      and v2 can be missing. If any other segment is missing, the constructor will throw an exception.
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public <Type> ImageFileName(Type pFileName) throws Exception
      {
      TextFile   file     = new TextFile(pFileName);
      XString    fileName = XString.toXString(file.getName());
      ArrayList  tokens   = fileName.tokenizedString(".");
      Iterator   iter     = tokens.iterator();
      int        i        = 0;
      /*.
      ==========================================================================================
      If the number of segments is wrong throw an exception.
      ------------------------------------------------------------------------------------------ */
      if ((tokens.size() < 6) || (tokens.size() > 8))
         {
         throw new Exception
            (
            "Invalid file name format.\n" +
            "Format must be in this format:\n" +
            "  yyyy.mm.dd.author.title.v1.v2.ext"
            );
         }
      /*.
      ==========================================================================================
      Iterate thru the segments
      ------------------------------------------------------------------------------------------ */
      while (iter.hasNext())
         {
         XString  tok = new XString((XString)iter.next());
         i++;
         /*.
         ==========================================================================================
         The last segment is the extension
         ------------------------------------------------------------------------------------------ */
         if (i==tokens.size())
            {
            mExt = tok;
            break;
            }
         /*.
         ==========================================================================================
         Otherwise, process the segment
         ------------------------------------------------------------------------------------------ */
         switch (i)
            {
            case 1:
               {
               mYyyy    = tok;
               mYyyyInt = (Integer.decode(mYyyy.trimLeft("0").toString())).intValue();
               break;
               }
            case 2:
               {
               mMm    = tok;
               mMmInt = (Integer.decode(mMm.trimLeft("0").toString())).intValue();
               break;
               }
            case 3:
               {
               mDd    = tok;
               mDdInt = (Integer.decode(mDd.trimLeft("0").toString())).intValue();
               break;
               }
            case 4:
               {
               mAuthor = tok;
               break;
               }
            case 5:
               {
               mTitle = tok;
               break;
               }
            case 6:
               {
               mV1 = tok;
               break;
               }
            case 7:
               {
               mV2 = tok;
               break;
               }
            default:
               {
               throw new Exception
                  (
                  "Invalid file name format.\n" +
                  "Format must be in this format:\n" +
                  "  yyyy.mm.dd.author.title.v1.v2.ext"
                  );
               }
            }
         }
      }



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   These are accessor methods

   <P><B>Implementation: </B><A HREF="ImageFileName.java.html#001">View source</A>

   @return
      The instance data
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public XString getYyyy()
      {
      return mYyyy;   // .toString();
      }
   public XString getMm()
      {
      return mMm;   // .toString();
      }
   public XString getDd()
      {
      return mDd;   // .toString();
      }
   public XString getAuthor()
      {
      return mAuthor;   // .toString();
      }
   public XString getTitle()
      {
      return mTitle;   // .toString();
      }
   public XString getV1()
      {
      return mV1;   // .toString();
      }
   public XString getV2()
      {
      return mV2;   // .toString();
      }
   public XString getExt()
      {
      return mExt;   // .toString();
      }
   public int getYyyyInt()
      {
      return mYyyyInt;
      }
   public int getMmInt()
      {
      return mMmInt;
      }
   public int getDdInt()
      {
      return mDdInt;
      }



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method returns a XString containing a date in this format:
      DDDD, MMMM dd, yyyy
   That it creates from these portions of the file name:
      Yyyy.mm.dd

   <P><B>Implementation: </B><A HREF="ImageFileName.java.html#002">View source</A>

   @return
      A XString containing a date in this format: DDDD, MMMM dd, yyyy
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public XString getDateString()
      {
      if (mDdInt == 0)
         {
         if (mMmInt == 0)
            {
            return mYyyy;   //  .toString();
            }
         else
            {
            Date  date = Util.getDate(mYyyyInt,mMmInt-1,1);
            return Util.timeStamp("MMMM yyyy",date);   //  .XString();
            }
         }
      if (mMmInt == 0)
         {
         return mYyyy;   //  .toString();
         }

      Date  date = Util.getDate(mYyyyInt,mMmInt-1,mDdInt);
      return Util.timeStamp("EEEE, MMMM d, yyyy",date);   //  .XString();
      }



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method returns the age of someone born on the date specified in the parameters on the date that
   is built into the file name

   <P><B>Implementation: </B><A HREF="ImageFileName.java.html#003">View source</A>

   @return
      The age on the date that is built into the file name

   @param
      pYyyy is the year someone was born
   @param
      pMm is the month (1..12) someone was born
   @param
      pDd is the day someone was born
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public XString getAge(int pYyyy, int pMm, int pDd)
      {
      if (mMmInt == 0)
         {
         Integer  age1 = new Integer(mYyyyInt - pYyyy - 1);
         Integer  age2 = new Integer(mYyyyInt - pYyyy);
         return XString.toXString(age1.toString() + "-" + age2.toString());
         }

      int dd = UMath.max(mDdInt,1);

      if (mMmInt < pMm)
         {
         Integer  age1 = new Integer(mYyyyInt - pYyyy - 1);
         return XString.toXString(age1);   //  .toString();
         }

      if (mMmInt == pMm)
         {
         if (dd < pDd)
            {
            Integer  age1 = new Integer(mYyyyInt - pYyyy - 1);
            return XString.toXString(age1);   //  .toString();
            }
         else
            {
            Integer  age2 = new Integer(mYyyyInt - pYyyy);
            return XString.toXString(age2);   //  .toString();
            }
         }

      Integer  age2 = new Integer(mYyyyInt - pYyyy);
      return XString.toXString(age2);   //  .toString();
      }



   /*:.
   ==============================================================================================================
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[  Protected  ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   ============================================================================================================== */



   /*:.
   ==============================================================================================================
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[  Private  ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   ============================================================================================================== */



   /*.
   ==========================================================================================
   Instance state data
   ------------------------------------------------------------------------------------------ */
   private XString  mYyyy      = new XString("0000");
   private XString  mMm        = new XString("00");
   private XString  mDd        = new XString("00");
   private XString  mAuthor    = new XString("");
   private XString  mTitle     = new XString("");
   private XString  mV1        = new XString("");
   private XString  mV2        = new XString("");
   private XString  mExt       = new XString("");
   private int      mYyyyInt   = 0;
   private int      mMmInt     = 0;
   private int      mDdInt     = 0;
   private HashMap  mSequence  = new HashMap();



   /*:.
   ==============================================================================================================
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[  Inner Classes  ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   ============================================================================================================== */



   /*:.
   ==============================================================================================================
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[  Public Static Methods  ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   ============================================================================================================== */



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method allows the ImageFileName class file to be unit tested as a stand-alone application. It's
   the method that's called when the class is invoked from the command line by using the java
   application launcher - "java". Main() is not a required method, but the practice of putting one in
   each class and wrapping class test code within it allows easy unit testing of the class; and main
   does not need to be removed when testing is complete.

   <P>
      <DL>
         <DT>
            <B>
               Command line usage:
            </B>
            <DD>
               Java cosmicabyss.com.lib.ImageFileName
            </DD>
         </DT>
      </DL>

   <P><B>Implementation: </B><A HREF="ImageFileName.java.html#004">View source</A>

   @param
      pArgs contains the command line arguments with which the class was invoked as an application.
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public static void main(String[] pArgs) throws Exception
      {
      ImageFileName  name = new ImageFileName("1989.06.12.Allen.title.00.00.jpg");
      System.out.println(name.getYyyy());
      System.out.println(name.getMm());
      System.out.println(name.getDd());
      System.out.println(name.getAuthor());
      System.out.println(name.getTitle());
      System.out.println(name.getV1());
      System.out.println(name.getV2());
      System.out.println(name.getExt());
      System.out.println(name.getYyyyInt());
      System.out.println(name.getMmInt());
      System.out.println(name.getDdInt());
      System.out.println(name.getDateString());
      System.out.println(name.getAge(1955,10,25));

      TreeMapOfHashMaps tm = new TreeMapOfHashMaps("C:/adb/Protected/Personal/HISTORY/!sequence.txt");
      System.out.println(tm.get("1978.07.19","other"));

      }



   }  // class ImageFileName