content: /*
   * This library is part of OpenCms -
   * the Open Source Content Management System
   *
   * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * For further information about Alkacon Software, please see the
   * company website: https://www.alkacon.com
   *
   * For further information about OpenCms, please see the
   * project website: https://www.opencms.org
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   */
  
  package org.opencms.util;
  
  import java.io.Serializable;
  
  /**
   * Simple data holder class which stores a path in a normalized form.<p>
   *
   * This is mostly useful when using paths as map keys, when you are not sure if the paths you are processing have a trailing slash
   * or not.<p>
   *
   * The paths are stored in the form '/foo/bar/baz', i.e. they include a leading but no trailing slash, except in the case of the root path '/'.
   */
  public class CmsPath implements Serializable, Comparable<CmsPath> {
  
      /** Serial version id. */
      private static final long serialVersionUID = 1L;
  
      /** The internal path string. */
      private String m_pathStr;
  
      /**
       * Creates a new instance.
       *
       * @param pathStr the path string
       */
      public CmsPath(String pathStr) {
  
          m_pathStr = normalize(pathStr);
      }
  
      /**
       * Checks if two (absolute) paths are equal in normalized form.
       *
       * @param path1 the first path
       * @param path2 the second path
       * @return true if the paths are logically equal
       */
      public static boolean equal(String path1, String path2) {
  
          if ((path1 == null) && (path2 == null)) {
              return true;
          }
          if ((path1 == null) || (path2 == null)) {
              return false;
          }
          return normalize(path1).equals(normalize(path2));
  
      }
  
      /**
       * Normalizes a path.
       *
       * <p>The normalized path always has a leading slash, but never a trailing slash if it's more than one character long.
       *
       * @param pathStr the path to normalize
       * @return the normalized path
       */
      private static String normalize(String pathStr) {
  
          String result = null;
          if (pathStr.equals("") || pathStr.equals("/")) {
              result = "/";
          } else {
              result = CmsFileUtil.removeTrailingSeparator(pathStr);
              if (!result.startsWith("/")) {
                  result = "/" + result;
              }
          }
          return result;
      }
  
      /**
       * Gets the path as a string.
       *
       * @return the path as a string.
       */
      public String asString() {
  
          return m_pathStr;
      }
  
      /**
       * @see java.lang.Comparable#compareTo(java.lang.Object)
       */
      public int compareTo(CmsPath o) {
  
          return m_pathStr.compareTo(o.m_pathStr);
      }
  
      /**
       * @see java.lang.Object#equals(java.lang.Object)
       */
      @Override
      public boolean equals(Object obj) {
  
          if (!(obj instanceof CmsPath)) {
              return false;
          }
          return ((CmsPath)obj).m_pathStr.equals(m_pathStr);
      }
  
      /**
       * @see java.lang.Object#hashCode()
       */
      @Override
      public int hashCode() {
  
          return m_pathStr.hashCode();
      }
  
      /**
       * Returns true if this path is a prefix of the path given as parameter.
       *
       * @param path a path
       * @return true if this path is a prefix of the parameter
       */
      public boolean isPrefixOf(CmsPath path) {
  
          return isPrefixOfStr(path.asString());
  
      }
  
      /**
       * Returns true if this path is a prefix path of the given path
       *
       * @param path a path
       * @return true if the path represented by this object is a prefix of the given path
       */
      public boolean isPrefixOfStr(String path) {
  
          return CmsStringUtil.isPrefixPath(m_pathStr, path);
  
      }
  
      /**
       * @see java.lang.Object#toString()
       */
      @Override
      public String toString() {
  
          return "CmsPath[" + asString() + "]";
      }
  
  }
  