All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sir.wellington.alchemy.collections.lists.Lists Maven / Gradle / Ivy

There is a newer version: 2.1
Show newest version
/*
 * Copyright 2015 Aroma Tech.
 *
 * 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.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.sirwellington.alchemy.annotations.access.NonInstantiable;
import tech.sirwellington.alchemy.annotations.arguments.NonEmpty;
import tech.sirwellington.alchemy.annotations.arguments.Optional;
import tech.sirwellington.alchemy.annotations.arguments.Required;

import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;
import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;
import static tech.sirwellington.alchemy.arguments.assertions.CollectionAssertions.nonEmptyList;

/**
 * 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");
    }
    
    public static boolean isEmpty(@Optional Collection list)
    {
        return list == null || list.isEmpty();
    }

    /**
     * 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(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)
    {
        checkThat(list)
            .usingMessage("List cannot be empty")
            .is(notNull())
            .is(nonEmptyList());
        
        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 ? emptyList() : list;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy