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

com.gs.collections.impl.block.function.MinSizeFunction Maven / Gradle / Ivy

/*
 * Copyright 2011 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.function;

import java.util.Collection;
import java.util.Map;

import com.gs.collections.api.block.function.Function2;

/**
 * MinSizeFunction compares the size of strings, collections, or maps to determine the min size.
 */
public final class MinSizeFunction
{
    public static final Function2 STRING = new MinSizeStringFunction();
    public static final Function2, Integer> COLLECTION = new MinSizeCollectionFunction();
    public static final Function2, Integer> MAP = new MinSizeMapFunction();

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

    private static class MinSizeStringFunction implements Function2
    {
        private static final long serialVersionUID = 1L;

        public Integer value(Integer previousMin, String s)
        {
            return Math.min(previousMin, s.length());
        }
    }

    private static class MinSizeCollectionFunction implements Function2, Integer>
    {
        private static final long serialVersionUID = 1L;

        public Integer value(Integer previousMin, Collection collection)
        {
            return Math.min(previousMin, collection.size());
        }
    }

    private static class MinSizeMapFunction implements Function2, Integer>
    {
        private static final long serialVersionUID = 1L;

        public Integer value(Integer previousMin, Map map)
        {
            return Math.min(previousMin, map.size());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy