public int compare( Path o1, Path o2 ) { if (o1 == o2) return 0; if (o1 == null) return -1; if (o2 == null) return 1; return o1.compareTo(o2); } };
/** * {@inheritDoc} * * @see java.lang.Comparable#compareTo(java.lang.Object) */ public int compareTo( Rule other ) { if (other == this) return 0; if (other instanceof PathRule) { PathRule that = (PathRule)other; int diff = this.getPathInRepository().compareTo(that.getPathInRepository()); if (diff != 0) return diff; diff = this.getPathInSource().compareTo(that.getPathInSource()); if (diff != 0) return diff; Iterator<Path> thisIter = this.getExceptionsToRule().iterator(); Iterator<Path> thatIter = that.getExceptionsToRule().iterator(); while (thisIter.hasNext() && thatIter.hasNext()) { diff = thisIter.next().compareTo(thatIter.next()); if (diff != 0) return diff; } if (thisIter.hasNext()) return 1; if (thatIter.hasNext()) return -1; return 0; } return other.getClass().getName().compareTo(this.getClass().getName()); }
@Test public void shouldAlwaysConsiderRootAsEqualToAnyOtherRoot() { Path other = mock(Path.class); when(other.isRoot()).thenReturn(true); assertThat(root.compareTo(other), is(0)); assertThat(root.equals(other), is(true)); assertThat(root.equals(root), is(true)); }
@Test public void shouldAlwaysConsiderPathEqualToItself() { Path other = mock(Path.class); when(other.isRoot()).thenReturn(true); assertThat(path.compareTo(path), is(0)); assertThat(path.equals(path), is(true)); }
@Test public void shouldAlwaysConsiderRootAsLessThanAnyPathOtherThanRoot() { Path other = mock(Path.class); when(other.isRoot()).thenReturn(false); assertThat(root.compareTo(other), is(-1)); assertThat(root.equals(other), is(false)); }
@Test public void shouldCreateEquivalentPathsWhetherOrNotThereIsATrailingDelimiter() { path = factory.create("/a/b/c/d/dna:e/dna:f"); path2 = factory.create("/a/b/c/d/dna:e/dna:f/"); assertThat(path.equals(path2), is(true)); assertThat(path.compareTo(path2), is(0)); }
/** * {@inheritDoc} * * @see java.lang.Comparable#compareTo(java.lang.Object) */ public int compareTo( Location that ) { if (this == that) return 0; if (this.hasPath() && that.hasPath()) { return this.getPath().compareTo(that.getPath()); } UUID thisUuid = this.getUuid(); UUID thatUuid = that.getUuid(); if (thisUuid != null && thatUuid != null) { return thisUuid.compareTo(thatUuid); } return this.hashCode() - that.hashCode(); }