-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- nextPermutation() added to MathUtilities
- size(), isEmpty(), and hasContent() added to CollectionUtilities
- Loading branch information
Showing
8 changed files
with
158 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,73 @@ | ||
package com.cedarsoftware.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author John DeRegnaucourt ([email protected]) | ||
* <br> | ||
* Copyright (c) Cedar Software LLC | ||
* <br><br> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <br><br> | ||
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a> | ||
* <br><br> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
public class CollectionUtilities { | ||
|
||
private static final Set<?> unmodifiableEmptySet = Collections.unmodifiableSet(new HashSet<>()); | ||
|
||
private static final List<?> unmodifiableEmptyList = Collections.unmodifiableList(new ArrayList<>()); | ||
|
||
/** | ||
* This is a null-safe isEmpty check. | ||
* | ||
* @param col Collection to check | ||
* @return true if empty or null | ||
*/ | ||
public static boolean isEmpty(Collection col) { | ||
return col == null || col.isEmpty(); | ||
} | ||
|
||
/** | ||
* This is a null-safe isEmpty check. | ||
* | ||
* @param col Collection to check | ||
* @return true if empty or null | ||
*/ | ||
public static boolean hasContent(Collection col) { | ||
return col != null && !col.isEmpty(); | ||
} | ||
|
||
/** | ||
* This is a null-safe size check. | ||
* | ||
* @param col Collection to check | ||
* @return true if empty or null | ||
*/ | ||
public static int size(Collection col) { | ||
return col == null ? 0 : col.size(); | ||
} | ||
|
||
/** | ||
* For JDK1.8 support. Remove this and change to List.of() for JDK11+ | ||
*/ | ||
@SafeVarargs | ||
public static <T> List<T> listOf(T... items) | ||
{ | ||
if (items == null || items.length ==0) | ||
{ | ||
return (List<T>)unmodifiableEmptyList; | ||
public static <T> List<T> listOf(T... items) { | ||
if (items == null || items.length == 0) { | ||
return (List<T>) unmodifiableEmptyList; | ||
} | ||
List<T> list = new ArrayList<>(); | ||
Collections.addAll(list, items); | ||
|
@@ -32,10 +78,8 @@ public static <T> List<T> listOf(T... items) | |
* For JDK1.8 support. Remove this and change to Set.of() for JDK11+ | ||
*/ | ||
@SafeVarargs | ||
public static <T> Set<T> setOf(T... items) | ||
{ | ||
if (items == null || items.length ==0) | ||
{ | ||
public static <T> Set<T> setOf(T... items) { | ||
if (items == null || items.length == 0) { | ||
return (Set<T>) unmodifiableEmptySet; | ||
} | ||
Set<T> set = new LinkedHashSet<>(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,40 @@ | ||
package com.cedarsoftware.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.cedarsoftware.util.CollectionUtilities.hasContent; | ||
import static com.cedarsoftware.util.CollectionUtilities.isEmpty; | ||
import static com.cedarsoftware.util.CollectionUtilities.listOf; | ||
import static com.cedarsoftware.util.CollectionUtilities.setOf; | ||
import static com.cedarsoftware.util.CollectionUtilities.size; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
/** | ||
* @author John DeRegnaucourt ([email protected]) | ||
* <br> | ||
* Copyright (c) Cedar Software LLC | ||
* <br><br> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <br><br> | ||
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a> | ||
* <br><br> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
class CollectionUtilitiesTests { | ||
static class Rec { | ||
final String s; | ||
|
@@ -28,35 +53,35 @@ static class Rec { | |
|
||
@Test | ||
void testListOf() { | ||
final List<String> list = CollectionUtilities.listOf(); | ||
final List<String> list = listOf(); | ||
assertEquals(0, list.size()); | ||
} | ||
|
||
@Test | ||
void testListOf_producesImmutableList() { | ||
final List<String> list = CollectionUtilities.listOf(); | ||
final List<String> list = listOf(); | ||
assertThatExceptionOfType(UnsupportedOperationException.class) | ||
.isThrownBy(() -> list.add("One")); | ||
} | ||
|
||
@Test | ||
void testListOfOne() { | ||
final List<String> list = CollectionUtilities.listOf("One"); | ||
final List<String> list = listOf("One"); | ||
assertEquals(1, list.size()); | ||
assertEquals("One", list.get(0)); | ||
} | ||
|
||
@Test | ||
void testListOfTwo() { | ||
final List<String> list = CollectionUtilities.listOf("One", "Two"); | ||
final List<String> list = listOf("One", "Two"); | ||
assertEquals(2, list.size()); | ||
assertEquals("One", list.get(0)); | ||
assertEquals("Two", list.get(1)); | ||
} | ||
|
||
@Test | ||
void testListOfThree() { | ||
final List<String> list = CollectionUtilities.listOf("One", "Two", "Three"); | ||
final List<String> list = listOf("One", "Two", "Three"); | ||
assertEquals(3, list.size()); | ||
assertEquals("One", list.get(0)); | ||
assertEquals("Two", list.get(1)); | ||
|
@@ -65,39 +90,68 @@ void testListOfThree() { | |
|
||
@Test | ||
void testSetOf() { | ||
final Set<?> set = CollectionUtilities.setOf(); | ||
final Set<?> set = setOf(); | ||
assertEquals(0, set.size()); | ||
} | ||
|
||
@Test | ||
void testSetOf_producesImmutableSet() { | ||
final Set<String> set = CollectionUtilities.setOf(); | ||
final Set<String> set = setOf(); | ||
assertThatExceptionOfType(UnsupportedOperationException.class) | ||
.isThrownBy(() -> set.add("One")); | ||
} | ||
|
||
|
||
@Test | ||
void testSetOfOne() { | ||
final Set<String> set = CollectionUtilities.setOf("One"); | ||
final Set<String> set = setOf("One"); | ||
assertEquals(1, set.size()); | ||
assertTrue(set.contains("One")); | ||
} | ||
|
||
@Test | ||
public void testSetOfTwo() { | ||
final Set<String> set = CollectionUtilities.setOf("One", "Two"); | ||
void testSetOfTwo() { | ||
final Set<String> set = setOf("One", "Two"); | ||
assertEquals(2, set.size()); | ||
assertTrue(set.contains("One")); | ||
assertTrue(set.contains("Two")); | ||
} | ||
|
||
@Test | ||
public void testSetOfThree() { | ||
final Set<String> set = CollectionUtilities.setOf("One", "Two", "Three"); | ||
void testSetOfThree() { | ||
final Set<String> set = setOf("One", "Two", "Three"); | ||
assertEquals(3, set.size()); | ||
assertTrue(set.contains("One")); | ||
assertTrue(set.contains("Two")); | ||
assertTrue(set.contains("Three")); | ||
} | ||
|
||
@Test | ||
void testIsEmpty() { | ||
assertTrue(isEmpty(null)); | ||
assertTrue(isEmpty(new ArrayList<>())); | ||
assertTrue(isEmpty(new HashSet<>())); | ||
assertFalse(isEmpty(setOf("one"))); | ||
assertFalse(isEmpty(listOf("one"))); | ||
} | ||
|
||
@Test | ||
void testHasContent() { | ||
assertFalse(hasContent(null)); | ||
assertFalse(hasContent(new ArrayList<>())); | ||
assertFalse(hasContent(new HashSet<>())); | ||
assertTrue(hasContent(setOf("one"))); | ||
assertTrue(hasContent(listOf("one"))); | ||
} | ||
|
||
@Test | ||
void testSize() { | ||
assertEquals(0, size(null)); | ||
assertEquals(0, size(new ArrayList<>())); | ||
assertEquals(0, size(new HashSet<>())); | ||
assertEquals(1, size(setOf("one"))); | ||
assertEquals(1, size(listOf("one"))); | ||
assertEquals(2, size(setOf("one", "two"))); | ||
assertEquals(2, size(listOf("one", "two"))); | ||
} | ||
} |