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

com.gs.collections.impl.block.factory.Procedures 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 2014 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.io.IOException;
import java.io.PrintStream;

import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.block.procedure.Procedure2;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.impl.block.procedure.CaseProcedure;
import com.gs.collections.impl.block.procedure.IfProcedure;
import com.gs.collections.impl.block.procedure.checked.CheckedProcedure;
import com.gs.collections.impl.block.procedure.checked.ThrowingProcedure;

/**
 * Factory class for commonly used procedures.
 */
public final class Procedures
{
    private Procedures()
    {
        throw new AssertionError("Suppress default constructor for noninstantiability");
    }

    /**
     * Allows a Java 8 lambda and method to be used in a forEach method without requiring a cast.
     */
    public static  Procedure cast(Procedure procedure)
    {
        return procedure;
    }

    public static  Procedure println(PrintStream stream)
    {
        return new PrintlnProcedure(stream);
    }

    public static  Procedure append(Appendable appendable)
    {
        return new AppendProcedure(appendable);
    }

    public static  Procedure throwing(ThrowingProcedure throwingProcedure)
    {
        return new ThrowingProcedureAdapter(throwingProcedure);
    }

    /**
     * @deprecated since 1.2 - Inlineable
     */
    @Deprecated
    public static  Procedure fromProcedureWithInt(ObjectIntProcedure objectIntProcedure)
    {
        return Procedures.fromObjectIntProcedure(objectIntProcedure);
    }

    public static  Procedure fromObjectIntProcedure(ObjectIntProcedure objectIntProcedure)
    {
        return new ObjectIntProcedureAdapter(objectIntProcedure);
    }

    public static  Procedure ifTrue(Predicate predicate, Procedure block)
    {
        return new IfProcedure(predicate, block);
    }

    public static  Procedure ifElse(
            Predicate predicate,
            Procedure trueProcedure,
            Procedure falseProcedure)
    {
        return new IfProcedure(predicate, trueProcedure, falseProcedure);
    }

    public static  CaseProcedure caseDefault(Procedure defaultProcedure)
    {
        return new CaseProcedure(defaultProcedure);
    }

    public static  CaseProcedure caseDefault(
            Procedure defaultProcedure,
            Predicate predicate,
            Procedure procedure)
    {
        return Procedures.caseDefault(defaultProcedure).addCase(predicate, procedure);
    }

    public static  Procedure synchronizedEach(Procedure procedure)
    {
        return new Procedures.SynchronizedProcedure(procedure);
    }

    public static  Procedure bind(Procedure2 procedure, P parameter)
    {
        return new BindProcedure(procedure, parameter);
    }

    private static final class PrintlnProcedure implements Procedure
    {
        private static final long serialVersionUID = 1L;

        private final PrintStream stream;

        private PrintlnProcedure(PrintStream stream)
        {
            this.stream = stream;
        }

        public void value(T each)
        {
            this.stream.println(each);
        }
    }

    private static final class AppendProcedure implements Procedure
    {
        private static final long serialVersionUID = 1L;

        private final Appendable appendable;

        private AppendProcedure(Appendable appendable)
        {
            this.appendable = appendable;
        }

        public void value(T each)
        {
            try
            {
                this.appendable.append(String.valueOf(each));
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
        }

        @Override
        public String toString()
        {
            return this.appendable.toString();
        }
    }

    private static final class ObjectIntProcedureAdapter implements Procedure
    {
        private static final long serialVersionUID = 2L;
        private int count;
        private final ObjectIntProcedure objectIntProcedure;

        private ObjectIntProcedureAdapter(ObjectIntProcedure objectIntProcedure)
        {
            this.objectIntProcedure = objectIntProcedure;
        }

        public void value(T each)
        {
            this.objectIntProcedure.value(each, this.count);
            this.count++;
        }
    }

    public static final class SynchronizedProcedure implements Procedure
    {
        private static final long serialVersionUID = 1L;
        private final Procedure procedure;

        private SynchronizedProcedure(Procedure procedure)
        {
            this.procedure = procedure;
        }

        public void value(T each)
        {
            if (each == null)
            {
                this.procedure.value(null);
            }
            else
            {
                synchronized (each)
                {
                    this.procedure.value(each);
                }
            }
        }
    }

    private static final class BindProcedure implements Procedure
    {
        private static final long serialVersionUID = 1L;
        private final Procedure2 procedure;
        private final P parameter;

        private BindProcedure(Procedure2 procedure, P parameter)
        {
            this.procedure = procedure;
            this.parameter = parameter;
        }

        public void value(T each)
        {
            this.procedure.value(each, this.parameter);
        }
    }

    private static final class ThrowingProcedureAdapter extends CheckedProcedure
    {
        private static final long serialVersionUID = 1L;
        private final ThrowingProcedure throwingProcedure;

        private ThrowingProcedureAdapter(ThrowingProcedure throwingProcedure)
        {
            this.throwingProcedure = throwingProcedure;
        }

        public void safeValue(T object) throws Exception
        {
            this.throwingProcedure.safeValue(object);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy