Java String Equal pathEquals(String path1, String path2)

Here you can find the source of pathEquals(String path1, String path2)

Description

path Equals

License

Apache License

Declaration

public static boolean pathEquals(String path1, String path2) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Main {
    public static boolean pathEquals(String path1, String path2) {
        return cleanPath(path1).equals(cleanPath(path2));
    }/*from   www.j  ava  2 s  .  com*/

    public static String cleanPath(String path) {
        String pathToUse = replace(path, "\\", "/");

        int prefixIndex = pathToUse.indexOf(":");
        String prefix = "";
        if (prefixIndex != -1) {
            prefix = pathToUse.substring(0, prefixIndex + 1);
            pathToUse = pathToUse.substring(prefixIndex + 1);
        }

        String[] pathArray = delimitedListToStringArray(pathToUse, "/");
        List pathElements = new LinkedList();
        int tops = 0;

        for (int i = pathArray.length - 1; i >= 0; i--) {
            if (".".equals(pathArray[i]))
                continue;
            if ("..".equals(pathArray[i])) {
                tops++;
            } else if (tops > 0) {
                tops--;
            } else {
                pathElements.add(0, pathArray[i]);
            }
        }
        for (int i = 0; i < tops; i++) {
            pathElements.add(0, "..");
        }
        return prefix + collectionToDelimitedString(pathElements, "/");
    }

    public static String replace(String inString, String oldPattern, String newPattern) {
        if (inString == null)
            return null;
        if ((oldPattern == null) || (newPattern == null)) {
            return inString;
        }
        StringBuffer sbuf = new StringBuffer();

        int pos = 0;
        int index = inString.indexOf(oldPattern);

        int patLen = oldPattern.length();
        while (index >= 0) {
            sbuf.append(inString.substring(pos, index));
            sbuf.append(newPattern);
            pos = index + patLen;
            index = inString.indexOf(oldPattern, pos);
        }
        sbuf.append(inString.substring(pos));

        return sbuf.toString();
    }

    public static String[] delimitedListToStringArray(String str, String delimiter) {
        if (str == null)
            return new String[0];
        if (delimiter == null) {
            return new String[] { str };
        }
        List result = new ArrayList();
        if ("".equals(delimiter)) {
            for (int i = 0; i < str.length(); i++)
                result.add(str.substring(i, i + 1));
        } else {
            int pos = 0;
            int delPos = 0;
            while ((delPos = str.indexOf(delimiter, pos)) != -1) {
                result.add(str.substring(pos, delPos));
                pos = delPos + delimiter.length();
            }
            if ((str.length() > 0) && (pos <= str.length())) {
                result.add(str.substring(pos));
            }
        }
        return toStringArray(result);
    }

    public static String collectionToDelimitedString(Collection coll, String delim) {
        return collectionToDelimitedString(coll, delim, "", "");
    }

    public static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix) {
        if (coll == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Iterator it = coll.iterator();
        int i = 0;
        while (it.hasNext()) {
            if (i > 0)
                sb.append(delim);
            sb.append(prefix).append(it.next()).append(suffix);
            i++;
        }
        return sb.toString();
    }

    public static String[] toStringArray(Collection collection) {
        if (collection == null)
            return null;
        return (String[]) collection.toArray(new String[collection.size()]);
    }
}

Related

  1. equalsIgnoreCase(String source, String query)
  2. equalsOrExceeds(String version, String threshold)
  3. getEquality(String str1, String str2)
  4. isEqual(String str, String str2, boolean isCaseSensitive)
  5. pathEquals(String path1, String path2)
  6. pathEquals(String path1, String path2)