Skip to content

Package: CollectionUtils

CollectionUtils

nameinstructionbranchcomplexitylinemethod
concat(List, List)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
concat(List, Object)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
head(List)
M: 0 C: 12
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
optionalHead(List)
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
reversed(List)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
tail(List)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Collections;
32: import java.util.List;
33: import java.util.Optional;
34: import lombok.AccessLevel;
35: import lombok.NoArgsConstructor;
36:
37: /***********************************************************************************************************************
38: *
39: * This class contains a bunch of utility methods for manipulating lists.
40: *
41: * @author Fabrizio Giudici
42: * @since 3.2-ALPHA-13
43: * @it.tidalwave.javadoc.stable
44: *
45: **********************************************************************************************************************/
46: @NoArgsConstructor(access = AccessLevel.PRIVATE)
47: public final class CollectionUtils
48: {
49: /*******************************************************************************************************************
50: *
51: * Appends a list to an object. The resulting list is mutable.
52: *
53: * @param list the list
54: * @param object the list to append
55: * @return the list with the appended object
56: *
57: * @it.tidalwave.javadoc.stable
58: *
59: ******************************************************************************************************************/
60: @Nonnull
61: public static <T> List<T> concat (@Nonnull final List<? extends T> list, @Nonnull final T object)
62: {
63: final List<T> result = new ArrayList<>(list);
64: result.add(object);
65: return result;
66: }
67:
68: /*******************************************************************************************************************
69: *
70: * Appends a list to another. The resulting list is mutable.
71: *
72: * @param list1 the former list
73: * @param list2 the latter list
74: * @return the list with the appended object
75: *
76: * @it.tidalwave.javadoc.stable
77: *
78: ******************************************************************************************************************/
79: @Nonnull
80: public static <T> List<T> concat (@Nonnull final List<? extends T> list1, @Nonnull List<? extends T> list2)
81: {
82: final List<T> result = new ArrayList<>(list1);
83: result.addAll(list2);
84: return result;
85: }
86:
87: /*******************************************************************************************************************
88: *
89: * Reverses a list. The resulting list is mutable.
90: *
91: * @param list the list
92: * @return the reversed list
93: *
94: * @it.tidalwave.javadoc.stable
95: *
96: ******************************************************************************************************************/
97: @Nonnull
98: public static <T> List<T> reversed (@Nonnull final List<? extends T> list)
99: {
100: final List<T> result = new ArrayList<>(list);
101: Collections.reverse(result);
102: return result;
103: }
104:
105: /*******************************************************************************************************************
106: *
107: * Returns the (optional) first element of a list.
108: *
109: * @param list the list
110: * @return the first element
111: *
112: * @it.tidalwave.javadoc.stable
113: *
114: ******************************************************************************************************************/
115: @Nonnull
116: public static <T> Optional<T> optionalHead (@Nonnull final List<? extends T> list)
117: {
118:• return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));
119: }
120:
121: /*******************************************************************************************************************
122: *
123: * Returns the first element of a list.
124: *
125: * @param list the list (cannot be empty)
126: * @return the first element
127: * @throws IllegalArgumentException if the list is empty
128: *
129: * @it.tidalwave.javadoc.stable
130: *
131: ******************************************************************************************************************/
132: @Nonnull
133: public static <T> T head (@Nonnull final List<? extends T> list)
134: {
135:• if (list.isEmpty())
136: {
137: throw new IllegalArgumentException("List is empty");
138: }
139:
140: return list.get(0);
141: }
142:
143: /*******************************************************************************************************************
144: *
145: * Returns the tail element of a list, that is a list without the first element. The tail of an empty list is an
146: * empty list. The resulting list is mutable.
147: *
148: * @param list the list
149: * @return the tail of the list
150: *
151: * @it.tidalwave.javadoc.stable
152: *
153: ******************************************************************************************************************/
154: @Nonnull
155: public static <T> List<T> tail (@Nonnull final List<? extends T> list)
156: {
157: return new ArrayList<>(list.subList(1, list.size()));
158: }
159: }