/*::.
==================================================================================================================================
=================================================¦ Copyright © 2006 Allen Baker ¦=================================================
                                                 +------------------------------+
File:       LockerPropertiesFile.java
Originator: Allen Baker (2006.11.21)
LayoutRev:  5
================================================================================================================================== */



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



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



/*::
======================================================================================================================== *//**
Instances of this class are ...

<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="LockerPropertiesFile.java.html">
               LockerPropertiesFile.java
            </A>
         </DD>
      </DT>
      <DT>
         <B>
            Author:
         </B>
         <DD>
            <A href="mailto:sourcecode.v01@cosmicabyss.com">
               Allen Baker
            </A>
         </DD>
      </DT>
   </DL>
*//*
======================================================================================================================== */
public class LockerPropertiesFile
   {



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method creates a LockerPropertiesFile class object.

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

   *//*
   ---------------------------------------------------------------------------------------------------- */
   public <Type> LockerPropertiesFile(Type pFileName)
      {
      iFile = new File(UString.toString(pFileName));
      }



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method returns the date that is in the file.

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

   @return
      Something
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public long date() throws Exception
      {
      return this.timeStamp();  // this line causes the file to be read
      }



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method compares the password to the one in the file.

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

   @return
      Something
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public <Type> boolean passwordMatches(Type pPassword) throws Exception
      {
      switch (this.version())  // this line causes the file to be read
         {
         default:
            {
            return iPasswordHash.equals(LockerUtils.passwordHash(pPassword));
            }
         }
      }



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method compares the iteration to the one in the file.

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

   @return
      Something
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public <Type> boolean iterationMatches(Type pPassword, int pIterations) throws Exception
      {
      switch (this.version())  // this line causes the file to be read
         {
         default:
            {
            return iIterationHash.equals(LockerUtils.iterationHash(pPassword,pIterations));
            }
         }
      }



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method writes out the parameter values to the file.

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

   *//*
   ---------------------------------------------------------------------------------------------------- */
   public <Type> LockerPropertiesFile write
      (
      Type  pPassword,
      int   pIterations,
      long  pDate
      )
      throws Exception
      {
      TextWriter  file = new TextWriter(iFile);
      file.println(VERSION);
      file.println(LockerUtils.passwordHash(pPassword));
      file.println(LockerUtils.iterationHash(pPassword,pIterations));
      file.println(pDate);
      file.close();

      iFileRead = false;

      return this;
      }



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



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



   /*.
   ==========================================================================================
   Class Constants
      VERSION : when a new LockerPropertiesFile is written, this is the
                Current version that is used
   ------------------------------------------------------------------------------------------ */
   private static final int  VERSION = 0;
   /*.
   ==========================================================================================
   Class variables
      cOut : console output.
   ------------------------------------------------------------------------------------------ */
   private static ConsoleStream  cOut = ConsoleStream.getSingleton();
   /*.
   ==========================================================================================
   Instance variables
      <BLOCKQUOTE>
         <PRE id="unindent">
            iFile          : the property file
            iFileRead      : flag to remember if the file has been read yet
            iVersion       : the version number in the file
            iPasswordHash  : the hashed password in the file
            iIterationHash : the hashed iteration count in the file
            iTimeStamp     : the time stamp in the file
         </PRE>
      </BLOCKQUOTE>
   ------------------------------------------------------------------------------------------ */
   private File     iFile          = null;
   private boolean  iFileRead      = false;
   private int      iVersion       = 0;
   private XString  iPasswordHash  = null;
   private XString  iIterationHash = null;
   private long     iTimeStamp     = 0;



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method gets the version number from the file

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

   @return
      Something
   *//*
   ---------------------------------------------------------------------------------------------------- */
   private int version() throws Exception
      {
      if ( ! iFileRead)
         {
         this.readFile();
         }
      return iVersion;
      }



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method gets the time stamp from the file

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

   @return
      Something
   *//*
   ---------------------------------------------------------------------------------------------------- */
   private long timeStamp() throws Exception
      {
      if ( ! iFileRead)
         {
         this.readFile();
         }
      return iTimeStamp;
      }



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method this method reads the file

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

   @return
      Something
   *//*
   ---------------------------------------------------------------------------------------------------- */
   private LockerPropertiesFile readFile() throws Exception
      {
      TextReaderIterator  iter = new TextReaderIterator(iFile);
      int                 line = 0;

      /*.
      ==========================================================================================
      ------------------------------------------------------------------------------------------ */
      while (( ! iFileRead) && iter.hasNext())
         {
         /*.
         ==========================================================================================
         ------------------------------------------------------------------------------------------ */
         line++;
         switch (line)
            {
            /*.
            ==========================================================================================
            Line 1 is the version number
            ------------------------------------------------------------------------------------------ */
            case 1:
               {
               iVersion = UMath.decodeDecimalInt((XString)iter.next());
               break;
               }
            /*.
            ==========================================================================================
            Line 2 is a hash of the password
            ------------------------------------------------------------------------------------------ */
            case 2:
               {
               iPasswordHash = (XString)iter.next();
               break;
               }
            /*.
            ==========================================================================================
            Line 3 is a hash of the iterations
            ------------------------------------------------------------------------------------------ */
            case 3:
               {
               iIterationHash = (XString)iter.next();
               break;
               }
            /*.
            ==========================================================================================
            Line 4 is the date-time-stamp
            ------------------------------------------------------------------------------------------ */
            case 4:
               {
               iTimeStamp = UMath.decodeDecimalLong((XString)iter.next());
               break;
               }
            /*.
            ==========================================================================================
            If there are any other lines, they are undefined and are skipped
            ------------------------------------------------------------------------------------------ */
            default:
               {
               iFileRead = true;
               break;
               }
            }
         }
      iter.cleanup();
      iFileRead = true;

      return this;
      }



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



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



   /*:                                    
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method allows the LockerPropertiesFile class file to be unit tested as a standalone
   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.LockerPropertiesFile
            </DD>
         </DT>
      </DL>

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

   @param
      pArgs contains the command line arguments with which the class was invoked as an application.
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public static void main(XString[] pArgs) throws Exception
      {
      }



   }  // class LockerPropertiesFile