From 106b2f7708a33a98e283997bf8155e0144f726ed Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 3 Apr 2020 09:14:57 +0100 Subject: [PATCH] Use list-head-tail (#64) The HeadTail interface has been extracted to its own project for reuse. --- pom.xml | 6 ++ src/main/java/net/kemitix/node/HeadTail.java | 62 ------------------- src/main/java/net/kemitix/node/NodeItem.java | 4 +- .../java/net/kemitix/node/HeadTailTest.java | 53 ---------------- 4 files changed, 8 insertions(+), 117 deletions(-) delete mode 100644 src/main/java/net/kemitix/node/HeadTail.java delete mode 100644 src/test/java/net/kemitix/node/HeadTailTest.java diff --git a/pom.xml b/pom.xml index b3a445e..15c7504 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,7 @@ 2.16 2.4.1 5.4.0 + 1.0.0 1.18.12 3.15.0 2.1.0 @@ -46,6 +47,11 @@ 2016 + + net.kemitix + list-head-tail + ${list-head-tail.version} + org.projectlombok lombok diff --git a/src/main/java/net/kemitix/node/HeadTail.java b/src/main/java/net/kemitix/node/HeadTail.java deleted file mode 100644 index 200454c..0000000 --- a/src/main/java/net/kemitix/node/HeadTail.java +++ /dev/null @@ -1,62 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2018 Paul Campbell - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies - * or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE - * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -package net.kemitix.node; - -import java.util.Collections; -import java.util.List; -import java.util.Optional; - -/** - * Provides a simple implementation of head() and tail() for {@link List}s. - */ -public interface HeadTail { - - /** - * Returns the first item in the list as an {@link Optional}. - * - * @param list the list - * @param the type of the lists contents - * @return an Optional containing the first item in the list, or empty if - * the list is empty. - */ - static Optional head(final List list) { - if (list.isEmpty()) { - return Optional.empty(); - } - return Optional.ofNullable(list.get(0)); - } - - /** - * Returns the list, minus the first item. - * - * @param list the list - * @param the type of the lists contents - * @return a view of the list starting with the second item, or an empty - * list if the original list has less than two items. - */ - static List tail(final List list) { - if (list.size() < 1) { - return Collections.emptyList(); - } - return list.subList(1, list.size()); - } -} diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index b705457..d5226b5 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -30,8 +30,8 @@ import java.util.*; import java.util.function.Function; import java.util.stream.Stream; -import static net.kemitix.node.HeadTail.head; -import static net.kemitix.node.HeadTail.tail; +import static net.kemitix.list.HeadTail.head; +import static net.kemitix.list.HeadTail.tail; /** * Represents a tree of nodes. diff --git a/src/test/java/net/kemitix/node/HeadTailTest.java b/src/test/java/net/kemitix/node/HeadTailTest.java deleted file mode 100644 index fc91686..0000000 --- a/src/test/java/net/kemitix/node/HeadTailTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package net.kemitix.node; - -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.UUID; - -import static net.kemitix.node.HeadTail.head; -import static net.kemitix.node.HeadTail.tail; -import static org.assertj.core.api.Assertions.assertThat; - -public class HeadTailTest { - - private String headValue = "1-" + UUID.randomUUID().toString(); - private String secondValue = "2-" + UUID.randomUUID().toString(); - private String thirdValue = "3-" + UUID.randomUUID().toString(); - private List emptyList = Collections.emptyList(); - private List singletonList = Collections.singletonList(headValue); - private List aList = Arrays.asList( - headValue, secondValue, thirdValue - ); - private List tailValue = Arrays.asList( - secondValue, thirdValue - ); - - @Test - public void headOfAnEmptyListIsEmpty() { - assertThat(head(emptyList)).isEmpty(); - } - @Test - public void headOfASingletonListIsTheItem() { - assertThat(head(singletonList)).contains(headValue); - } - @Test - public void headOfAListIsTheFirstItem() { - assertThat(head(aList)).contains(headValue); - } - - @Test - public void tailOfAnEmptyListIsEmpty() { - assertThat(tail(emptyList)).isEmpty(); - } - @Test - public void tailOfASingletonListIsEmpty() { - assertThat(tail(singletonList)).isEmpty(); - } - @Test - public void tailOfAListIsMinusTheHead() { - assertThat(tail(aList)).isEqualTo(tailValue); - } -} \ No newline at end of file