com.gs.collections.impl.block.factory.Procedures Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gs-collections Show documentation
Show all versions of gs-collections Show documentation
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.
/*
* 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 super T> objectIntProcedure)
{
return Procedures.fromObjectIntProcedure(objectIntProcedure);
}
public static Procedure fromObjectIntProcedure(ObjectIntProcedure super T> objectIntProcedure)
{
return new ObjectIntProcedureAdapter(objectIntProcedure);
}
public static Procedure ifTrue(Predicate super T> predicate, Procedure super T> block)
{
return new IfProcedure(predicate, block);
}
public static Procedure ifElse(
Predicate super T> predicate,
Procedure super T> trueProcedure,
Procedure super T> falseProcedure)
{
return new IfProcedure(predicate, trueProcedure, falseProcedure);
}
public static CaseProcedure caseDefault(Procedure super T> defaultProcedure)
{
return new CaseProcedure(defaultProcedure);
}
public static CaseProcedure caseDefault(
Procedure super T> defaultProcedure,
Predicate super T> predicate,
Procedure super T> procedure)
{
return Procedures.caseDefault(defaultProcedure).addCase(predicate, procedure);
}
public static Procedure synchronizedEach(Procedure procedure)
{
return new Procedures.SynchronizedProcedure(procedure);
}
public static Procedure bind(Procedure2 super T, ? super P> 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 super T> objectIntProcedure;
private ObjectIntProcedureAdapter(ObjectIntProcedure super T> 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 super T, ? super P> procedure;
private final P parameter;
private BindProcedure(Procedure2 super T, ? super P> 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