Skip to content

Commit

Permalink
Merge pull request #4667 from cleishm/functions
Browse files Browse the repository at this point in the history
Add BiConsumer to neo4j-functions
  • Loading branch information
cleishm committed May 21, 2015
2 parents 9913b05 + 793e413 commit 72c7977
Show file tree
Hide file tree
Showing 40 changed files with 267 additions and 78 deletions.
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2002-2015 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.function;

/**
* Represents an operation that accepts two arguments and returns no result.
* Unlike most other functional interfaces, BiConsumer is expected to operate via side-effects.
*
* @param <T> the type of the input to the operation
*/
public interface BiConsumer<T, U> extends ThrowingBiConsumer<T,U,RuntimeException>
{
/**
* Performs this operation on the given arguments.
*
* @param t the first input argument
* @param u the second input argument
*/
void accept( T t, U u );
}
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2002-2015 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.function;

/**
* Constructors for basic {@link BiConsumer} types
*/
public final class BiConsumers
{
private static final BiConsumer<?,?> NOOP = new BiConsumer()
{
@Override
public void accept( Object t, Object u )
{
// noop
}
};

/**
* @param <T> The type to be consumed
* @return a {@link BiConsumers} that does nothing.
*/
@SuppressWarnings( "unchecked" )
public static <T, U> BiConsumer<T,U> noop()
{
return (BiConsumer<T,U>) NOOP;
}
}
Expand Up @@ -26,7 +26,14 @@
* @param <U> the type of the second argument to the function
* @param <R> the type of the result of the function
*/
public interface BiFunction<T, U, R> extends ThrowingBiFunction<T, U, R, RuntimeException>
public interface BiFunction<T, U, R> extends ThrowingBiFunction<T,U,R,RuntimeException>
{
/**
* Map a single item from one type to another
*
* @param t the first input item
* @param u the second input item
* @return the mapped item
*/
R apply( T t, U u );
}
Expand Up @@ -20,10 +20,11 @@
package org.neo4j.function;

/**
* Represents an operation on a single operand that produces a result of the same type as its operand. This is a specialization of {@link BiFunction} for the case where the operand and result are of the same type.
* Represents an operation on a single operand that produces a result of the same type as its operand. This is a specialization of {@link BiFunction} for the
* case where the operand and result are of the same type.
*
* @param <T> the type of the operand and result of the operator
*/
public interface BinaryOperator<T> extends BiFunction<T, T, T>, ThrowingBinaryOperator<T, RuntimeException>
public interface BinaryOperator<T> extends BiFunction<T,T,T>, ThrowingBinaryOperator<T,RuntimeException>
{
}
Expand Up @@ -20,11 +20,12 @@
package org.neo4j.function;

/**
* Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.
* Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate
* via side-effects.
*
* @param <T> the type of the input to the operation
*/
public interface Consumer<T> extends ThrowingConsumer<T, RuntimeException>
public interface Consumer<T> extends ThrowingConsumer<T,RuntimeException>
{
/**
* Performs this operation on the given argument.
Expand Down
Expand Up @@ -24,7 +24,8 @@
*/
public final class Consumers
{
private static final Consumer<?> NOOP = new Consumer() {
private static final Consumer<?> NOOP = new Consumer()
{
@Override
public void accept( Object value )
{
Expand All @@ -36,6 +37,7 @@ public void accept( Object value )
* @param <T> The type to be consumed
* @return a {@link Consumer} that does nothing.
*/
@SuppressWarnings( "unchecked" )
public static <T> Consumer<T> noop()
{
return (Consumer<T>) NOOP;
Expand Down
Expand Up @@ -21,7 +21,6 @@

/**
* A generic factory interface for creating instances that do not need any additional dependencies or parameters.
*
* If the implementation is not always creating new instances, you should probably use {@link Supplier}.
*
* @param <T> a new instance
Expand Down
Expand Up @@ -25,7 +25,7 @@
* @param <T> the type of the input to the function
* @param <R> the type of the result of the function
*/
public interface Function<T, R> extends ThrowingFunction<T, R, RuntimeException>, RawFunction<T, R, RuntimeException>
public interface Function<T, R> extends ThrowingFunction<T,R,RuntimeException>, RawFunction<T,R,RuntimeException>
{
/**
* Apply a value to this function
Expand Down
Expand Up @@ -23,6 +23,6 @@
* @deprecated use {@link BiFunction} instead
*/
@Deprecated
public interface Function2<T1, T2, R> extends BiFunction<T1, T2, R>
public interface Function2<T1, T2, R> extends BiFunction<T1,T2,R>
{
}
50 changes: 25 additions & 25 deletions community/function/src/main/java/org/neo4j/function/Functions.java
Expand Up @@ -26,9 +26,9 @@
*/
public final class Functions
{
public static <From, To> Function<From, To> map( final Map<From, To> map )
public static <From, To> Function<From,To> map( final Map<From,To> map )
{
return new Function<From, To>()
return new Function<From,To>()
{
@Override
public To apply( From from )
Expand All @@ -38,10 +38,10 @@ public To apply( From from )
};
}

public static <From, To> Function<From, To> withDefaults( final Function<From, To> defaults, final Function<From,
public static <From, To> Function<From,To> withDefaults( final Function<From,To> defaults, final Function<From,
To> f )
{
return new Function<From, To>()
return new Function<From,To>()
{
@Override
public To apply( From from )
Expand All @@ -60,9 +60,9 @@ public To apply( From from )
};
}

public static <From, To> Function<From, To> nullFunction()
public static <From, To> Function<From,To> nullFunction()
{
return new Function<From, To>()
return new Function<From,To>()
{
@Override
public To apply( From from )
Expand All @@ -72,9 +72,9 @@ public To apply( From from )
};
}

public static <From, To> Function<From, To> constant( final To value )
public static <From, To> Function<From,To> constant( final To value )
{
return new Function<From, To>()
return new Function<From,To>()
{
@Override
public To apply( From from )
Expand All @@ -93,20 +93,20 @@ public Object apply( Object value )
}
};

@SuppressWarnings("unchecked")
@SuppressWarnings( "unchecked" )
public static <T> UnaryOperator<T> identity()
{
return IDENTITY;
}

public static <From, From2, To> Function2<? super Function<From, From2>, ? super Function<From2, To>, Function<From, To>> compose()
public static <From, From2, To> Function2<? super Function<From,From2>,? super Function<From2,To>,Function<From,To>> compose()
{
return new Function2<Function<From, From2>, Function<From2, To>, Function<From, To>>()
return new Function2<Function<From,From2>,Function<From2,To>,Function<From,To>>()
{
@Override
public Function<From, To> apply( final Function<From, From2> f1, final Function<From2, To> f2 )
public Function<From,To> apply( final Function<From,From2> f1, final Function<From2,To> f2 )
{
return new Function<From, To>()
return new Function<From,To>()
{
@Override
public To apply( From from )
Expand All @@ -118,15 +118,15 @@ public To apply( From from )
};
}

public static <T1, T2> Function2<? super BiFunction<T1, T2, T1>, ? super BiFunction<T1, T2, T1>, Function2<T1, T2, T1>> compose2()
public static <T1, T2> Function2<? super BiFunction<T1,T2,T1>,? super BiFunction<T1,T2,T1>,Function2<T1,T2,T1>> compose2()
{
return new Function2<BiFunction<T1, T2, T1>, BiFunction<T1, T2, T1>, Function2<T1, T2, T1>>()
return new Function2<BiFunction<T1,T2,T1>,BiFunction<T1,T2,T1>,Function2<T1,T2,T1>>()
{
@Override
public Function2<T1, T2, T1> apply( final BiFunction<T1, T2, T1> function1, final BiFunction<T1, T2,
public Function2<T1,T2,T1> apply( final BiFunction<T1,T2,T1> function1, final BiFunction<T1,T2,
T1> function2 )
{
return new Function2<T1, T2, T1>()
return new Function2<T1,T2,T1>()
{
@Override
public T1 apply( T1 from1, T2 from2 )
Expand All @@ -138,12 +138,12 @@ public T1 apply( T1 from1, T2 from2 )
};
}

public static Function<Object, String> TO_STRING = new Function<Object, String>()
public static Function<Object,String> TO_STRING = new Function<Object,String>()
{
@Override
public String apply( Object from )
{
if (from != null)
if ( from != null )
{
return from.toString();
}
Expand All @@ -154,9 +154,9 @@ public String apply( Object from )
}
};

public static <FROM, TO> Function<FROM, TO> cast( final Class<TO> to )
public static <FROM, TO> Function<FROM,TO> cast( final Class<TO> to )
{
return new Function<FROM, TO>()
return new Function<FROM,TO>()
{
@Override
public TO apply( FROM from )
Expand All @@ -172,9 +172,9 @@ public String toString()
};
}

public static <T> Function<T, Void> fromConsumer( final Consumer<T> consumer )
public static <T> Function<T,Void> fromConsumer( final Consumer<T> consumer )
{
return new Function<T, Void>()
return new Function<T,Void>()
{
@Override
public Void apply( T t )
Expand All @@ -185,9 +185,9 @@ public Void apply( T t )
};
}

public static <T> Function<Void, T> fromSupplier( final Supplier<T> supplier )
public static <T> Function<Void,T> fromSupplier( final Supplier<T> supplier )
{
return new Function<Void, T>()
return new Function<Void,T>()
{
@Override
public T apply( Void t )
Expand Down
Expand Up @@ -21,6 +21,6 @@

import java.io.IOException;

public interface IOFunction<T, R> extends RawFunction<T, R, IOException>
public interface IOFunction<T, R> extends RawFunction<T,R,IOException>
{
}
Expand Up @@ -33,8 +33,8 @@ public Object apply( Object value )
}
};

@SuppressWarnings("unchecked")
public static <T> IOFunction<T, T> identity()
@SuppressWarnings( "unchecked" )
public static <T> IOFunction<T,T> identity()
{
return IDENTITY;
}
Expand Down
Expand Up @@ -24,7 +24,7 @@
*
* @param <R> the type of the result of the function
*/
public interface IntFunction<R> extends ThrowingIntFunction<R, RuntimeException>, org.neo4j.function.primitive.FunctionFromPrimitiveInt<R>
public interface IntFunction<R> extends ThrowingIntFunction<R,RuntimeException>, org.neo4j.function.primitive.FunctionFromPrimitiveInt<R>
{
/**
* Applies this function to the given argument.
Expand Down
Expand Up @@ -20,14 +20,15 @@
package org.neo4j.function;

/**
* Represents an operation upon two long-valued operands and producing a long-valued result. This is the primitive type specialization of {@link BinaryOperator} for long.
* Represents an operation upon two long-valued operands and producing a long-valued result. This is the primitive type specialization of {@link BinaryOperator}
* for long.
*/
public interface LongBinaryOperator extends ThrowingLongBinaryOperator<RuntimeException>
{
/**
* Applies this operator to the given operand.
*
* @param left the first operand
* @param left the first operand
* @param right the second operand
* @return the operator result
*/
Expand Down
Expand Up @@ -24,7 +24,7 @@
*
* @param <R> the type of the result of the function
*/
public interface LongFunction<R> extends ThrowingLongFunction<R, RuntimeException>, org.neo4j.function.primitive.FunctionFromPrimitiveLong<R>
public interface LongFunction<R> extends ThrowingLongFunction<R,RuntimeException>, org.neo4j.function.primitive.FunctionFromPrimitiveLong<R>
{
/**
* Applies this function to the given argument.
Expand Down
Expand Up @@ -20,7 +20,8 @@
package org.neo4j.function;

/**
* Represents an operation on a single long-valued operand that produces a long-valued result. This is the primitive type specialization of {@link UnaryOperator} for long.
* Represents an operation on a single long-valued operand that produces a long-valued result. This is the primitive type specialization of {@link
* UnaryOperator} for long.
*/
public interface LongUnaryOperator extends ThrowingLongUnaryOperator<RuntimeException>
{
Expand Down
Expand Up @@ -24,7 +24,7 @@
*
* @param <T> the type of the input to the predicate
*/
public interface Predicate<T> extends ThrowingPredicate<T, RuntimeException>
public interface Predicate<T> extends ThrowingPredicate<T,RuntimeException>
{
/**
* Evaluates this predicate on the given argument.
Expand Down

0 comments on commit 72c7977

Please sign in to comment.