/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.app;

import com.silverwrist.util.*;
import com.silverwrist.dynamo.except.*;

class ComponentResName
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String[] m_components;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  ComponentResName(String resource_path)
  {
    if (!(resource_path.startsWith("/")))
      throw new NoSuchResourceException(resource_path);
    m_components = StringUtils.split(resource_path.substring(1),"/");
    if ((m_components==null) || (m_components.length==0))
      throw new NoSuchResourceException(resource_path);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class Object
   *--------------------------------------------------------------------------------
   */

  public boolean equals(Object obj)
  {
    if ((obj==null) || !(obj instanceof ComponentResName))
      return false;
    ComponentResName other = (ComponentResName)obj;
    if (m_components.length!=other.m_components.length)
      return false;
    return (getMatchCount(other)==m_components.length);

  } // end equals

  public int hashCode()
  {
    int rc = 0;
    for (int i=0; i<m_components.length; i++)
      rc ^= m_components[i].hashCode();
    return rc;

  } // end hashCode

  public String toString()
  {
    return getStringValue(0,true);

  } // end toString

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  int getComponentCount()
  {
    return m_components.length;

  } // end getComponentCount

  int getMatchCount(ComponentResName other)
  {
    int max_count = Math.min(other.m_components.length,m_components.length);
    for (int i=0; i<max_count; i++)
    { // if a component does not match, return the count immediately
      if (!(m_components[i].equals(other.m_components[i])))
	return i;

    } // end for

    return max_count;  // all components matched

  } // end getMatchCount

  String getStringValue(int skip_initial, boolean prefix_slash)
  {
    StringBuffer rc = new StringBuffer();
    if (prefix_slash)
      rc.append('/');
    boolean need_sep = false;
    for (int i=skip_initial; i<m_components.length; i++)
    { // append together components
      if (need_sep)
	rc.append('/');
      else
	need_sep = true;
      rc.append(m_components[i]);

    } // end for

    return rc.toString();

  } // end getStringValue

} // end class ComponentResName
