package org.jboss.cache;

import net.jcip.annotations.Immutable;

/**
 * Contains version information about this release of JBoss Cache.
 *
 * @author Bela Ban
 * @version $Id: Version.java 4592 2007-10-10 16:44:36Z manik.surtani@jboss.com $
 */
@Immutable
public class Version
{
   public static final String version = "2.2.2.GA";
   public static final String codename = "Poblano";
   //public static final String cvs = "$Id: Version.java 4592 2007-10-10 16:44:36Z manik.surtani@jboss.com $";
   static final byte[] version_id = {'0', '2', '2', '2', 'G', 'A'};

   private static final int MAJOR_SHIFT = 11;
   private static final int MINOR_SHIFT = 6;
   private static final int MAJOR_MASK = 0x00f800;
   private static final int MINOR_MASK = 0x0007c0;
   private static final int PATCH_MASK = 0x00003f;

   private static final short SHORT_1_2_3 = encodeVersion(1, 2, 3);
   private static final short SHORT_1_2_4_SP2 = encodeVersion(1, 2, 4);

   /**
    * Prints version information.
    */
   public static void main(String[] args)
   {
      System.out.println("\nVersion: \t" + version);
      System.out.println("Codename: \t" + codename);
      //System.out.println("CVS:      \t" + cvs);
      System.out.println("History:  \t(see http://jira.jboss.com/jira/browse/JBCACHE for details)\n");
   }

   /**
    * Returns version information as a string.
    */
   public static String printVersion()
   {
      return "JBossCache '" + codename + "' " + version;// + "[ " + cvs + "]";
   }

   public static String printVersionId(byte[] v, int len)
   {
      StringBuilder sb = new StringBuilder();
      if (v != null)
      {
         if (len <= 0)
            len = v.length;
         for (int i = 0; i < len; i++)
            sb.append((char) v[i]);
      }
      return sb.toString();
   }

   public static String printVersionId(byte[] v)
   {
      StringBuilder sb = new StringBuilder();
      if (v != null)
      {
         for (byte aV : v) sb.append((char) aV);
      }
      return sb.toString();
   }


   public static boolean compareTo(byte[] v)
   {
      if (v == null)
         return false;
      if (v.length < version_id.length)
         return false;
      for (int i = 0; i < version_id.length; i++)
      {
         if (version_id[i] != v[i])
            return false;
      }
      return true;
   }

   public static int getLength()
   {
      return version_id.length;
   }

   public static short getVersionShort()
   {
      return getVersionShort(version);
   }

   public static short getVersionShort(String versionString)
   {
      if (versionString == null)
         throw new IllegalArgumentException("versionString is null");

      // Special cases for version prior to 1.2.4.SP2
      if ("1.2.4".equals(versionString))
         return 124;
      else if ("1.2.4.SP1".equals(versionString))
         return 1241;

      String parts[] = versionString.split("[\\.\\-]");
      int a = 0;
      int b = 0;
      int c = 0;
      if (parts.length > 0)
         a = Integer.parseInt(parts[0]);
      if (parts.length > 1)
         b = Integer.parseInt(parts[1]);
      if (parts.length > 2)
         c = Integer.parseInt(parts[2]);
      return encodeVersion(a, b, c);
   }

   public static String getVersionString(short versionShort)
   {
      if (versionShort == SHORT_1_2_4_SP2)
         return "1.2.4.SP2";

      switch (versionShort)
      {
         case 124:
            return "1.2.4";
         case 1241:
            return "1.2.4.SP1";
         default:
            return decodeVersion(versionShort);
      }
   }

   public static short encodeVersion(int major, int minor, int patch)
   {
      return (short) ((major << MAJOR_SHIFT)
            + (minor << MINOR_SHIFT)
            + patch);
   }

   public static String decodeVersion(short version)
   {
      int major = (version & MAJOR_MASK) >> MAJOR_SHIFT;
      int minor = (version & MINOR_MASK) >> MINOR_SHIFT;
      int patch = (version & PATCH_MASK);
      return major + "." + minor + "." + patch;
   }

   public static boolean isBefore124(short version)
   {
      return (version > 1241 && version <= SHORT_1_2_3);
   }

   /**
    * Retroweaver version info.
    */
   public static class Retro
   {
      public static void main(String[] args)
      {
         System.out.println("\nVersion: \t" + version + " (Retroweaved for JDK 1.4.x compatibility)");
         System.out.println("Codename: \t" + codename);
         //System.out.println("CVS:      \t" + cvs);
         System.out.println("History:  \t(see http://jira.jboss.com/jira/browse/JBCACHE for details)\n");
      }
   }
}
