//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.workflow.xpdl.util;

import java.util.HashMap;
import java.util.Iterator;

import org.huihoo.workflow.WorkflowComponent;

/**
 * Class which defines all duration units available.  This class defines
 * the following duration units:
 * 
 * <ul>
 * <li><b>Y</b> - Year
 * <li><b>M</b> - Month
 * <li><b>D</b> - Day
 * <li><b>h</b> - Hour
 * <li><b>m</b> - Minute
 * <li><b>s</b> - Second
 * </ul>
 * 
 */

public final class DurationUnit  implements WorkflowComponent
{
  public static final int TYPE_YEAR   = 1;
  public static final int TYPE_MONTH  = TYPE_YEAR+1;
  public static final int TYPE_DAY    = TYPE_MONTH+1;
  public static final int TYPE_HOUR   = TYPE_DAY+1;
  public static final int TYPE_MINUTE = TYPE_HOUR+1;
  public static final int TYPE_SECOND = TYPE_MINUTE+1;
  
  public static final DurationUnit YEAR   = new DurationUnit(TYPE_YEAR);
  public static final DurationUnit MONTH  = new DurationUnit(TYPE_MONTH);
  public static final DurationUnit DAY    = new DurationUnit(TYPE_DAY);
  public static final DurationUnit HOUR   = new DurationUnit(TYPE_HOUR);
  public static final DurationUnit MINUTE = new DurationUnit(TYPE_MINUTE);
  public static final DurationUnit SECOND = new DurationUnit(TYPE_SECOND);
  
  private static HashMap stringMap = new HashMap();
  private static HashMap unitToMillisecondsMap = new HashMap();
  
  static
  {
    stringMap.put(YEAR, "Y");
    stringMap.put(MONTH, "M");
    stringMap.put(DAY, "D");
    stringMap.put(HOUR, "h");
    stringMap.put(MINUTE, "m");
    stringMap.put(SECOND, "s");
    
    unitToMillisecondsMap.put(SECOND, new Long(1000));
    unitToMillisecondsMap.put(MINUTE, new Long(1000 * 60));
    unitToMillisecondsMap.put(HOUR, new Long(1000 * 60 * 60));
    unitToMillisecondsMap.put(DAY, new Long(1000 * 60 * 60 * 24));
    unitToMillisecondsMap.put(MONTH, new Long((long)(1000 * 60 * 60 * 24) * (long)30));
    unitToMillisecondsMap.put(YEAR, new Long((long)(1000 * 60 * 60 * 24) * (long)365));
  }
  
  private int value;
  
  private DurationUnit(int value)
  {
    this.value = value;
  }
  
  /** Get the int value of the DurationUnit constant.  This is useful
    for use in <code>case</code> logic.
    
    @return The int value
  */
  
  public int getValue()
  {
    return value;
  }
  
  /** Return the String representation of the duration unit.
  
    @return String representation of the duration unit
  */
  
  public String toString()
  {
    return typeToString(this);
  }
  
  /** jConvert the duration unit to milliseconds.
  
    @return jConvert duration unit to milliseconds
  */
  
  public long toMilliseconds()
  {
    return unitToMilliseconds(this);
  }
  
  public static String typeToString(DurationUnit type)
  {
    return (String)stringMap.get(type);
  }
  
  public static DurationUnit stringToType(String type)
  {
    Iterator keys = stringMap.keySet().iterator();
    while(keys.hasNext())
    {
      DurationUnit key = (DurationUnit)keys.next();
      String stringValue = (String)stringMap.get(key);
      if(stringValue.equals(type))
      {
        return key;
      }
    }
    return null;
  }
  
  public static long unitToMilliseconds(DurationUnit unit)
  {
    Long value = (Long)unitToMillisecondsMap.get(unit);
    if(value == null)
    {
      throw new IllegalArgumentException("Unknown duration unit: " + unit);
    } 
    else 
    {
      return value.longValue();
    }
  }
  
  public String getTagName()
  {
  	return "DurationUnit";
  }
    
}
