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

com.gs.collections.impl.block.factory.Functions0 Maven / Gradle / Ivy

Go to download

GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map implementations with a rich API and set of utility classes that work with any JDK compatible Collections, Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.

There is a newer version: 7.0.3
Show newest version
/*
 * Copyright 2015 Goldman Sachs.
 *
 * 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 com.gs.collections.impl.block.factory;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.gs.collections.api.bag.MutableBag;
import com.gs.collections.api.block.function.Function0;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.impl.block.function.PassThruFunction0;
import com.gs.collections.impl.block.function.checked.CheckedFunction0;
import com.gs.collections.impl.block.function.checked.ThrowingFunction0;
import com.gs.collections.impl.factory.Bags;
import com.gs.collections.impl.factory.Lists;
import com.gs.collections.impl.factory.Maps;
import com.gs.collections.impl.factory.Sets;

public final class Functions0
{
    private static final TrueFunction TRUE_FUNCTION = new TrueFunction();
    private static final FalseFunction FALSE_FUNCTION = new FalseFunction();
    private static final NewFastListFunction NEW_FAST_LIST_FUNCTION = new NewFastListFunction();
    private static final NewUnifiedSetFunction NEW_UNIFIED_SET_FUNCTION = new NewUnifiedSetFunction();
    private static final NewHashBagFunction NEW_HASH_BAG_FUNCTION = new NewHashBagFunction();
    private static final NewUnifiedMapFunction NEW_UNIFIED_MAP_FUNCTION = new NewUnifiedMapFunction();
    private static final NullFunction NULL_FUNCTION = new NullFunction();
    private static final AtomicIntegerZeroFunction ATOMIC_INTEGER_ZERO = new AtomicIntegerZeroFunction();
    private static final AtomicLongZeroFunction ATOMIC_LONG_ZERO = new AtomicLongZeroFunction();
    private static final IntegerZeroFunction INTEGER_ZERO = new IntegerZeroFunction();
    private static final BigDecimalZeroFunction BIG_DECIMAL_ZERO = new BigDecimalZeroFunction();
    private static final BigIntegerZeroFunction BIG_INTEGER_ZERO = new BigIntegerZeroFunction();

    private Functions0()
    {
        throw new AssertionError("Suppress default constructor for noninstantiability");
    }

    /**
     * @since 6.0
     */
    public static Function0 getTrue()
    {
        return TRUE_FUNCTION;
    }

    /**
     * @since 6.0
     */
    public static Function0 getFalse()
    {
        return FALSE_FUNCTION;
    }

    public static  Function0> newFastList()
    {
        return (Function0>) (Function0) NEW_FAST_LIST_FUNCTION;
    }

    public static  Function0> newUnifiedSet()
    {
        return (Function0>) (Function0) NEW_UNIFIED_SET_FUNCTION;
    }

    public static  Function0> newHashBag()
    {
        return (Function0>) (Function0) NEW_HASH_BAG_FUNCTION;
    }

    public static  Function0> newUnifiedMap()
    {
        return (Function0>) (Function0) NEW_UNIFIED_MAP_FUNCTION;
    }

    public static  Function0 throwing(ThrowingFunction0 throwingFunction0)
    {
        return new ThrowingFunction0Adapter(throwingFunction0);
    }

    public static  Function0 nullValue()
    {
        return (Function0) NULL_FUNCTION;
    }

    public static  Function0 value(T t)
    {
        return new PassThruFunction0(t);
    }

    public static Function0 zeroInteger()
    {
        return INTEGER_ZERO;
    }

    public static Function0 zeroAtomicInteger()
    {
        return ATOMIC_INTEGER_ZERO;
    }

    public static Function0 zeroAtomicLong()
    {
        return ATOMIC_LONG_ZERO;
    }

    /**
     * @since 6.0
     */
    public static Function0 zeroBigDecimal()
    {
        return BIG_DECIMAL_ZERO;
    }

    /**
     * @since 6.0
     */
    public static Function0 zeroBigInteger()
    {
        return BIG_INTEGER_ZERO;
    }

    private static final class NewFastListFunction implements Function0>
    {
        private static final long serialVersionUID = 1L;

        public MutableList value()
        {
            return Lists.mutable.empty();
        }
    }

    private static final class NewUnifiedMapFunction implements Function0>
    {
        private static final long serialVersionUID = 1L;

        public MutableMap value()
        {
            return Maps.mutable.empty();
        }
    }

    private static final class NewUnifiedSetFunction implements Function0>
    {
        private static final long serialVersionUID = 1L;

        public MutableSet value()
        {
            return Sets.mutable.empty();
        }
    }

    private static final class NewHashBagFunction implements Function0>
    {
        private static final long serialVersionUID = 1L;

        public MutableBag value()
        {
            return Bags.mutable.empty();
        }
    }

    private static final class NullFunction implements Function0
    {
        private static final long serialVersionUID = 1L;

        public T value()
        {
            return null;
        }
    }

    private static final class IntegerZeroFunction implements Function0
    {
        private static final long serialVersionUID = 1L;

        public Integer value()
        {
            return Integer.valueOf(0);
        }
    }

    private static final class AtomicIntegerZeroFunction implements Function0
    {
        private static final long serialVersionUID = 1L;

        public AtomicInteger value()
        {
            return new AtomicInteger(0);
        }
    }

    private static final class AtomicLongZeroFunction implements Function0
    {
        private static final long serialVersionUID = 1L;

        public AtomicLong value()
        {
            return new AtomicLong(0);
        }
    }

    private static final class ThrowingFunction0Adapter extends CheckedFunction0
    {
        private static final long serialVersionUID = 1L;
        private final ThrowingFunction0 throwingFunction0;

        private ThrowingFunction0Adapter(ThrowingFunction0 throwingFunction0)
        {
            this.throwingFunction0 = throwingFunction0;
        }

        public T safeValue() throws Exception
        {
            return this.throwingFunction0.safeValue();
        }
    }

    private static final class BigDecimalZeroFunction implements Function0
    {
        private static final long serialVersionUID = 1L;

        public BigDecimal value()
        {
            return BigDecimal.ZERO;
        }
    }

    private static final class BigIntegerZeroFunction implements Function0
    {
        private static final long serialVersionUID = 1L;

        public BigInteger value()
        {
            return BigInteger.ZERO;
        }
    }

    private static final class TrueFunction implements Function0
    {
        private static final long serialVersionUID = 1L;

        public Boolean value()
        {
            return Boolean.TRUE;
        }
    }

    private static final class FalseFunction implements Function0
    {
        private static final long serialVersionUID = 1L;

        public Boolean value()
        {
            return Boolean.FALSE;
        }
    }
}