Skip to content

Commit

Permalink
ArrayComparator
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Jan 31, 2014
1 parent 551e743 commit 3544416
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 51 deletions.
81 changes: 81 additions & 0 deletions src/main/java/com/jcabi/immutable/ArrayComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2012-2013, JCabi.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the jcabi.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcabi.immutable;

import com.jcabi.aspects.Immutable;

/**
* Comparator for arrays.
*
* @param <T> Value type
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 1.0
*/
@Immutable
public interface ArrayComparator<T> extends java.util.Comparator<T> {

/**
* Default comparator.
* @param <T> Type of argument
*/
@Immutable
final class Default<T> implements ArrayComparator<T> {
@Override
public int compare(final T left, final T right) {
return Comparable.class.cast(left).compareTo(right);
}
}

/**
* Neutral comparator (never compares).
* @param <T> Type of argument
*/
@Immutable
final class Neutral<T> implements ArrayComparator<T> {
@Override
public int compare(final T left, final T right) {
return 1;
}
}

/**
* Reverse comparator.
* @param <T> Type of argument
*/
@Immutable
final class Reverse<T> implements ArrayComparator<T> {
@Override
public int compare(final T left, final T right) {
return Comparable.class.cast(right).compareTo(left);
}
}

}
60 changes: 11 additions & 49 deletions src/main/java/com/jcabi/immutable/ArraySortedSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
Expand Down Expand Up @@ -65,13 +66,13 @@ public final class ArraySortedSet<T> implements SortedSet<T> {
/**
* Comparator to use.
*/
private final transient ArraySortedSet.Comparator<T> cmp;
private final transient ArrayComparator<T> cmp;

/**
* Public ctor.
* @param comparator Comparator to use
*/
public ArraySortedSet(final ArraySortedSet.Comparator<T> comparator) {
public ArraySortedSet(final ArrayComparator<T> comparator) {
this(comparator, (T[]) new Object[0]);
}

Expand All @@ -88,7 +89,7 @@ public ArraySortedSet(final T... set) {
* @param comparator The comparator to use
* @param set Original set
*/
public ArraySortedSet(final ArraySortedSet.Comparator<T> comparator,
public ArraySortedSet(final ArrayComparator<T> comparator,
final T... set) {
this(Arrays.asList(set), comparator);
}
Expand All @@ -99,7 +100,7 @@ public ArraySortedSet(final ArraySortedSet.Comparator<T> comparator,
* @since 0.12
*/
public ArraySortedSet(final Iterable<T> set) {
this(set, new ArraySortedSet.Comparator.Default<T>());
this(set, new ArrayComparator.Default<T>());
}

/**
Expand All @@ -108,7 +109,7 @@ public ArraySortedSet(final Iterable<T> set) {
* @param comparator Comparator to use
*/
public ArraySortedSet(final Iterable<T> set,
final ArraySortedSet.Comparator<T> comparator) {
final ArrayComparator<T> comparator) {
if (set == null) {
throw new IllegalArgumentException(
"first argument of ArraySortedSet ctor can't be NULL"
Expand Down Expand Up @@ -235,7 +236,9 @@ public Comparator<? super T> comparator() {

@Override
public SortedSet<T> subSet(final T from, final T till) {
throw new UnsupportedOperationException();
return Collections.unmodifiableSortedSet(
new TreeSet<T>(this).subSet(from, till)
);
}

@Override
Expand All @@ -255,15 +258,15 @@ public SortedSet<T> tailSet(final T from) {
@Override
public T first() {
if (this.values.length == 0) {
throw new NoSuchElementException();
throw new NoSuchElementException("sorted set is empty, no first()");
}
return this.values[0];
}

@Override
public T last() {
if (this.values.length == 0) {
throw new NoSuchElementException();
throw new NoSuchElementException("sorted set is empty, not last()");
}
return this.values[this.values.length - 1];
}
Expand Down Expand Up @@ -339,45 +342,4 @@ public void clear() {
);
}

/**
* Comparator.
* @param <T> Type of argument
*/
@Immutable
public interface Comparator<T> extends java.util.Comparator<T> {
/**
* Default comparator.
* @param <T> Type of argument
*/
@Immutable
final class Default<T> implements ArraySortedSet.Comparator<T> {
@Override
public int compare(final T left, final T right) {
return Comparable.class.cast(left).compareTo(right);
}
};
/**
* Neutral comparator (never compares).
* @param <T> Type of argument
*/
@Immutable
final class Neutral<T> implements ArraySortedSet.Comparator<T> {
@Override
public int compare(final T left, final T right) {
return 1;
}
};
/**
* Reverse comparator.
* @param <T> Type of argument
*/
@Immutable
final class Reverse<T> implements ArraySortedSet.Comparator<T> {
@Override
public int compare(final T left, final T right) {
return Comparable.class.cast(right).compareTo(left);
}
};
};

}
52 changes: 50 additions & 2 deletions src/test/java/com/jcabi/immutable/ArraySortedSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public final class ArraySortedSetTest {
/**
* Simple comparator.
*/
private static final ArraySortedSet.Comparator<Integer> CMP =
new ArraySortedSet.Comparator.Default<Integer>();
private static final ArrayComparator<Integer> CMP =
new ArrayComparator.Default<Integer>();

/**
* ArraySortedSet can work as a sorted set.
Expand Down Expand Up @@ -121,4 +121,52 @@ public void encapsulatesIterables() throws Exception {
);
}

/**
* ArraySortedSet can work with a custom comparator.
* @throws Exception If some problem inside
*/
@Test
public void worksWithCustomComparator() throws Exception {
final String first = "some text that is long";
final String second = "short text";
MatcherAssert.assertThat(
new ArraySortedSet<String>(
Arrays.asList(second, first),
new ArrayComparator<String>() {
@Override
public int compare(final String left, final String right) {
return right.length() - left.length();
}
}
),
Matchers.contains(first, second)
);
}

/**
* ArraySortedSet can replace a comparator of an another ArraySortedSet.
* @throws Exception If some problem inside
*/
@Test
public void replacesComparator() throws Exception {
final String first = "B very long long text";
final String second = "A short text";
final SortedSet<String> origin = new ArraySortedSet<String>(
Arrays.asList(second, first),
new ArrayComparator<String>() {
@Override
public int compare(final String left, final String right) {
return right.length() - left.length();
}
}
);
MatcherAssert.assertThat(
new ArraySortedSet<String>(
origin,
new ArrayComparator.Default<String>()
),
Matchers.contains(second, first)
);
}

}

0 comments on commit 3544416

Please sign in to comment.