Skip to content

Commit

Permalink
Add missing empty with comparator methods - fixes #1328
Browse files Browse the repository at this point in the history
  • Loading branch information
Desislav-Petrov committed Aug 16, 2022
1 parent 90eecc4 commit 7586a56
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public interface MutableSortedMapFactory
*/
<K, V> MutableSortedMap<K, V> empty();

/**
* @since 12.0
*/
<K, V> MutableSortedMap<K, V> empty(Comparator<? super K> comparator);

/**
* Same as {@link #empty()}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public interface MutableSortedSetFactory
*/
<T> MutableSortedSet<T> empty();

/**
* @since 12.0
*/
<T> MutableSortedSet<T> empty(Comparator<? super T> comparator);

/**
* Same as {@link #empty()}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public <K, V> MutableSortedMap<K, V> empty()
return TreeSortedMap.newMap();
}

@Override
public <K, V> MutableSortedMap<K, V> empty(Comparator<? super K> comparator)
{
if (comparator == null)
{
return this.of();
}
return TreeSortedMap.newMap(comparator);
}

@Override
public <K, V> MutableSortedMap<K, V> of()
{
Expand Down Expand Up @@ -96,11 +106,7 @@ public <K, V> MutableSortedMap<K, V> of(Comparator<? super K> comparator)
@Override
public <K, V> MutableSortedMap<K, V> with(Comparator<? super K> comparator)
{
if (comparator == null)
{
return this.of();
}
return TreeSortedMap.newMap(comparator);
return this.empty(comparator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public <T> MutableSortedSet<T> empty()
return TreeSortedSet.newSet();
}

@Override
public <T> MutableSortedSet<T> empty(Comparator<? super T> comparator)
{
return TreeSortedSet.newSet(comparator);
}

@Override
public <T> MutableSortedSet<T> of()
{
Expand Down Expand Up @@ -71,7 +77,7 @@ public <T> MutableSortedSet<T> of(Comparator<? super T> comparator)
@Override
public <T> MutableSortedSet<T> with(Comparator<? super T> comparator)
{
return TreeSortedSet.newSet(comparator);
return this.empty(comparator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/

package org.eclipse.collections.impl.factory;

import org.eclipse.collections.api.factory.map.sorted.MutableSortedMapFactory;
import org.junit.Assert;
import org.junit.Test;

public class SortedMapsTest
{
@Test
public void mutables()
{
MutableSortedMapFactory factory = SortedMaps.mutable;
Assert.assertEquals(SortedMaps.mutable.empty(), factory.empty());
Assert.assertEquals(SortedMaps.mutable.empty(Integer::compare), factory.empty(Integer::compare));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void immutables()
Assert.assertEquals(SortedSets.mutable.of(1, 2, 3, 4, 5, 6, 7, 8), factory.ofSortedSet(SortedSets.immutable.of(1, 2, 3, 4, 5, 6, 7, 8).castToSortedSet()));
Assert.assertEquals(SortedSets.mutable.of(1, 2, 3, 4, 5, 6, 7, 8), factory.ofSortedSet(SortedSets.immutable.of(Comparators.reverseNaturalOrder(), 1, 2, 3, 4, 5, 6, 7, 8).castToSortedSet()));
Assert.assertEquals(SortedSets.mutable.empty(), factory.ofSortedSet(SortedSets.mutable.with()));
Assert.assertEquals(SortedSets.mutable.empty(Integer::compare), factory.ofSortedSet(SortedSets.mutable.with(Integer::compare)));
Assert.assertEquals(SortedSets.mutable.of(1, 2, 3, 4, 5, 6, 7, 8), factory.ofSortedSet(SortedSets.mutable.of(1, 2, 3, 4, 5, 6, 7, 8)));
Assert.assertEquals(SortedSets.mutable.of(1, 2, 3, 4, 5, 6, 7, 8), factory.ofSortedSet(SortedSets.mutable.of(Comparators.reverseNaturalOrder(), 1, 2, 3, 4, 5, 6, 7, 8)));
Assert.assertEquals(SortedSets.immutable.empty(), SortedSets.immutable.ofAll(null, Lists.mutable.empty()));
Expand Down

0 comments on commit 7586a56

Please sign in to comment.