sir.wellington.alchemy.collections.lists.Lists Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alchemy-collections Show documentation
Show all versions of alchemy-collections Show documentation
Collect Yo'Self!
Part of the Alchemy Collection.
The newest version!
/*
* Copyright © 2019. Sir Wellington.
* 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
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package sir.wellington.alchemy.collections.lists;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.sirwellington.alchemy.annotations.access.NonInstantiable;
import tech.sirwellington.alchemy.annotations.arguments.Optional;
import tech.sirwellington.alchemy.annotations.arguments.*;
import tech.sirwellington.alchemy.arguments.assertions.Assertions;
import tech.sirwellington.alchemy.arguments.assertions.CollectionAssertions;
import static tech.sirwellington.alchemy.arguments.Arguments.*;
/**
* Operations built around {@linkplain List Lists}.
*
* @author SirWellington
*/
@NonInstantiable
public final class Lists
{
private final static Logger LOG = LoggerFactory.getLogger(Lists.class);
Lists() throws IllegalAccessException
{
throw new IllegalAccessException("cannot instantiate this class");
}
/**
* Determines whether the specified collection is empty or not.
* @param list
* @return
*/
public static boolean isEmpty(@Optional Collection> list)
{
return list == null || list.isEmpty();
}
/**
* Determines whether the specified collection is not empty.
*
* @param list
* @return
*/
public static boolean notEmpty(@Optional Collection> list)
{
return !isEmpty(list);
}
/**
* Creates an {@link ArrayList}.
*
* @param
*
* @return
*/
public static List create()
{
return new ArrayList<>();
}
public static List createFrom(@Required E first, @Optional E... rest)
{
checkThat(first)
.usingMessage("missing first value")
.is(Assertions.notNull());
List list = Lists.create();
list.add(first);
if (rest != null)
{
list.addAll(Arrays.asList(rest));
}
return list;
}
/**
* Creates a copy of the
* @param
* @param collection
* @return
*/
public static List copy(@Optional Collection collection)
{
List list = create();
if (isEmpty(collection))
{
return list;
}
list.addAll(collection);
return list;
}
/**
* Creates a new Empty List that is immutable, so no new values
* can be added to it.
*
* @param
* @return
*/
public static List emptyList()
{
List list = create();
return Collections.unmodifiableList(list);
}
public static List toList(Collection set)
{
return copy(set);
}
public static E oneOf(@NonEmpty List list)
{
checkListNotEmpty(list);
Random random = new Random();
int index = random.nextInt(list.size());
return list.get(index);
}
public static List combine(@Optional List first, @Optional List...additional)
{
List result = Lists.create();
if (!isEmpty(first))
{
result.addAll(first);
}
if (additional != null)
{
for (List list : additional)
{
if (!isEmpty(list))
{
result.addAll(list);
}
}
}
return result;
}
public static List nullToEmpty(@Optional List list)
{
return list == null ? Lists.emptyList() : list;
}
public static List immutableCopyOf(@Optional List list)
{
if (isEmpty(list))
{
return emptyList();
}
return Collections.unmodifiableList(list);
}
/**
* Gets the first element in the list.
*
* @param
* @param list Cannot be null or empty;
* @return
* @throws IllegalArgumentException If the list is empty
*/
public static E first(@NonEmpty List list) throws IllegalArgumentException
{
checkListNotEmpty(list);
return list.get(0);
}
/**
* Gets the last element in the list.
*
* @param
* @param list Cannot be null or empty.
* @return
* @throws IllegalArgumentException If the list is empty
*/
public static E last(@NonEmpty List list) throws IllegalArgumentException
{
checkListNotEmpty(list);
int lastIndex = list.size() - 1;
return list.get(lastIndex);
}
/**
* Removes and retrieves the first element in the list.
*
* @param
* @param list Cannot be null or empty.
* @return
* @throws IllegalArgumentException If the list is empty.
*/
public static E removeFirst(@NonEmpty List list) throws IllegalArgumentException
{
checkListNotEmpty(list);
return list.remove(0);
}
/**
* Removes and retrieves the last element in the list.
*
* @param
* @param list Cannot be null or empty.
* @return
* @throws IllegalArgumentException If the list is empty.
*/
public static E removeLast(@NonEmpty List list) throws IllegalArgumentException
{
checkListNotEmpty(list);
int lastIndex = list.size() - 1;
return list.remove(lastIndex);
}
static void checkListNotEmpty(@NonEmpty List list)
{
checkThat(list)
.usingMessage("list cannot be null")
.is(Assertions.>notNull())
.usingMessage("list cannot be empty")
.is(CollectionAssertions.nonEmptyList());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy