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

tech.sirwellington.alchemy.arguments.assertions.CollectionAssertions Maven / Gradle / Ivy

Go to download

Part of the Alchemy Collection. Easy, Simple, and Robust argument checking logic for your Services, Libraries, and Scripts.

There is a newer version: 2.3
Show newest version
/*
 * Copyright 2015 SirWellington 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 tech.sirwellington.alchemy.arguments.assertions;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
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.NonNull;
import tech.sirwellington.alchemy.arguments.AlchemyAssertion;
import tech.sirwellington.alchemy.arguments.Checks;
import tech.sirwellington.alchemy.arguments.FailedAssertionException;

import static java.lang.String.format;
import static tech.sirwellington.alchemy.arguments.Checks.Internal.checkNotNull;
import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;

/**
 *
 * @author SirWellington
 */
@NonInstantiable
public final class CollectionAssertions
{

    private final static Logger LOG = LoggerFactory.getLogger(CollectionAssertions.class);

    CollectionAssertions() throws IllegalAccessException
    {
        throw new IllegalAccessException("cannot instantiate");
    }

    /**
     * Asserts that the collection is not null and not empty.
     *
     * @param 
     *
     * @return
     */
    public static  AlchemyAssertion> nonEmptyCollection()
    {
        return (collection) ->
        {
            notNull().check(collection);

            if (collection.isEmpty())
            {
                throw new FailedAssertionException("Collection is empty");
            }
        };
    }

    /**
     * Asserts that the List is not null and not empty
     *
     * @param 
     *
     * @return
     */
    public static  AlchemyAssertion> nonEmptyList()
    {
        return (list) ->
        {
            notNull().check(list);

            if (list.isEmpty())
            {
                throw new FailedAssertionException("List is empty");
            }
        };

    }
    
    /**
     * Asserts that the Set is not null and not empty.
     * @param 
     * 
     * @return 
     */
    public static  AlchemyAssertion> nonEmptySet()
    {
        return set ->
        {
            notNull().check(set);
            
            if(set.isEmpty())
            {
                throw new FailedAssertionException("Set is empty");
            }
        };
    }

    /**
     * Asserts that the Map is not null and not empty
     *
     * @param 
     * @param 
     *
     * @return
     */
    public static  AlchemyAssertion> nonEmptyMap()
    {
        return (map) ->
        {
            notNull().check(map);

            if (map.isEmpty())
            {
                throw new FailedAssertionException("Map is empty");
            }

        };
    }

    public static  AlchemyAssertion nonEmptyArray()
    {
        return (array) ->
        {
            notNull().check(array);

            if (array.length == 0)
            {
                throw new FailedAssertionException("Array is empty");
            }
        };
    }
   
    public static  AlchemyAssertion> emptyCollection()
    {
        return collection ->
        {
            notNull().check(collection);
            
            if(!collection.isEmpty())
            {
                throw new FailedAssertionException(format("Expected an empty collection, but it has size [%s]",
                                                          collection.size()));
            }
        };
    }
    
    public static  AlchemyAssertion> emptyList()
    {
        return list ->
        {
            CollectionAssertions.emptyCollection().check(list);
        };
    }
    
    public static  AlchemyAssertion> emptySet()
    {
        return set ->
        {
            CollectionAssertions.emptyCollection().check(set);
        };
    }

    public static  AlchemyAssertion> listContaining(@NonNull E element) throws IllegalArgumentException
    {
        checkNotNull(element, "cannot check for null");
        
        return list ->
        {
            notNull().check(list);
            if (!list.contains(element))
            {
                throw new FailedAssertionException(element + " not found in List");
            }
        };
    }

    public static  AlchemyAssertion> mapWithKey(@NonNull K key) throws IllegalArgumentException
    {
        checkNotNull(key, "key cannot be null");

        return map ->
        {
            notNull().check(map);

            if (!map.containsKey(key))
            {
                throw new FailedAssertionException(format("Expected Key %s in Map", key));
            }
        };
    }

    public static  AlchemyAssertion> mapWithKeyValue(@NonNull K key, V value) throws IllegalArgumentException
    {
        checkNotNull(key, "key cannot be null");

        return map ->
        {
            CollectionAssertions.mapWithKey(key)
                    .check(map);

            V valueInMap = map.get(key);

            if (!Objects.equals(value, valueInMap))
            {
                throw new FailedAssertionException(format("Value in Map [%s] does not match expcted value %s", valueInMap, value));
            }

        };
    }
    
    public static  AlchemyAssertion keyInMap(Map map) throws IllegalArgumentException
    {
        checkNotNull(map, "map cannot be null");
        
        Consumer failAssertion = key ->
        {
            throw new FailedAssertionException(format("Expected key [%s] to be in map", key));
        };
        
        return key ->
        {
            notNull().check(key);
            
            if (!map.containsKey(key))
            {
                failAssertion.accept(key);
            }
        };
    }
    
    public static  AlchemyAssertion valueInMap(Map map) throws IllegalArgumentException
    {
        checkNotNull(map, "map cannot be null");
        
        Consumer failAssertion = key ->
        {
            throw new FailedAssertionException(format("Expected value [%s] to be in map", key));
        };
        
        return value ->
        {
            notNull().check(value);
            
            if (!map.containsValue(value))
            {
                failAssertion.accept(value);
            }
        };
    }
    
    public static  AlchemyAssertion elementInCollection(@NonEmpty Collection collection) throws IllegalArgumentException
    {
        checkNotNull(collection, "collection cannot be null");
        
        return element ->
        {
            notNull().check(element);
            
            if (!collection.contains(element))
            {
                throw new FailedAssertionException(format("Expected element [%s] to be in collection", element));
            }
        };
    }
    
    public static  AlchemyAssertion collectionOfSize(int size) throws IllegalArgumentException
    {
        Checks.Internal.checkThat(size >= 0, "size must be >= 0");
        
        return collection ->
        {
            CollectionAssertions.nonEmptyCollection().check(collection);
            
            int actualSize = collection.size();
            
            if(actualSize != size)
            {
                throw new FailedAssertionException(format("Expected collection with size [%s] but is instead [%s]",
                                                          size, 
                                                          actualSize));
            }
        };
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy