Java Code Examples for java.nio.file.Path#compareTo()

The following examples show how to use java.nio.file.Path#compareTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MissingTOCTargetIDInvalidLink.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public int compareTo(InvalidLink other) {
	if (other == null) {
		return 1;
	}

	if (!(other instanceof MissingTOCTargetIDInvalidLink)) {
		return -1; // always put us above other types of Invalid Links
	}

	MissingTOCTargetIDInvalidLink otherLink = (MissingTOCTargetIDInvalidLink) other;
	Path sourceFile = item.getSourceFile();
	Path otherSourceFile = otherLink.item.getSourceFile();
	int result = sourceFile.compareTo(otherSourceFile);
	if (result != 0) {
		return result;
	}

	return item.getIDAttribute().compareTo(otherLink.item.getIDAttribute());
}
 
Example 2
Source File: Main.java    From Java-Coding-Problems with MIT License 5 votes vote down vote up
public static void main(String[] args) throws IOException {

        Path path1 = Paths.get("/learning/packt/JavaModernChallenge.pdf");
        Path path2 = Paths.get("/LEARNING/PACKT/JavaModernChallenge.pdf");
        Path path3 = Paths.get("D:/learning/packt/JavaModernChallenge.pdf");

        boolean path1EqualsPath2 = path1.equals(path2);
        boolean path2EqualsPath3 = path2.equals(path3);
        System.out.println("path1.equals(path2): " + path1EqualsPath2);
        System.out.println("path2.equals(path3): " + path2EqualsPath3);

        boolean path1IsSameFilePath2 = Files.isSameFile(path1, path2);
        boolean path1IsSameFilePath3 = Files.isSameFile(path1, path3);
        boolean path2IsSameFilePath3 = Files.isSameFile(path2, path3);
        System.out.println("\nisSameFile(path1, path2): " + path1IsSameFilePath2);
        System.out.println("isSameFile(path1, path3): " + path1IsSameFilePath3);
        System.out.println("isSameFile(path2, path3): " + path2IsSameFilePath3);

        int path1compareToPath2 = path1.compareTo(path2);
        int path1compareToPath3 = path1.compareTo(path3);
        int path2compareToPath3 = path2.compareTo(path3);
        System.out.println("\npath1.compareTo(path2): " + path1compareToPath2);
        System.out.println("path1.compareTo(path3): " + path1compareToPath3);
        System.out.println("path2.compareTo(path3): " + path2compareToPath3);

        boolean sw = path1.startsWith("/learning/packt");
        boolean ew = path1.endsWith("JavaModernChallenge.pdf");
        System.out.println("\nStart width: " + sw);
        System.out.println("End with: " + ew);
    }
 
Example 3
Source File: DuplicateAnchorCollectionByHelpFile.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(DuplicateAnchorCollectionByHelpFile o) {
	HelpFile helpFile1 = getHelpFile();
	HelpFile helpFile2 = o.getHelpFile();
	Path file1 = helpFile1.getFile();
	Path file2 = helpFile2.getFile();
	return file1.compareTo(file2);
}
 
Example 4
Source File: PeriodicFilesRollModeFactory.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public Comparator<Path> getComparator() {
  return new Comparator<Path>() {
    @Override
    public int compare(Path o1, Path o2) {
      return o1.compareTo(o2);
    }
  };
}
 
Example 5
Source File: ExternalModuleSpecService.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private int compareBySameLevel(Path path1, Path path2) {
    int i = 0;
    int comparison = 0;
    for (Iterator<Path> iterPath1 = path1.iterator(); iterPath1.hasNext(); i++) {
        Path levelPath1 = iterPath1.next();
        Path levelPath2 = path2.getName(i);
        comparison = levelPath1.compareTo(levelPath2);
        if (comparison!=0) {
            break;
        }
    }
    return comparison;
}
 
Example 6
Source File: Global.java    From kanzi with Apache License 2.0 5 votes vote down vote up
public static void sortFilesByPathAndSize(List<Path> files, final boolean sortBySize)
{
   Comparator<Path> c = new Comparator<Path>()
   {
      @Override
      public int compare(Path p1, Path p2)
      {
         if (sortBySize == false)
            return p1.compareTo(p2);
         
         // First, compare parent directory paths
         final int res = p1.getParent().compareTo(p2.getParent());
         
         if (res != 0)
            return res;
         
         try 
         {
            // Then, compare file sizes (decreasing order)
            return (int) (Files.size(p2) - Files.size(p1));
         }
         catch (IOException e)
         {
            return -1;
         }
      }         
   };
   
   Collections.sort(files, c);     
}
 
Example 7
Source File: ParsingJavaPackageFinder.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(Path o1, Path o2) {
  int lengthCompare = Integer.compare(o2.getNameCount(), o1.getNameCount());
  if (lengthCompare == 0) {
    return o2.compareTo(o1);
  }
  return lengthCompare;
}
 
Example 8
Source File: BuckUnixPathTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
@Parameters({
  "/,/,=",
  ",,=",
  "/some/path,/some/path,=",
  "a,b,<",
  "b,a,>",
  "some/a,some/b,<",
  "some/b,some/a,>",
  "/some,/some/a,<"
})
public void compareToMethod(String data1, String data2, String expected) {
  Path path1 = BuckUnixPathUtils.createPath(data1);
  Path path2 = BuckUnixPathUtils.createPath(data2);
  int actual = path1.compareTo(path2);
  switch (expected) {
    case "=":
      assertEquals(0, actual);
      break;
    case ">":
      assertTrue(actual > 0);
      break;
    case "<":
      assertTrue(actual < 0);
      break;
    default:
      throw new IllegalArgumentException(expected);
  }
}
 
Example 9
Source File: HREF.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(HREF other) {
	if (this.equals(other)) {
		return 0;
	}

	// group all HREFs in the same directory first
	HelpModuleLocation otherHelp = other.help;
	Path otherHelpLoc = otherHelp.getHelpLocation();
	Path myHelpLoc = help.getHelpLocation();
	if (!myHelpLoc.equals(otherHelpLoc)) {
		return myHelpLoc.compareTo(otherHelpLoc);
	}

	// check file
	Path otherSourceFile = other.getSourceFile();
	if (!sourceFile.equals(otherSourceFile)) {
		return sourceFile.compareTo(otherSourceFile);
	}

	// same source file, check line number
	if (lineNumber != other.lineNumber) {
		return lineNumber - other.lineNumber;
	}

	String helpPath = getHelpPath();
	String otherHelpPath = other.getHelpPath();
	if (helpPath != null && otherHelpPath != null) {
		int result = helpPath.compareTo(otherHelpPath);
		if (result != 0) {
			return result;
		}
	}
	else {
		if (helpPath == null && otherHelpPath != null) {
			return -1; // our path is null and 'other's is not; we go before
		}
		else if (helpPath != null && otherHelpPath == null) {
			return 1; // we have a non-null path, but 'other' doesn't; we go after
		}
	}

	// highly unlikely case that we have to HREFs from the same file, pointing to the same
	// place, on the same HTML line.  In this case, just use the object that was created first,
	// as it was probably parsed first from the file
	int identityHashCode = System.identityHashCode(this);
	int otherIdentityHashCode = System.identityHashCode(other);
	return identityHashCode - otherIdentityHashCode;
}
 
Example 10
Source File: IMG.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(IMG other) {

	// group all HREFs in the same directory first
	HelpModuleLocation otherHelp = other.help;
	Path otherHelpLoc = otherHelp.getHelpLocation();
	Path myHelpLoc = help.getHelpLocation();
	if (!myHelpLoc.equals(otherHelpLoc)) {
		return myHelpLoc.compareTo(otherHelpLoc);
	}

	// check file
	Path otherSourceFile = other.getSourceFile();
	if (!sourceFile.equals(otherSourceFile)) {
		return sourceFile.toUri().compareTo(otherSourceFile.toUri());
	}

	// same source file, check line number
	if (lineNumber != other.lineNumber) {
		return lineNumber - other.lineNumber;
	}

	Path myHelpPath = getHelpPath();
	Path otherHelpPath = other.getHelpPath();
	if (myHelpPath != null && otherHelpPath != null) {
		int result = myHelpPath.compareTo(otherHelpPath);
		if (result != 0) {
			return result;
		}
	}
	else {
		if (myHelpPath == null && otherHelpPath != null) {
			return -1; // our path is null and 'other's is not; we go before
		}
		else if (myHelpPath != null && otherHelpPath == null) {
			return 1; // we have a non-null path, but 'other' doesn't; we go after
		}
	}

	// highly unlikely case that we have to HREFs from the same file, pointing to the same
	// place, on the same HTML line.  In this case, just use the object that was created first,
	// as it was probably parsed first from the file
	int identityHashCode = System.identityHashCode(this);
	int otherIdentityHashCode = System.identityHashCode(other);
	return identityHashCode - otherIdentityHashCode;
}
 
Example 11
Source File: DuplicateAnchorCollectionByHelpTopic.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(DuplicateAnchorCollectionByHelpTopic o) {
	Path topicFile1 = topic.getTopicFile();
	Path topicFile2 = o.topic.getTopicFile();
	return topicFile1.compareTo(topicFile2);
}
 
Example 12
Source File: PathOrderService.java    From AsciidocFX with Apache License 2.0 3 votes vote down vote up
public int comparePaths(Path first, Path second) {

        if (OSHelper.isMac() || OSHelper.isWindows())
            return first.getFileName().toString().compareToIgnoreCase(second.getFileName().toString());

        return first.compareTo(second);
    }