Use list-head-tail (#64)
The HeadTail interface has been extracted to its own project for reuse.
This commit is contained in:
parent
c2b4bcddba
commit
106b2f7708
4 changed files with 8 additions and 117 deletions
6
pom.xml
6
pom.xml
|
@ -22,6 +22,7 @@
|
||||||
<tiles-maven-plugin.version>2.16</tiles-maven-plugin.version>
|
<tiles-maven-plugin.version>2.16</tiles-maven-plugin.version>
|
||||||
<kemitix-maven-tiles.version>2.4.1</kemitix-maven-tiles.version>
|
<kemitix-maven-tiles.version>2.4.1</kemitix-maven-tiles.version>
|
||||||
<kemitix-checkstyle.version>5.4.0</kemitix-checkstyle.version>
|
<kemitix-checkstyle.version>5.4.0</kemitix-checkstyle.version>
|
||||||
|
<list-head-tail.version>1.0.0</list-head-tail.version>
|
||||||
<lombok.version>1.18.12</lombok.version>
|
<lombok.version>1.18.12</lombok.version>
|
||||||
<assertj.version>3.15.0</assertj.version>
|
<assertj.version>3.15.0</assertj.version>
|
||||||
<trajano-commons-testing.version>2.1.0</trajano-commons-testing.version>
|
<trajano-commons-testing.version>2.1.0</trajano-commons-testing.version>
|
||||||
|
@ -46,6 +47,11 @@
|
||||||
<inceptionYear>2016</inceptionYear>
|
<inceptionYear>2016</inceptionYear>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.kemitix</groupId>
|
||||||
|
<artifactId>list-head-tail</artifactId>
|
||||||
|
<version>${list-head-tail.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
|
|
|
@ -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 <X> the type of the lists contents
|
|
||||||
* @return an Optional containing the first item in the list, or empty if
|
|
||||||
* the list is empty.
|
|
||||||
*/
|
|
||||||
static <X> Optional<X> head(final List<X> 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 <X> 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 <X> List<X> tail(final List<X> list) {
|
|
||||||
if (list.size() < 1) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
return list.subList(1, list.size());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -30,8 +30,8 @@ import java.util.*;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static net.kemitix.node.HeadTail.head;
|
import static net.kemitix.list.HeadTail.head;
|
||||||
import static net.kemitix.node.HeadTail.tail;
|
import static net.kemitix.list.HeadTail.tail;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a tree of nodes.
|
* Represents a tree of nodes.
|
||||||
|
|
|
@ -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<String> emptyList = Collections.emptyList();
|
|
||||||
private List<String> singletonList = Collections.singletonList(headValue);
|
|
||||||
private List<String> aList = Arrays.asList(
|
|
||||||
headValue, secondValue, thirdValue
|
|
||||||
);
|
|
||||||
private List<String> 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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue