├── .gitignore ├── .hgignore ├── LICENSE.txt ├── README.md ├── build.gradle ├── funk-core ├── build.gradle └── src │ ├── main │ └── java │ │ └── org │ │ └── javafunk │ │ └── funk │ │ ├── Accumulators.java │ │ ├── Arrays.java │ │ ├── BigDecimals.java │ │ ├── BigIntegers.java │ │ ├── Checks.java │ │ ├── Classes.java │ │ ├── Doubles.java │ │ ├── Eagerly.java │ │ ├── Eithers.java │ │ ├── Exceptions.java │ │ ├── Floats.java │ │ ├── Generators.java │ │ ├── Integers.java │ │ ├── Iterables.java │ │ ├── Iterators.java │ │ ├── Lazily.java │ │ ├── Literals.java │ │ ├── Longs.java │ │ ├── Mappers.java │ │ ├── Maps.java │ │ ├── Multisets.java │ │ ├── Numbers.java │ │ ├── Objects.java │ │ ├── Options.java │ │ ├── Predicates.java │ │ ├── Sequences.java │ │ ├── Sets.java │ │ ├── Strings.java │ │ ├── Tuples.java │ │ ├── UnaryFunctions.java │ │ ├── annotations │ │ ├── Alpha.java │ │ ├── Beta.java │ │ └── ToDo.java │ │ ├── behaviours │ │ ├── Describable.java │ │ ├── Generatable.java │ │ ├── Generator.java │ │ ├── Mappable.java │ │ ├── Value.java │ │ └── ordinals │ │ │ ├── Eighth.java │ │ │ ├── Fifth.java │ │ │ ├── First.java │ │ │ ├── Fourth.java │ │ │ ├── Ninth.java │ │ │ ├── Second.java │ │ │ ├── Seventh.java │ │ │ ├── Sixth.java │ │ │ ├── Third.java │ │ │ └── mappables │ │ │ ├── MappableEighth.java │ │ │ ├── MappableFifth.java │ │ │ ├── MappableFirst.java │ │ │ ├── MappableFourth.java │ │ │ ├── MappableNinth.java │ │ │ ├── MappableSecond.java │ │ │ ├── MappableSeventh.java │ │ │ ├── MappableSixth.java │ │ │ └── MappableThird.java │ │ ├── builders │ │ ├── AbstractBuilder.java │ │ ├── ArrayBuilder.java │ │ ├── CollectionBuilder.java │ │ ├── IterableBuilder.java │ │ ├── IteratorBuilder.java │ │ ├── ListBuilder.java │ │ ├── MapBuilder.java │ │ ├── MultisetBuilder.java │ │ └── SetBuilder.java │ │ ├── datastructures │ │ ├── IntegerRange.java │ │ ├── Range.java │ │ └── tuples │ │ │ ├── AbstractTuple.java │ │ │ ├── Nonuple.java │ │ │ ├── Octuple.java │ │ │ ├── Pair.java │ │ │ ├── Quadruple.java │ │ │ ├── Quintuple.java │ │ │ ├── Septuple.java │ │ │ ├── Sextuple.java │ │ │ ├── Single.java │ │ │ └── Triple.java │ │ ├── functors │ │ ├── Action.java │ │ ├── Equivalence.java │ │ ├── Factory.java │ │ ├── Indexer.java │ │ ├── Mapper.java │ │ ├── Predicate.java │ │ ├── Reducer.java │ │ ├── adapters │ │ │ └── CallableNullaryFunctionAdapter.java │ │ ├── functions │ │ │ ├── BinaryFunction.java │ │ │ ├── NonaryFunction.java │ │ │ ├── NullaryFunction.java │ │ │ ├── OctaryFunction.java │ │ │ ├── QuaternaryFunction.java │ │ │ ├── QuinaryFunction.java │ │ │ ├── SenaryFunction.java │ │ │ ├── SeptenaryFunction.java │ │ │ ├── TernaryFunction.java │ │ │ └── UnaryFunction.java │ │ ├── predicates │ │ │ ├── BinaryPredicate.java │ │ │ ├── NonaryPredicate.java │ │ │ ├── NullaryPredicate.java │ │ │ ├── OctaryPredicate.java │ │ │ ├── QuaternaryPredicate.java │ │ │ ├── QuinaryPredicate.java │ │ │ ├── SenaryPredicate.java │ │ │ ├── SeptenaryPredicate.java │ │ │ ├── TernaryPredicate.java │ │ │ └── UnaryPredicate.java │ │ └── procedures │ │ │ ├── BinaryProcedure.java │ │ │ ├── NonaryProcedure.java │ │ │ ├── NullaryProcedure.java │ │ │ ├── OctaryProcedure.java │ │ │ ├── QuaternaryProcedure.java │ │ │ ├── QuinaryProcedure.java │ │ │ ├── SenaryProcedure.java │ │ │ ├── SeptenaryProcedure.java │ │ │ ├── TernaryProcedure.java │ │ │ └── UnaryProcedure.java │ │ ├── generators │ │ ├── AbstractGenerator.java │ │ ├── ConstantGenerator.java │ │ ├── CyclicGenerator.java │ │ ├── FiniteGenerator.java │ │ └── RandomGenerator.java │ │ ├── iterators │ │ ├── BatchedIterator.java │ │ ├── CachingIterator.java │ │ ├── ChainedIterator.java │ │ ├── ComprehensionIterator.java │ │ ├── CyclicIterator.java │ │ ├── EachIterator.java │ │ ├── FilteredIterator.java │ │ ├── FunctionBackedIterator.java │ │ ├── MappedIterator.java │ │ ├── PredicatedIterator.java │ │ ├── SubSequenceIterator.java │ │ └── ZippedIterator.java │ │ ├── monads │ │ ├── Either.java │ │ ├── Option.java │ │ ├── eithers │ │ │ ├── Left.java │ │ │ └── Right.java │ │ └── options │ │ │ ├── None.java │ │ │ └── Some.java │ │ └── predicates │ │ ├── AndPredicate.java │ │ ├── EqualsPredicate.java │ │ ├── FalsePredicate.java │ │ ├── InstanceOfPredicate.java │ │ ├── NotPredicate.java │ │ ├── OrPredicate.java │ │ └── TruePredicate.java │ └── test │ └── java │ └── org │ └── javafunk │ └── funk │ ├── AccumulatorsTest.java │ ├── ArraysTest.java │ ├── BigDecimalsTest.java │ ├── BigIntegersTest.java │ ├── ChecksTest.java │ ├── ClassesTest.java │ ├── DoublesTest.java │ ├── EagerlyAnyAllNoneTest.java │ ├── EagerlyBatchTest.java │ ├── EagerlyCartesianProductTest.java │ ├── EagerlyComprehensionTest.java │ ├── EagerlyEquateTest.java │ ├── EagerlyFilterRejectPartitionTest.java │ ├── EagerlyFirstSecondThirdRestLastTest.java │ ├── EagerlyGroupIndexTest.java │ ├── EagerlyMapCatTest.java │ ├── EagerlyMapReduceTest.java │ ├── EagerlyMinMaxTest.java │ ├── EagerlyNthRestTest.java │ ├── EagerlyRepeatTest.java │ ├── EagerlyReverseTest.java │ ├── EagerlySliceTest.java │ ├── EagerlyTakeDropTest.java │ ├── EagerlyTimesEachTest.java │ ├── EagerlyZipEnumerateTest.java │ ├── EithersTest.java │ ├── FloatsTest.java │ ├── GeneratorsTest.java │ ├── IntegersTest.java │ ├── IterablesTest.java │ ├── IteratorsTest.java │ ├── LazilyBatchTest.java │ ├── LazilyCartesianProductTest.java │ ├── LazilyComprehensionTest.java │ ├── LazilyConstructConjoinTest.java │ ├── LazilyCycleRepeatTest.java │ ├── LazilyEachTest.java │ ├── LazilyEquateTest.java │ ├── LazilyFilterRejectPartitionTest.java │ ├── LazilyIndexTest.java │ ├── LazilyMapCatTest.java │ ├── LazilyMapTest.java │ ├── LazilyNthRestTest.java │ ├── LazilyRepeatedlyTest.java │ ├── LazilyRestTest.java │ ├── LazilySliceTest.java │ ├── LazilyTakeDropTest.java │ ├── LazilyZipEnumerateTest.java │ ├── LiteralsTest.java │ ├── LongsTest.java │ ├── MappersTest.java │ ├── MapsTest.java │ ├── MultisetsTest.java │ ├── NumbersMultiplyTest.java │ ├── NumbersSumTest.java │ ├── ObjectsTest.java │ ├── OptionsTest.java │ ├── PredicatesTest.java │ ├── SequencesTest.java │ ├── SetsTest.java │ ├── StringsTest.java │ ├── TuplesTest.java │ ├── UnaryFunctionsTest.java │ ├── builders │ ├── ArrayBuilderTest.java │ ├── CollectionBuilderTest.java │ ├── IterableBuilderTest.java │ ├── IteratorBuilderTest.java │ ├── ListBuilderTest.java │ ├── MapBuilderTest.java │ ├── MultisetBuilderTest.java │ └── SetBuilderTest.java │ ├── datastructures │ ├── IntegerRangeTest.java │ └── tuples │ │ ├── NonupleTest.java │ │ ├── OctupleTest.java │ │ ├── PairTest.java │ │ ├── QuadrupleTest.java │ │ ├── QuintupleTest.java │ │ ├── SeptupleTest.java │ │ ├── SextupleTest.java │ │ ├── SingleTest.java │ │ └── TripleTest.java │ ├── functors │ └── adapters │ │ └── CallableNullaryFunctionAdapterTest.java │ ├── generators │ ├── ConstantGeneratorTest.java │ ├── CyclicGeneratorTest.java │ ├── FiniteGeneratorTest.java │ └── RandomGeneratorTest.java │ ├── iterators │ ├── BatchedIteratorTest.java │ ├── ChainedIteratorTest.java │ ├── ComprehensionIteratorTest.java │ ├── CyclicIteratorTest.java │ ├── EachIteratorTest.java │ ├── FilteredIteratorTest.java │ ├── MappedIteratorTest.java │ ├── PredicatedIteratorTest.java │ ├── SubSequenceIteratorTest.java │ └── ZippedIteratorTest.java │ ├── monads │ ├── EitherTest.java │ └── OptionTest.java │ ├── predicates │ ├── AndPredicateTest.java │ ├── EqualsPredicateTest.java │ ├── FalsePredicateTest.java │ ├── InstanceOfPredicateTest.java │ ├── NotPredicateTest.java │ ├── OrPredicateTest.java │ └── TruePredicateTest.java │ └── testclasses │ ├── Age.java │ ├── Animal.java │ ├── Cat.java │ ├── Colour.java │ ├── Dog.java │ ├── Location.java │ ├── Matchers.java │ ├── NaiveMultiset.java │ ├── Name.java │ ├── NoArgsConstructorIterator.java │ ├── NoArgsConstructorMultiset.java │ ├── NoNoArgsConstructorIterator.java │ ├── NoNoArgsConstructorList.java │ ├── NoNoArgsConstructorMap.java │ ├── NoNoArgsConstructorSet.java │ ├── PrivateAccessConstructorMultiset.java │ ├── SomeArgsConstructorMultiset.java │ ├── StubMultiset.java │ ├── TrackingCallable.java │ └── TrackingNullaryFunction.java ├── funk-jackson ├── README.md ├── build.gradle └── src │ ├── main │ └── java │ │ └── org │ │ └── javafunk │ │ └── funk │ │ └── jackson │ │ ├── FunkDeserializers.java │ │ ├── FunkModule.java │ │ ├── FunkSerializers.java │ │ └── monad │ │ ├── OptionDeserializer.java │ │ └── OptionSerializer.java │ └── test │ └── java │ └── org │ └── javafunk │ └── funk │ └── jackson │ └── monad │ ├── GenericData.java │ ├── OptionData.java │ ├── OptionDeserializerTest.java │ └── OptionSerializerTest.java ├── funk-matchers ├── build.gradle └── src │ ├── main │ └── java │ │ └── org │ │ └── javafunk │ │ └── funk │ │ └── matchers │ │ ├── EitherMatchers.java │ │ ├── OptionMatchers.java │ │ └── implementations │ │ ├── EitherAnyLeftMatcher.java │ │ ├── EitherAnyRightMatcher.java │ │ ├── EitherSpecificLeftMatcher.java │ │ ├── EitherSpecificRightMatcher.java │ │ ├── OptionHasAnyValueMatcher.java │ │ ├── OptionHasNoValueMatcher.java │ │ └── OptionHasSpecificValueMatcher.java │ └── test │ └── java │ └── org │ └── javafunk │ └── funk │ └── matchers │ ├── EitherMatchersTest.java │ └── OptionMatchersTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── scripts ├── release.sh └── test.sh └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/workspace.xml 2 | .idea/tasks.xml 3 | .idea/vcs.xml 4 | .idea/scopes/scope_settings.xml 5 | .idea/dictionaries/* 6 | .idea/* 7 | .gradle/ 8 | gradle.properties 9 | *.iws 10 | build/ 11 | target/ 12 | out/ 13 | *.DS_Store 14 | *~ 15 | *.ipr 16 | *.iml 17 | -------------------------------------------------------------------------------- /.hgignore: -------------------------------------------------------------------------------- 1 | .idea/workspace.xml 2 | .idea/tasks.xml 3 | .idea/vcs.xml 4 | target/ 5 | build/ 6 | out/ 7 | .gradle/ 8 | 9 | syntax: glob 10 | *.DS_Store 11 | *~ 12 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | (BSD Style License) 2 | 3 | Copyright (c) 2012, Funk Committers 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /funk-core/build.gradle: -------------------------------------------------------------------------------- 1 | import org.gradle.api.artifacts.maven.MavenDeployment 2 | 3 | dependencies { 4 | compile 'org.apache.commons:commons-lang3:3.3.2' 5 | compile 'org.hamcrest:hamcrest-core:1.3' 6 | compile "org.hamcrest:hamcrest-library:1.3" 7 | compile 'com.google.guava:guava:18.0' 8 | 9 | testCompile "org.javafunk.matchbox:matchbox:0.1.4" 10 | } 11 | 12 | uploadArchives { 13 | repositories { 14 | flatDir { 15 | dirs "${rootDir}/repos" 16 | } 17 | 18 | mavenDeployer { 19 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 20 | 21 | repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { 22 | authentication( 23 | userName: project.property("oss-releases.username"), 24 | password: project.property("oss-releases.password")) 25 | } 26 | 27 | snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { 28 | authentication( 29 | userName: project.property("oss-releases.username"), 30 | password: project.property("oss-releases.password")) 31 | } 32 | 33 | pom.project { 34 | name 'Funk Core' 35 | description 'Functional utilities for Java: core APIs' 36 | packaging 'jar' 37 | url 'https://github.com/javafunk/funk' 38 | 39 | scm { 40 | url 'scm:git:https://github.com/javafunk/funk.git' 41 | connection 'scm:git:https://github.com/javafunk/funk.git' 42 | developerConnection 'http://github.com/javafunk/funk' 43 | } 44 | 45 | licenses { 46 | license { 47 | name 'BSD License' 48 | url 'http://www.opensource.org/licenses/bsd-license.php' 49 | } 50 | } 51 | 52 | developers { 53 | developer { 54 | id 'toby' 55 | name 'Toby Clemson' 56 | email 'tobyclemson@gmail.com' 57 | } 58 | developer { 59 | id 'andrew' 60 | name 'Andrew Kiellor' 61 | email 'akiellor@gmail.com' 62 | } 63 | developer { 64 | id 'simon' 65 | name 'Simon Brunning' 66 | email 'simon@brunningonline.net' 67 | } 68 | } 69 | } 70 | 71 | pom.whenConfigured { pom -> 72 | rootProject.addAsParentFor(pom) 73 | } 74 | } 75 | } 76 | } -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Arrays.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import java.util.List; 12 | 13 | import static org.javafunk.funk.Literals.iterableFrom; 14 | import static org.javafunk.funk.Literals.listFrom; 15 | 16 | public class Arrays { 17 | private Arrays() {} 18 | 19 | public static List asList(T[] array) { 20 | return listFrom(array); 21 | } 22 | 23 | public static Iterable asIterable(T[] array) { 24 | return iterableFrom(array); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/BigIntegers.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.javafunk.funk.functors.Mapper; 4 | 5 | import java.math.BigDecimal; 6 | import java.math.BigInteger; 7 | 8 | public class BigIntegers { 9 | public static Mapper fromStringToBigInteger() { 10 | return new Mapper() { 11 | @Override public BigInteger map(String input) { 12 | return new BigInteger(input); 13 | } 14 | }; 15 | } 16 | 17 | public static Mapper fromIntegerToBigInteger() { 18 | return new Mapper() { 19 | @Override public BigInteger map(Integer input) { 20 | return new BigInteger(input.toString()); 21 | } 22 | }; 23 | } 24 | 25 | public static Mapper fromLongToBigInteger() { 26 | return new Mapper() { 27 | @Override public BigInteger map(Long input) { 28 | return new BigInteger(input.toString()); 29 | } 30 | }; 31 | } 32 | 33 | public static Mapper fromFloatToBigInteger() { 34 | return new Mapper() { 35 | @Override public BigInteger map(Float input) { 36 | return new BigInteger(((Integer) input.intValue()).toString()); 37 | } 38 | }; 39 | } 40 | 41 | public static Mapper fromDoubleToBigInteger() { 42 | return new Mapper() { 43 | @Override public BigInteger map(Double input) { 44 | return new BigInteger(((Long) input.longValue()).toString()); 45 | } 46 | }; 47 | } 48 | 49 | public static Mapper fromBigDecimalToBigInteger() { 50 | return new Mapper() { 51 | @Override public BigInteger map(BigDecimal input) { 52 | return input.toBigInteger(); 53 | } 54 | }; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Checks.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.javafunk.funk.functors.functions.NullaryFunction; 4 | 5 | import static org.javafunk.funk.Eagerly.any; 6 | import static org.javafunk.funk.Exceptions.nullPointerFactory; 7 | import static org.javafunk.funk.Predicates.equalTo; 8 | 9 | public class Checks { 10 | public static T returnOrThrowIfNull(T value, NullaryFunction exceptionFactory) { 11 | return orThrowIf(value, value == null, exceptionFactory); 12 | } 13 | 14 | public static > T returnOrThrowIfEmpty( 15 | T value, NullaryFunction exceptionFactory) { 16 | return orThrowIf( 17 | value, 18 | !value.iterator().hasNext(), 19 | exceptionFactory); 20 | } 21 | 22 | public static > T returnOrThrowIfContainsNull(T iterable) { 23 | return orThrowIf( 24 | iterable, 25 | any(iterable, equalTo(null)), 26 | nullPointerFactory()); 27 | } 28 | 29 | private static T orThrowIf(T value, boolean condition, NullaryFunction exceptionFactory) { 30 | throwIf(condition, exceptionFactory); 31 | return value; 32 | } 33 | 34 | private static void throwIf(boolean condition, NullaryFunction exceptionFactory) { 35 | if (condition) throw exceptionFactory.call(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Classes.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.javafunk.funk.functors.functions.UnaryFunction; 4 | 5 | import static com.google.common.base.Preconditions.checkNotNull; 6 | import static java.lang.String.format; 7 | 8 | public class Classes { 9 | public static T uncheckedInstantiate(final Class classToInstantiate) { 10 | return uncheckedInstantiate(classToInstantiate, new UnaryFunction() { 11 | @Override public RuntimeException call(Exception exception) { 12 | throw new IllegalArgumentException( 13 | format("Could not instantiate instance of type %s. Does it have a public no argument constructor?", 14 | classToInstantiate.getSimpleName()), 15 | exception); 16 | } 17 | }); 18 | } 19 | 20 | public static T uncheckedInstantiate( 21 | Class classToInstantiate, 22 | final RuntimeException exception) { 23 | return uncheckedInstantiate(classToInstantiate, new UnaryFunction() { 24 | @Override public RuntimeException call(Exception instantiationException) { 25 | return exception; 26 | } 27 | }); 28 | } 29 | 30 | public static T uncheckedInstantiate( 31 | Class classToInstantiate, 32 | UnaryFunction exceptionHandler) { 33 | checkNotNull(exceptionHandler); 34 | try { 35 | return classToInstantiate.newInstance(); 36 | } catch (InstantiationException exception) { 37 | throw exceptionHandler.call(exception); 38 | } catch (IllegalAccessException exception) { 39 | throw exceptionHandler.call(exception); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Doubles.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.javafunk.funk.functors.Mapper; 4 | 5 | import java.math.BigDecimal; 6 | import java.math.BigInteger; 7 | 8 | public class Doubles { 9 | public static Mapper fromStringToDouble() { 10 | return new Mapper() { 11 | @Override public Double map(String input) { 12 | return Double.valueOf(input); 13 | } 14 | }; 15 | } 16 | 17 | public static Mapper fromIntegerToDouble() { 18 | return new Mapper() { 19 | @Override public Double map(Integer input) { 20 | return input.doubleValue(); 21 | } 22 | }; 23 | } 24 | 25 | public static Mapper fromLongToDouble() { 26 | return new Mapper() { 27 | @Override public Double map(Long input) { 28 | return input.doubleValue(); 29 | } 30 | }; 31 | } 32 | 33 | public static Mapper fromBigIntegerToDouble() { 34 | return new Mapper() { 35 | @Override public Double map(BigInteger input) { 36 | return input.doubleValue(); 37 | } 38 | }; 39 | } 40 | 41 | public static Mapper fromFloatToDouble() { 42 | return new Mapper() { 43 | @Override public Double map(Float input) { 44 | return input.doubleValue(); 45 | } 46 | }; 47 | } 48 | 49 | public static Mapper fromBigDecimalToDouble() { 50 | return new Mapper() { 51 | @Override public Double map(BigDecimal input) { 52 | return input.doubleValue(); 53 | } 54 | }; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Eithers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import org.javafunk.funk.functors.Mapper; 12 | import org.javafunk.funk.functors.Predicate; 13 | import org.javafunk.funk.monads.Either; 14 | 15 | import static org.javafunk.funk.Lazily.filter; 16 | import static org.javafunk.funk.Lazily.map; 17 | 18 | public class Eithers { 19 | private Eithers() {} 20 | 21 | public static Iterable rights(Iterable> eithers) { 22 | return map(filter(eithers, Eithers.isRight()), Eithers.toRight()); 23 | } 24 | 25 | public static Iterable lefts(Iterable> eithers) { 26 | return map(filter(eithers, Eithers.isLeft()), Eithers.toLeft()); 27 | } 28 | 29 | public static Mapper, T> toRight() { 30 | return new Mapper, T>(){ 31 | @Override public T map(Either either) { 32 | return either.getRight(); 33 | } 34 | }; 35 | } 36 | 37 | public static Mapper, S> toLeft() { 38 | return new Mapper, S>() { 39 | @Override public S map(Either either) { 40 | return either.getLeft(); 41 | } 42 | }; 43 | } 44 | 45 | public static Predicate> isRight() { 46 | return new Predicate>() { 47 | @Override public boolean evaluate(Either either) { 48 | return either.isRight(); 49 | } 50 | }; 51 | } 52 | 53 | public static Predicate> isLeft() { 54 | return new Predicate>() { 55 | @Override public boolean evaluate(Either either) { 56 | return either.isLeft(); 57 | } 58 | }; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Floats.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.javafunk.funk.functors.Mapper; 4 | 5 | import java.math.BigDecimal; 6 | import java.math.BigInteger; 7 | 8 | public class Floats { 9 | public static Mapper fromStringToFloat() { 10 | return new Mapper() { 11 | @Override public Float map(String input) { 12 | return Float.valueOf(input); 13 | } 14 | }; 15 | } 16 | 17 | public static Mapper fromIntegerToFloat() { 18 | return new Mapper() { 19 | @Override public Float map(Integer input) { 20 | return input.floatValue(); 21 | } 22 | }; 23 | } 24 | 25 | public static Mapper fromLongToFloat() { 26 | return new Mapper() { 27 | @Override public Float map(Long input) { 28 | return input.floatValue(); 29 | } 30 | }; 31 | } 32 | 33 | public static Mapper fromBigIntegerToFloat() { 34 | return new Mapper() { 35 | @Override public Float map(BigInteger input) { 36 | return input.floatValue(); 37 | } 38 | }; 39 | } 40 | 41 | public static Mapper fromDoubleToFloat() { 42 | return new Mapper() { 43 | @Override public Float map(Double input) { 44 | return input.floatValue(); 45 | } 46 | }; 47 | } 48 | 49 | public static Mapper fromBigDecimalToFloat() { 50 | return new Mapper() { 51 | @Override public Float map(BigDecimal input) { 52 | return input.floatValue(); 53 | } 54 | }; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Generators.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import org.javafunk.funk.annotations.ToDo; 12 | import org.javafunk.funk.behaviours.Generatable; 13 | import org.javafunk.funk.behaviours.Generator; 14 | import org.javafunk.funk.generators.ConstantGenerator; 15 | import org.javafunk.funk.generators.FiniteGenerator; 16 | 17 | import java.util.Iterator; 18 | 19 | public class Generators { 20 | private Generators() {} 21 | 22 | public static Generator constantGenerator(T value) { 23 | return new ConstantGenerator(value); 24 | } 25 | 26 | public static Generator finiteGenerator(Iterable iterable) { 27 | return new FiniteGenerator(iterable); 28 | } 29 | 30 | @ToDo(raisedBy = "Toby", 31 | date = "2012-03-25", 32 | message = "This needs to tee rather than using the same instance in the same way that toIterable would") 33 | public static Generatable toGeneratable(final Generator generator) { 34 | return new Generatable() { 35 | @Override public Iterator iterator() { 36 | return generator; 37 | } 38 | }; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Integers.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.javafunk.funk.functors.Mapper; 4 | 5 | import java.math.BigDecimal; 6 | import java.math.BigInteger; 7 | 8 | import static com.google.common.base.Preconditions.checkNotNull; 9 | 10 | public class Integers { 11 | public static Mapper fromStringToInteger() { 12 | return new Mapper() { 13 | @Override public Integer map(String input) { 14 | checkNotNull(input); 15 | return Integer.valueOf(input); 16 | } 17 | }; 18 | } 19 | 20 | public static Mapper fromLongToInteger() { 21 | return new Mapper() { 22 | @Override public Integer map(Long input) { 23 | return input.intValue(); 24 | } 25 | }; 26 | } 27 | 28 | public static Mapper fromBigIntegerToInteger() { 29 | return new Mapper() { 30 | @Override public Integer map(BigInteger input) { 31 | return input.intValue(); 32 | } 33 | }; 34 | } 35 | 36 | public static Mapper fromFloatToInteger() { 37 | return new Mapper() { 38 | @Override public Integer map(Float input) { 39 | return input.intValue(); 40 | } 41 | }; 42 | } 43 | 44 | public static Mapper fromDoubleToInteger() { 45 | return new Mapper() { 46 | @Override public Integer map(Double input) { 47 | return input.intValue(); 48 | } 49 | }; 50 | } 51 | 52 | public static Mapper fromBigDecimalToInteger() { 53 | return new Mapper() { 54 | @Override public Integer map(BigDecimal input) { 55 | return input.intValue(); 56 | } 57 | }; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Iterables.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import com.google.common.collect.Multiset; 12 | 13 | import java.util.Collection; 14 | import java.util.List; 15 | import java.util.Set; 16 | 17 | import static org.javafunk.funk.Literals.*; 18 | 19 | public class Iterables { 20 | private Iterables() {} 21 | 22 | public static Iterable concat(Iterable> iterables) { 23 | return com.google.common.collect.Iterables.concat(iterables); 24 | } 25 | 26 | public static List asList(Iterable iterable) { 27 | return listFrom(iterable); 28 | } 29 | 30 | public static Set asSet(Iterable iterable) { 31 | return setFrom(iterable); 32 | } 33 | 34 | public static Multiset asMultiset(Iterable iterable) { 35 | return multisetFrom(iterable); 36 | } 37 | 38 | public static Collection materialize(Iterable iterable) { 39 | return collectionFrom(iterable); 40 | } 41 | 42 | public static Iterable empty() { 43 | return iterable(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Maps.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import org.javafunk.funk.functors.Mapper; 12 | import org.javafunk.funk.functors.functions.NullaryFunction; 13 | import org.javafunk.funk.functors.functions.UnaryFunction; 14 | 15 | import java.util.Map; 16 | import java.util.Set; 17 | 18 | public class Maps { 19 | private Maps() {} 20 | 21 | public static V getOrAdd(Map map, U key, UnaryFunction mapper) { 22 | if (map.containsKey(key)) { 23 | return map.get(key); 24 | } else { 25 | V newValue = mapper.call(key); 26 | map.put(key, newValue); 27 | return newValue; 28 | } 29 | } 30 | 31 | public static V getOrAdd(Map map, U key, final NullaryFunction factory) { 32 | return getOrAdd(map, key, new Mapper() { 33 | @Override public V map(U input) { 34 | return factory.call(); 35 | } 36 | }); 37 | } 38 | 39 | public static Mapper, Set>> toEntrySet() { 40 | return new Mapper, Set>>() { 41 | @Override public Set> map(Map input) { 42 | return input.entrySet(); 43 | } 44 | }; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Objects.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import org.javafunk.funk.functors.Mapper; 12 | import org.javafunk.funk.functors.predicates.UnaryPredicate; 13 | 14 | public class Objects { 15 | public static boolean isNull(Object value) { 16 | return value == null; 17 | } 18 | 19 | public static boolean isNotNull(Object value) { 20 | return !isNull(value); 21 | } 22 | 23 | public static UnaryPredicate whereNull() { 24 | return new UnaryPredicate() { 25 | @Override public boolean evaluate(Object input) { 26 | return isNull(input); 27 | } 28 | }; 29 | } 30 | 31 | public static UnaryPredicate whereNotNull() { 32 | return new UnaryPredicate() { 33 | @Override public boolean evaluate(Object input) { 34 | return isNotNull(input); 35 | } 36 | }; 37 | } 38 | 39 | public static Mapper toStringValue() { 40 | return new Mapper() { 41 | @Override public String map(Object input) { 42 | return input.toString(); 43 | } 44 | }; 45 | } 46 | 47 | public static Mapper toStringValueFor(@SuppressWarnings("unused") Class targetClass) { 48 | return new Mapper() { 49 | @Override public String map(T input) { 50 | return input.toString(); 51 | } 52 | }; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Options.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.javafunk.funk.functors.Mapper; 4 | import org.javafunk.funk.functors.Predicate; 5 | import org.javafunk.funk.functors.predicates.UnaryPredicate; 6 | import org.javafunk.funk.monads.Option; 7 | 8 | public class Options { 9 | public static Iterable> somes(Iterable> options) { 10 | return Lazily.filter(options, Options.isSome()); 11 | } 12 | 13 | public static Iterable> nones(Iterable> options) { 14 | return Lazily.filter(options, Options.isNone()); 15 | } 16 | 17 | public static Predicate> isSome() { 18 | return new Predicate>() { 19 | @Override public boolean evaluate(Option option) { 20 | return option.hasValue(); 21 | } 22 | }; 23 | } 24 | 25 | public static Predicate> isNone() { 26 | return new Predicate>() { 27 | @Override public boolean evaluate(Option option) { 28 | return option.hasNoValue(); 29 | } 30 | }; 31 | } 32 | 33 | public static Predicate> hasValue() { 34 | return isSome(); 35 | } 36 | 37 | public static Predicate> hasNoValue() { 38 | return isNone(); 39 | } 40 | 41 | public static Mapper, T> toValue() { 42 | return new Mapper, T>() { 43 | @Override public T map(Option input) { 44 | return input.getValue(); 45 | } 46 | }; 47 | } 48 | 49 | public static Mapper, T> toValue( 50 | @SuppressWarnings("unused") Class valueClass) { 51 | return toValue(); 52 | } 53 | 54 | public static Iterable flatten(Iterable> options) { 55 | return Lazily.map(somes(options), Options.toValue()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Predicates.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import org.javafunk.funk.functors.Predicate; 12 | import org.javafunk.funk.functors.predicates.UnaryPredicate; 13 | import org.javafunk.funk.predicates.EqualsPredicate; 14 | import org.javafunk.funk.predicates.FalsePredicate; 15 | import org.javafunk.funk.predicates.InstanceOfPredicate; 16 | import org.javafunk.funk.predicates.NotPredicate; 17 | import org.javafunk.funk.predicates.TruePredicate; 18 | 19 | import static com.google.common.base.Preconditions.checkNotNull; 20 | 21 | public class Predicates { 22 | private Predicates() {} 23 | 24 | public static Predicate alwaysTrue() { 25 | return new TruePredicate(); 26 | } 27 | 28 | public static Predicate alwaysTrue( 29 | @SuppressWarnings("unused") Class klass) { 30 | return alwaysTrue(); 31 | } 32 | 33 | public static Predicate alwaysFalse() { 34 | return new FalsePredicate(); 35 | } 36 | 37 | public static Predicate alwaysFalse( 38 | @SuppressWarnings("unused") Class klass) { 39 | return alwaysFalse(); 40 | } 41 | 42 | public static Predicate not(UnaryPredicate predicate) { 43 | return new NotPredicate(checkNotNull(predicate)); 44 | } 45 | 46 | public static Predicate instanceOf(Class testClass) { 47 | return new InstanceOfPredicate(testClass); 48 | } 49 | 50 | public static Predicate equalTo(T testObject) { 51 | return new EqualsPredicate(testObject); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/Sequences.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import org.javafunk.funk.datastructures.IntegerRange; 12 | 13 | public class Sequences { 14 | private Sequences() {} 15 | 16 | private enum Direction { 17 | ASCENDING(+1), DESCENDING(-1); 18 | 19 | private int multiplier; 20 | 21 | Direction(int multiplier) { 22 | this.multiplier = multiplier; 23 | } 24 | 25 | int getMultiplier() { 26 | return multiplier; 27 | } 28 | } 29 | 30 | public static Direction increasing() { 31 | return Direction.ASCENDING; 32 | } 33 | 34 | public static Direction decreasing() { 35 | return Direction.DESCENDING; 36 | } 37 | 38 | public static Iterable integers(Direction direction) { 39 | return new IntegerRange(0, null, direction.getMultiplier()); 40 | } 41 | 42 | public static Iterable integersFrom(int startPoint, Direction direction) { 43 | return new IntegerRange(startPoint, null, direction.getMultiplier()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/annotations/Alpha.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.annotations; 10 | 11 | import java.lang.annotation.Retention; 12 | import java.lang.annotation.RetentionPolicy; 13 | 14 | @Retention(RetentionPolicy.SOURCE) 15 | public @interface Alpha {} 16 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/annotations/Beta.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.annotations; 10 | 11 | import java.lang.annotation.Retention; 12 | import java.lang.annotation.RetentionPolicy; 13 | 14 | @Retention(RetentionPolicy.SOURCE) 15 | public @interface Beta {} 16 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/annotations/ToDo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.annotations; 10 | 11 | import java.lang.annotation.Retention; 12 | import java.lang.annotation.RetentionPolicy; 13 | 14 | @Retention(RetentionPolicy.SOURCE) 15 | public @interface ToDo { 16 | String raisedBy(); 17 | String date(); 18 | String message(); 19 | } 20 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/Describable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours; 10 | 11 | public interface Describable { 12 | String describe(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/Generatable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours; 10 | 11 | public interface Generatable extends Iterable {} 12 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/Generator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours; 10 | 11 | import java.util.Iterator; 12 | 13 | public interface Generator extends Iterator {} 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/Mappable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours; 10 | 11 | import org.javafunk.funk.functors.functions.UnaryFunction; 12 | 13 | public interface Mappable> { 14 | O map(UnaryFunction mapper); 15 | } 16 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/Value.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours; 10 | 11 | public interface Value { 12 | T getValue(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/Eighth.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours.ordinals; 10 | 11 | public interface Eighth { 12 | T getEighth(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/Fifth.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours.ordinals; 10 | 11 | public interface Fifth { 12 | T getFifth(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/First.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours.ordinals; 10 | 11 | public interface First { 12 | T getFirst(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/Fourth.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours.ordinals; 10 | 11 | public interface Fourth { 12 | T getFourth(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/Ninth.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours.ordinals; 10 | 11 | public interface Ninth { 12 | T getNinth(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/Second.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours.ordinals; 10 | 11 | public interface Second { 12 | T getSecond(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/Seventh.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours.ordinals; 10 | 11 | public interface Seventh { 12 | T getSeventh(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/Sixth.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours.ordinals; 10 | 11 | public interface Sixth { 12 | T getSixth(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/Third.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.behaviours.ordinals; 10 | 11 | public interface Third { 12 | T getThird(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/mappables/MappableEighth.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.behaviours.ordinals.mappables; 2 | 3 | import org.javafunk.funk.functors.functions.UnaryFunction; 4 | 5 | public interface MappableEighth> { 6 | O mapEighth(UnaryFunction function); 7 | } 8 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/mappables/MappableFifth.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.behaviours.ordinals.mappables; 2 | 3 | import org.javafunk.funk.functors.functions.UnaryFunction; 4 | 5 | public interface MappableFifth> { 6 | O mapFifth(UnaryFunction function); 7 | } 8 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/mappables/MappableFirst.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.behaviours.ordinals.mappables; 2 | 3 | import org.javafunk.funk.functors.functions.UnaryFunction; 4 | 5 | public interface MappableFirst> { 6 | O mapFirst(UnaryFunction function); 7 | } 8 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/mappables/MappableFourth.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.behaviours.ordinals.mappables; 2 | 3 | import org.javafunk.funk.functors.functions.UnaryFunction; 4 | 5 | public interface MappableFourth> { 6 | O mapFourth(UnaryFunction function); 7 | } 8 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/mappables/MappableNinth.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.behaviours.ordinals.mappables; 2 | 3 | import org.javafunk.funk.functors.functions.UnaryFunction; 4 | 5 | public interface MappableNinth> { 6 | O mapNinth(UnaryFunction function); 7 | } 8 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/mappables/MappableSecond.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.behaviours.ordinals.mappables; 2 | 3 | import org.javafunk.funk.functors.functions.UnaryFunction; 4 | 5 | public interface MappableSecond> { 6 | O mapSecond(UnaryFunction function); 7 | } 8 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/mappables/MappableSeventh.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.behaviours.ordinals.mappables; 2 | 3 | import org.javafunk.funk.functors.functions.UnaryFunction; 4 | 5 | public interface MappableSeventh> { 6 | O mapSeventh(UnaryFunction function); 7 | } 8 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/mappables/MappableSixth.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.behaviours.ordinals.mappables; 2 | 3 | import org.javafunk.funk.functors.functions.UnaryFunction; 4 | 5 | public interface MappableSixth> { 6 | O mapSixth(UnaryFunction function); 7 | } 8 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/behaviours/ordinals/mappables/MappableThird.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.behaviours.ordinals.mappables; 2 | 3 | import org.javafunk.funk.functors.functions.UnaryFunction; 4 | 5 | public interface MappableThird> { 6 | O mapThird(UnaryFunction function); 7 | } 8 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/builders/CollectionBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.builders; 10 | 11 | import org.javafunk.funk.Classes; 12 | import org.javafunk.funk.functors.functions.UnaryFunction; 13 | 14 | import java.util.ArrayList; 15 | import java.util.Collection; 16 | import java.util.Collections; 17 | 18 | public class CollectionBuilder 19 | extends AbstractBuilder, Collection> 20 | implements AbstractBuilder.WithCustomImplementationSupport> { 21 | private Collection elements = new ArrayList(); 22 | 23 | public static CollectionBuilder collectionBuilder() { 24 | return new CollectionBuilder(); 25 | } 26 | 27 | public static CollectionBuilder collectionBuilder(Class elementClass) { 28 | return new CollectionBuilder(); 29 | } 30 | 31 | @Override public Collection build() { 32 | return Collections.unmodifiableList(new ArrayList(elements)); 33 | } 34 | 35 | @Override public Collection build(Class implementationClass) { 36 | @SuppressWarnings("unchecked") 37 | Collection collection = Classes.uncheckedInstantiate(implementationClass); 38 | collection.addAll(elements); 39 | return collection; 40 | } 41 | 42 | @Override public > T build(UnaryFunction, ? extends T> builderFunction) { 43 | return builderFunction.call(Collections.unmodifiableCollection(elements)); 44 | } 45 | 46 | @Override protected void handle(E element) { 47 | elements.add(element); 48 | } 49 | 50 | @Override protected CollectionBuilder updatedBuilder() { 51 | return this; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/builders/IterableBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.builders; 10 | 11 | import java.util.ArrayList; 12 | import java.util.Collections; 13 | import java.util.List; 14 | 15 | public class IterableBuilder extends AbstractBuilder, Iterable> { 16 | private List elements = new ArrayList(); 17 | 18 | public static IterableBuilder iterableBuilder() { 19 | return new IterableBuilder(); 20 | } 21 | 22 | public static IterableBuilder iterableBuilder(Class elementClass) { 23 | return new IterableBuilder(); 24 | } 25 | 26 | @Override public Iterable build() { 27 | return Collections.unmodifiableList(new ArrayList(elements)); 28 | } 29 | 30 | @Override protected void handle(E element) { 31 | elements.add(element); 32 | } 33 | 34 | @Override protected IterableBuilder updatedBuilder() { 35 | return this; 36 | } 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/builders/IteratorBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.builders; 10 | 11 | import java.util.ArrayList; 12 | import java.util.Collections; 13 | import java.util.Iterator; 14 | import java.util.List; 15 | 16 | public class IteratorBuilder extends AbstractBuilder, Iterator> { 17 | private List elements = new ArrayList(); 18 | 19 | public static IteratorBuilder iteratorBuilder() { 20 | return new IteratorBuilder(); 21 | } 22 | 23 | public static IteratorBuilder iteratorBuilder(Class elementClass) { 24 | return new IteratorBuilder(); 25 | } 26 | 27 | @Override public Iterator build() { 28 | return Collections.unmodifiableList(new ArrayList(elements)).iterator(); 29 | } 30 | 31 | @Override protected void handle(E element) { 32 | elements.add(element); 33 | } 34 | 35 | @Override protected IteratorBuilder updatedBuilder() { 36 | return this; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/builders/ListBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.builders; 10 | 11 | import org.javafunk.funk.Classes; 12 | import org.javafunk.funk.functors.functions.UnaryFunction; 13 | 14 | import java.util.ArrayList; 15 | import java.util.Collections; 16 | import java.util.List; 17 | 18 | public class ListBuilder 19 | extends AbstractBuilder, List> 20 | implements AbstractBuilder.WithCustomImplementationSupport> { 21 | private List elements = new ArrayList(); 22 | 23 | public static ListBuilder listBuilder() { 24 | return new ListBuilder(); 25 | } 26 | 27 | public static ListBuilder listBuilder(Class elementClass) { 28 | return new ListBuilder(); 29 | } 30 | 31 | @Override public List build() { 32 | return Collections.unmodifiableList(new ArrayList(elements)); 33 | } 34 | 35 | @Override public List build(Class implementationClass) { 36 | @SuppressWarnings("unchecked") 37 | List list = (List) Classes.uncheckedInstantiate(implementationClass); 38 | list.addAll(elements); 39 | return list; 40 | } 41 | 42 | @Override public > T build(UnaryFunction, ? extends T> builderFunction) { 43 | return builderFunction.call(Collections.unmodifiableList(elements)); 44 | } 45 | 46 | @Override protected void handle(E element) { 47 | elements.add(element); 48 | } 49 | 50 | @Override protected ListBuilder updatedBuilder() { 51 | return this; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/builders/MultisetBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.builders; 10 | 11 | import com.google.common.collect.HashMultiset; 12 | import com.google.common.collect.Multiset; 13 | import com.google.common.collect.Multisets; 14 | import org.javafunk.funk.Classes; 15 | import org.javafunk.funk.functors.functions.UnaryFunction; 16 | 17 | import java.util.ArrayList; 18 | import java.util.Collections; 19 | import java.util.List; 20 | 21 | public class MultisetBuilder 22 | extends AbstractBuilder, Multiset> 23 | implements AbstractBuilder.WithCustomImplementationSupport> { 24 | // TODO: Use a sparse multiset that maintains exact insertion ordering for this purpose. 25 | // This is not memory efficient. 26 | private List elements = new ArrayList(); 27 | 28 | public static MultisetBuilder multisetBuilder() { 29 | return new MultisetBuilder(); 30 | } 31 | 32 | public static MultisetBuilder multisetBuilder(Class elementClass) { 33 | return new MultisetBuilder(); 34 | } 35 | 36 | @Override public Multiset build() { 37 | return Multisets.unmodifiableMultiset(HashMultiset.create(elements)); 38 | } 39 | 40 | @Override public Multiset build(Class implementationClass) { 41 | @SuppressWarnings("unchecked") 42 | Multiset multiset = (Multiset) Classes.uncheckedInstantiate(implementationClass); 43 | multiset.addAll(elements); 44 | return multiset; 45 | } 46 | 47 | @Override public > T build(UnaryFunction, ? extends T> builderFunction) { 48 | return builderFunction.call(Collections.unmodifiableCollection(elements)); 49 | } 50 | 51 | @Override protected void handle(E element) { 52 | elements.add(element); 53 | } 54 | 55 | @Override protected MultisetBuilder updatedBuilder() { 56 | return this; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/builders/SetBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.builders; 10 | 11 | import org.javafunk.funk.Classes; 12 | import org.javafunk.funk.functors.functions.UnaryFunction; 13 | 14 | import java.util.ArrayList; 15 | import java.util.Collections; 16 | import java.util.HashSet; 17 | import java.util.List; 18 | import java.util.Set; 19 | 20 | public class SetBuilder 21 | extends AbstractBuilder, Set> 22 | implements AbstractBuilder.WithCustomImplementationSupport> { 23 | private List elements = new ArrayList(); 24 | 25 | public static SetBuilder setBuilder() { 26 | return new SetBuilder(); 27 | } 28 | 29 | public static SetBuilder setBuilder(Class elementClass) { 30 | return new SetBuilder(); 31 | } 32 | 33 | @Override public Set build() { 34 | return Collections.unmodifiableSet(new HashSet(elements)); 35 | } 36 | 37 | @Override public Set build(Class implementationClass) { 38 | @SuppressWarnings("unchecked") 39 | Set set = (Set) Classes.uncheckedInstantiate(implementationClass); 40 | set.addAll(elements); 41 | return set; 42 | } 43 | 44 | @Override public > T build(UnaryFunction, ? extends T> builderFunction) { 45 | return builderFunction.call(Collections.unmodifiableList(elements)); 46 | } 47 | 48 | @Override protected void handle(E element) { 49 | elements.add(element); 50 | } 51 | 52 | @Override protected SetBuilder updatedBuilder() { 53 | return this; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/datastructures/Range.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.datastructures; 10 | 11 | public interface Range extends Iterable { 12 | boolean contains(Object other); 13 | T getStart(); 14 | T getEnd(); 15 | T getStep(); 16 | } 17 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/datastructures/tuples/AbstractTuple.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.datastructures.tuples; 10 | 11 | import org.apache.commons.lang3.builder.EqualsBuilder; 12 | import org.apache.commons.lang3.builder.HashCodeBuilder; 13 | 14 | import java.util.Iterator; 15 | 16 | import static java.lang.String.format; 17 | import static org.javafunk.funk.Lazily.zip; 18 | import static org.javafunk.funk.Strings.join; 19 | 20 | public abstract class AbstractTuple implements Iterable { 21 | public abstract Iterable getValues(); 22 | 23 | @Override 24 | public String toString() { 25 | return format("(%s)", join(getValues(), ", ")); 26 | } 27 | 28 | @Override 29 | public boolean equals(Object other) { 30 | if (other == null) { 31 | return false; 32 | } 33 | 34 | if (other == this) { 35 | return true; 36 | } 37 | 38 | if (other.getClass() != this.getClass()) { 39 | return false; 40 | } 41 | 42 | EqualsBuilder equalsBuilder = new EqualsBuilder(); 43 | Iterable> zippedValues = zip(this.getValues(), ((AbstractTuple) other).getValues()); 44 | for (Pair values : zippedValues) { 45 | equalsBuilder.append(values.getFirst(), values.getSecond()); 46 | } 47 | 48 | return equalsBuilder.isEquals(); 49 | } 50 | 51 | @Override 52 | public int hashCode() { 53 | HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(); 54 | for (Object value : getValues()) { 55 | hashCodeBuilder.append(value); 56 | } 57 | return hashCodeBuilder.toHashCode(); 58 | } 59 | 60 | public Iterator iterator(){ 61 | return getValues().iterator(); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/datastructures/tuples/Pair.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.datastructures.tuples; 10 | 11 | import org.javafunk.funk.behaviours.ordinals.Second; 12 | import org.javafunk.funk.behaviours.ordinals.mappables.MappableSecond; 13 | import org.javafunk.funk.functors.functions.UnaryFunction; 14 | 15 | import static org.javafunk.funk.Literals.iterableBuilderFrom; 16 | 17 | public class Pair 18 | extends Single 19 | implements Second, 20 | MappableSecond> { 21 | private S second; 22 | 23 | public static Pair pair(R first, S second) { 24 | return new Pair(first, second); 25 | } 26 | 27 | public Pair(R first, S second) { 28 | super(first); 29 | this.second = second; 30 | } 31 | 32 | @Override public S getSecond() { 33 | return second; 34 | } 35 | 36 | @Override public Pair mapFirst(UnaryFunction function) { 37 | return pair(function.call(getFirst()), getSecond()); 38 | } 39 | 40 | @Override public Pair mapSecond(UnaryFunction function) { 41 | return pair(getFirst(), function.call(getSecond())); 42 | } 43 | 44 | @Override public Iterable getValues() { 45 | return iterableBuilderFrom(super.getValues()).with(getSecond()).build(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/datastructures/tuples/Quadruple.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.datastructures.tuples; 10 | 11 | import org.javafunk.funk.behaviours.ordinals.Fourth; 12 | import org.javafunk.funk.behaviours.ordinals.mappables.MappableFourth; 13 | import org.javafunk.funk.functors.functions.UnaryFunction; 14 | 15 | import static org.javafunk.funk.Literals.iterableBuilderFrom; 16 | 17 | public class Quadruple 18 | extends Triple 19 | implements Fourth, 20 | MappableFourth> { 21 | private U fourth; 22 | 23 | public static Quadruple quadruple(R first, S second, T third, U fourth) { 24 | return new Quadruple(first, second, third, fourth); 25 | } 26 | 27 | public Quadruple(R first, S second, T third, U fourth) { 28 | super(first, second, third); 29 | this.fourth = fourth; 30 | } 31 | 32 | @Override public U getFourth() { 33 | return fourth; 34 | } 35 | 36 | @Override public Quadruple mapFirst(UnaryFunction function) { 37 | return quadruple(function.call(getFirst()), getSecond(), getThird(), getFourth()); 38 | } 39 | 40 | @Override public Quadruple mapSecond(UnaryFunction function) { 41 | return quadruple(getFirst(), function.call(getSecond()), getThird(), getFourth()); 42 | } 43 | 44 | @Override public Quadruple mapThird(UnaryFunction function) { 45 | return quadruple(getFirst(), getSecond(), function.call(getThird()), getFourth()); 46 | } 47 | 48 | @Override public Quadruple mapFourth(UnaryFunction function) { 49 | return quadruple(getFirst(), getSecond(), getThird(), function.call(getFourth())); 50 | } 51 | 52 | @Override public Iterable getValues() { 53 | return iterableBuilderFrom(super.getValues()).with(getFourth()).build(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/datastructures/tuples/Quintuple.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.datastructures.tuples; 10 | 11 | import org.javafunk.funk.behaviours.ordinals.Fifth; 12 | import org.javafunk.funk.behaviours.ordinals.mappables.MappableFifth; 13 | import org.javafunk.funk.functors.functions.UnaryFunction; 14 | 15 | import static org.javafunk.funk.Literals.iterableBuilderFrom; 16 | 17 | public class Quintuple 18 | extends Quadruple 19 | implements Fifth, 20 | MappableFifth> { 21 | private final V fifth; 22 | 23 | public static Quintuple quintuple(R first, S second, T third, U fourth, V fifth) { 24 | return new Quintuple(first, second, third, fourth, fifth); 25 | } 26 | 27 | public Quintuple(R first, S second, T third, U fourth, V fifth) { 28 | super(first, second, third, fourth); 29 | this.fifth = fifth; 30 | } 31 | 32 | @Override public V getFifth() { 33 | return fifth; 34 | } 35 | 36 | @Override public Quintuple mapFirst(UnaryFunction function) { 37 | return quintuple(function.call(getFirst()), getSecond(), getThird(), getFourth(), getFifth()); 38 | } 39 | 40 | @Override public Quintuple mapSecond(UnaryFunction function) { 41 | return quintuple(getFirst(), function.call(getSecond()), getThird(), getFourth(), getFifth()); 42 | } 43 | 44 | @Override public Quintuple mapThird(UnaryFunction function) { 45 | return quintuple(getFirst(), getSecond(), function.call(getThird()), getFourth(), getFifth()); 46 | } 47 | 48 | @Override public Quintuple mapFourth(UnaryFunction function) { 49 | return quintuple(getFirst(), getSecond(), getThird(), function.call(getFourth()), getFifth()); 50 | } 51 | 52 | @Override public Quintuple mapFifth(UnaryFunction function) { 53 | return quintuple(getFirst(), getSecond(), getThird(), getFourth(), function.call(getFifth())); 54 | } 55 | 56 | @Override public Iterable getValues() { 57 | return iterableBuilderFrom(super.getValues()).with(getFifth()).build(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/datastructures/tuples/Single.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.datastructures.tuples; 10 | 11 | import org.javafunk.funk.behaviours.ordinals.First; 12 | import org.javafunk.funk.behaviours.ordinals.mappables.MappableFirst; 13 | import org.javafunk.funk.functors.functions.UnaryFunction; 14 | 15 | import static org.javafunk.funk.Literals.iterableBuilderOf; 16 | 17 | public class Single 18 | extends AbstractTuple 19 | implements First, 20 | MappableFirst> { 21 | private R first; 22 | 23 | public static Single single(R first) { 24 | return new Single(first); 25 | } 26 | 27 | public Single(R first) { 28 | this.first = first; 29 | } 30 | 31 | @Override public R getFirst() { 32 | return first; 33 | } 34 | 35 | @Override public Single mapFirst(UnaryFunction mapper) { 36 | return single(mapper.call(first)); 37 | } 38 | 39 | @Override public Iterable getValues() { 40 | return iterableBuilderOf(Object.class).with(first).build(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/datastructures/tuples/Triple.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.datastructures.tuples; 10 | 11 | import org.javafunk.funk.behaviours.ordinals.Third; 12 | import org.javafunk.funk.behaviours.ordinals.mappables.MappableThird; 13 | import org.javafunk.funk.functors.functions.UnaryFunction; 14 | 15 | import static org.javafunk.funk.Literals.iterableBuilderFrom; 16 | 17 | public class Triple 18 | extends Pair 19 | implements Third, 20 | MappableThird> { 21 | private T third; 22 | 23 | public static Triple triple(R first, S second, T third) { 24 | return new Triple(first, second, third); 25 | } 26 | 27 | public Triple(R first, S second, T third) { 28 | super(first, second); 29 | this.third = third; 30 | } 31 | 32 | @Override public T getThird() { 33 | return third; 34 | } 35 | 36 | @Override public Triple mapFirst(UnaryFunction function) { 37 | return triple(function.call(getFirst()), getSecond(), getThird()); 38 | } 39 | 40 | @Override public Triple mapSecond(UnaryFunction function) { 41 | return triple(getFirst(), function.call(getSecond()), getThird()); 42 | } 43 | 44 | 45 | @Override public Triple mapThird(UnaryFunction function) { 46 | return triple(getFirst(), getSecond(), function.call(getThird())); 47 | } 48 | 49 | @Override public Iterable getValues() { 50 | return iterableBuilderFrom(super.getValues()).with(getThird()).build(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/Action.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors; 10 | 11 | import org.javafunk.funk.functors.procedures.UnaryProcedure; 12 | 13 | public abstract class Action implements UnaryProcedure { 14 | public abstract void on(T input); 15 | 16 | @Override public void execute(T input) { 17 | on(input); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/Equivalence.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors; 10 | 11 | import org.javafunk.funk.functors.predicates.BinaryPredicate; 12 | 13 | public abstract class Equivalence implements BinaryPredicate { 14 | public abstract boolean equal(T first, T second); 15 | 16 | @Override public boolean evaluate(T first, T second) { 17 | return equal(first, second); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/Factory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors; 10 | 11 | import org.javafunk.funk.functors.functions.NullaryFunction; 12 | 13 | public abstract class Factory implements NullaryFunction { 14 | public abstract T create(); 15 | 16 | @Override public T call() { 17 | return create(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/Indexer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors; 10 | 11 | import org.javafunk.funk.functors.functions.UnaryFunction; 12 | 13 | public abstract class Indexer implements UnaryFunction { 14 | public abstract O index(I item); 15 | 16 | @Override public O call(I item) { 17 | return index(item); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/Mapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors; 10 | 11 | import org.javafunk.funk.functors.functions.UnaryFunction; 12 | 13 | public abstract class Mapper implements UnaryFunction { 14 | public abstract T map(S input); 15 | 16 | @Override public T call(S input) { 17 | return map(input); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/Predicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors; 10 | 11 | import org.javafunk.funk.functors.predicates.UnaryPredicate; 12 | 13 | public interface Predicate extends UnaryPredicate {} -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/Reducer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors; 10 | 11 | import org.javafunk.funk.functors.functions.BinaryFunction; 12 | 13 | public abstract class Reducer implements BinaryFunction { 14 | public abstract T accumulate(T accumulator, S element); 15 | 16 | @Override public T call(T accumulator, S element) { 17 | return accumulate(accumulator, element); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/functions/BinaryFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.functions; 10 | 11 | public interface BinaryFunction { 12 | R call(A firstArgument, 13 | B secondArgument); 14 | } 15 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/functions/NonaryFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.functions; 10 | 11 | public interface NonaryFunction { 12 | R call(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument, 15 | D fourthArgument, 16 | E fifthArgument, 17 | F sixthArgument, 18 | G seventhArgument, 19 | H eighthArgument, 20 | I ninthArgument); 21 | } 22 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/functions/NullaryFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.functions; 10 | 11 | public interface NullaryFunction { 12 | R call(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/functions/OctaryFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.functions; 10 | 11 | public interface OctaryFunction { 12 | R call(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument, 15 | D fourthArgument, 16 | E fifthArgument, 17 | F sixthArgument, 18 | G seventhArgument, 19 | H eighthArgument); 20 | } 21 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/functions/QuaternaryFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.functions; 10 | 11 | public interface QuaternaryFunction { 12 | R call(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument, 15 | D fourthArgument); 16 | } 17 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/functions/QuinaryFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.functions; 10 | 11 | public interface QuinaryFunction { 12 | R call(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument, 15 | D fourthArgument, 16 | E fifthArgument); 17 | } 18 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/functions/SenaryFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.functions; 10 | 11 | public interface SenaryFunction { 12 | R call(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument, 15 | D fourthArgument, 16 | E fifthArgument, 17 | F sixthArgument); 18 | } 19 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/functions/SeptenaryFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.functions; 10 | 11 | public interface SeptenaryFunction { 12 | R call(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument, 15 | D fourthArgument, 16 | E fifthArgument, 17 | F sixthArgument, 18 | G seventhArgument); 19 | } 20 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/functions/TernaryFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.functions; 10 | 11 | public interface TernaryFunction { 12 | R call(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument); 15 | } 16 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/functions/UnaryFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.functions; 10 | 11 | public interface UnaryFunction { 12 | R call(A firstArgument); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/predicates/BinaryPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.predicates; 10 | 11 | public interface BinaryPredicate { 12 | boolean evaluate(A firstInput, B secondInput); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/predicates/NonaryPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.predicates; 10 | 11 | public interface NonaryPredicate { 12 | boolean evaluate(A firstInput, B secondInput, C thirdInput, 13 | D fourthInput, E fifthInput, F sixthInput, 14 | G seventhInput, H octaryInput, I ninthInput); 15 | } 16 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/predicates/NullaryPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.predicates; 10 | 11 | public interface NullaryPredicate { 12 | boolean evaluate(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/predicates/OctaryPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.predicates; 10 | 11 | public interface OctaryPredicate { 12 | boolean evaluate(A firstInput, B secondInput, C thirdInput, 13 | D fourthInput, E fifthInput, F sixthInput, 14 | G seventhInput, H octaryInput); 15 | } 16 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/predicates/QuaternaryPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.predicates; 10 | 11 | public interface QuaternaryPredicate { 12 | boolean evaluate(A firstInput, B secondInput, C thirdInput, 13 | D fourthInput); 14 | } 15 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/predicates/QuinaryPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.predicates; 10 | 11 | public interface QuinaryPredicate { 12 | boolean evaluate(A firstInput, B secondInput, C thirdInput, 13 | D fourthInput, E fifthInput); 14 | } 15 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/predicates/SenaryPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.predicates; 10 | 11 | public interface SenaryPredicate { 12 | boolean evaluate(A firstInput, B secondInput, C thirdInput, 13 | D fourthInput, E fifthInput, F sixthInput); 14 | } 15 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/predicates/SeptenaryPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.predicates; 10 | 11 | public interface SeptenaryPredicate { 12 | boolean evaluate(A firstInput, B secondInput, C thirdInput, 13 | D fourthInput, E fifthInput, F sixthInput, 14 | G seventhInput); 15 | } 16 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/predicates/TernaryPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.predicates; 10 | 11 | public interface TernaryPredicate { 12 | boolean evaluate(A firstInput, B secondInput, C thirdInput); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/predicates/UnaryPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.predicates; 10 | 11 | public interface UnaryPredicate { 12 | boolean evaluate(A firstInput); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/procedures/BinaryProcedure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.procedures; 10 | 11 | public interface BinaryProcedure { 12 | void execute(A firstArgument, 13 | B secondArgument); 14 | } 15 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/procedures/NonaryProcedure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.procedures; 10 | 11 | public interface NonaryProcedure { 12 | void execute(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument, 15 | D fourthArgument, 16 | E fifthArgument, 17 | F sixthArgument, 18 | G seventhArgument, 19 | H eighthArgument, 20 | I ninthArgument); 21 | } 22 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/procedures/NullaryProcedure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.procedures; 10 | 11 | public interface NullaryProcedure { 12 | void execute(); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/procedures/OctaryProcedure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.procedures; 10 | 11 | public interface OctaryProcedure { 12 | void execute(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument, 15 | D fourthArgument, 16 | E fifthArgument, 17 | F sixthArgument, 18 | G seventhArgument, 19 | H eighthArgument); 20 | } 21 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/procedures/QuaternaryProcedure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.procedures; 10 | 11 | public interface QuaternaryProcedure { 12 | void execute(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument, 15 | D fourthArgument); 16 | } 17 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/procedures/QuinaryProcedure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.procedures; 10 | 11 | public interface QuinaryProcedure { 12 | void execute(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument, 15 | D fourthArgument, 16 | E fifthArgument); 17 | } 18 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/procedures/SenaryProcedure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.procedures; 10 | 11 | public interface SenaryProcedure { 12 | void execute(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument, 15 | D fourthArgument, 16 | E fifthArgument, 17 | F sixthArgument); 18 | } 19 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/procedures/SeptenaryProcedure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.procedures; 10 | 11 | public interface SeptenaryProcedure { 12 | void execute(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument, 15 | D fourthArgument, 16 | E fifthArgument, 17 | F sixthArgument, 18 | G seventhArgument); 19 | } 20 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/procedures/TernaryProcedure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.procedures; 10 | 11 | public interface TernaryProcedure { 12 | void execute(A firstArgument, 13 | B secondArgument, 14 | C thirdArgument); 15 | } 16 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/functors/procedures/UnaryProcedure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.functors.procedures; 10 | 11 | public interface UnaryProcedure { 12 | void execute(A firstArgument); 13 | } 14 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/generators/AbstractGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.generators; 10 | 11 | import org.javafunk.funk.behaviours.Generator; 12 | 13 | public abstract class AbstractGenerator implements Generator { 14 | @Override public boolean hasNext() { 15 | return true; 16 | } 17 | 18 | @Override public void remove() { 19 | throw new UnsupportedOperationException(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/generators/ConstantGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.generators; 10 | 11 | import org.apache.commons.lang3.builder.EqualsBuilder; 12 | import org.apache.commons.lang3.builder.HashCodeBuilder; 13 | import org.apache.commons.lang3.builder.ToStringBuilder; 14 | 15 | import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE; 16 | 17 | public class ConstantGenerator extends AbstractGenerator { 18 | private final T value; 19 | 20 | public ConstantGenerator(T value) { 21 | this.value = value; 22 | } 23 | 24 | @Override public T next() { 25 | return value; 26 | } 27 | 28 | @Override public boolean equals(Object other) { 29 | return EqualsBuilder.reflectionEquals(this, other); 30 | } 31 | 32 | @Override public int hashCode() { 33 | return HashCodeBuilder.reflectionHashCode(this); 34 | } 35 | 36 | @Override public String toString() { 37 | return ToStringBuilder.reflectionToString(this, SHORT_PREFIX_STYLE); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/generators/CyclicGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.generators; 10 | 11 | import org.apache.commons.lang3.builder.EqualsBuilder; 12 | import org.apache.commons.lang3.builder.HashCodeBuilder; 13 | import org.apache.commons.lang3.builder.ToStringBuilder; 14 | 15 | import java.util.Iterator; 16 | 17 | import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE; 18 | import static org.javafunk.funk.Lazily.cycle; 19 | 20 | public class CyclicGenerator extends AbstractGenerator { 21 | private static final String[] excludedFields = new String[]{"iterator"}; 22 | 23 | private final Iterator iterator; 24 | 25 | // These fields are required for equality. 26 | private final Iterable iterable; 27 | private T mostRecentElement; 28 | 29 | public CyclicGenerator(Iterable iterable) { 30 | this.iterable = iterable; 31 | this.iterator = cycle(iterable).iterator(); 32 | } 33 | 34 | @Override public T next() { 35 | mostRecentElement = iterator.next(); 36 | return mostRecentElement; 37 | } 38 | 39 | @Override public boolean equals(Object other) { 40 | return EqualsBuilder.reflectionEquals(this, other, excludedFields); 41 | } 42 | 43 | @Override public int hashCode() { 44 | return HashCodeBuilder.reflectionHashCode(this); 45 | } 46 | 47 | @Override public String toString() { 48 | return ToStringBuilder.reflectionToString(this, SHORT_PREFIX_STYLE); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/generators/FiniteGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.generators; 10 | 11 | import org.apache.commons.lang3.builder.EqualsBuilder; 12 | import org.apache.commons.lang3.builder.HashCodeBuilder; 13 | import org.apache.commons.lang3.builder.ToStringBuilder; 14 | 15 | import java.util.Iterator; 16 | 17 | import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE; 18 | 19 | public class FiniteGenerator extends AbstractGenerator { 20 | private static final String[] excludedFields = new String[]{"iterator"}; 21 | 22 | private final Iterator iterator; 23 | 24 | // These fields are required for equality. 25 | private final Iterable iterable; 26 | private T mostRecentElement; 27 | 28 | public FiniteGenerator(Iterable iterable) { 29 | this.iterable = iterable; 30 | this.iterator = iterable.iterator(); 31 | } 32 | 33 | @Override public boolean hasNext() { 34 | return iterator.hasNext(); 35 | } 36 | 37 | @Override public T next() { 38 | mostRecentElement = iterator.next(); 39 | return mostRecentElement; 40 | } 41 | 42 | @Override public boolean equals(Object other) { 43 | return EqualsBuilder.reflectionEquals(this, other, excludedFields); 44 | } 45 | 46 | @Override public int hashCode() { 47 | return HashCodeBuilder.reflectionHashCode(this, excludedFields); 48 | } 49 | 50 | @Override public String toString() { 51 | return ToStringBuilder.reflectionToString(this, SHORT_PREFIX_STYLE); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/generators/RandomGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.generators; 10 | 11 | import org.apache.commons.lang3.builder.EqualsBuilder; 12 | import org.apache.commons.lang3.builder.HashCodeBuilder; 13 | import org.apache.commons.lang3.builder.ToStringBuilder; 14 | 15 | import java.util.List; 16 | import java.util.Random; 17 | 18 | import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE; 19 | import static org.javafunk.funk.Iterables.asList; 20 | 21 | public class RandomGenerator extends AbstractGenerator { 22 | private static final String[] excludedFields = new String[]{"random"}; 23 | 24 | private final List elements; 25 | private final Random random; 26 | 27 | public RandomGenerator(Iterable iterable) { 28 | this(iterable, new Random()); 29 | } 30 | 31 | public RandomGenerator(Iterable iterable, Random random) { 32 | this.random = random; 33 | this.elements = asList(iterable); 34 | } 35 | 36 | @Override public T next() { 37 | return elements.get(random.nextInt(elements.size())); 38 | } 39 | 40 | @Override public boolean equals(Object other) { 41 | return EqualsBuilder.reflectionEquals(this, other, excludedFields); 42 | } 43 | 44 | @Override public int hashCode() { 45 | return HashCodeBuilder.reflectionHashCode(this); 46 | } 47 | 48 | @Override public String toString() { 49 | return ToStringBuilder.reflectionToString(this, SHORT_PREFIX_STYLE); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/iterators/BatchedIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.iterators; 10 | 11 | import org.apache.commons.lang3.builder.ToStringBuilder; 12 | import org.apache.commons.lang3.builder.ToStringStyle; 13 | 14 | import java.util.ArrayList; 15 | import java.util.Iterator; 16 | import java.util.List; 17 | import java.util.NoSuchElementException; 18 | 19 | import static com.google.common.base.Preconditions.checkNotNull; 20 | import static java.util.Collections.unmodifiableList; 21 | 22 | public class BatchedIterator implements Iterator> { 23 | private Iterator iterator; 24 | private int batchSize; 25 | 26 | public BatchedIterator(Iterator iterator, int batchSize) { 27 | this.iterator = checkNotNull(iterator); 28 | if (batchSize <= 0) { 29 | throw new IllegalArgumentException("Batch size must be greater than zero."); 30 | } 31 | this.batchSize = batchSize; 32 | } 33 | 34 | @Override 35 | public boolean hasNext() { 36 | return iterator.hasNext(); 37 | } 38 | 39 | @Override 40 | public Iterable next() { 41 | if (hasNext()) { 42 | List nextBatch = new ArrayList(); 43 | for (int i = 0; i < batchSize; i++) { 44 | if (iterator.hasNext()) { 45 | nextBatch.add(iterator.next()); 46 | } 47 | } 48 | return unmodifiableList(nextBatch); 49 | } else { 50 | throw new NoSuchElementException(); 51 | } 52 | } 53 | 54 | @Override 55 | public void remove() { 56 | throw new UnsupportedOperationException(); 57 | } 58 | 59 | @Override public String toString() { 60 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 61 | .append("batchSize", batchSize) 62 | .append("iterator", iterator.toString()) 63 | .toString(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/iterators/CachingIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.iterators; 10 | 11 | import java.util.Iterator; 12 | import java.util.NoSuchElementException; 13 | 14 | abstract class CachingIterator implements Iterator { 15 | private IteratorCache matchCache = new IteratorCache(); 16 | private IteratorRemovalFlag removalFlag = new IteratorRemovalFlag(); 17 | 18 | @Override 19 | public boolean hasNext() { 20 | if (matchCache.isPopulated()) { 21 | return true; 22 | } else { 23 | try { 24 | matchCache.store(findNext()); 25 | removalFlag.disable(); 26 | return true; 27 | } catch (NoSuchElementException exception) { 28 | return false; 29 | } 30 | } 31 | } 32 | 33 | @Override 34 | public T next() { 35 | if (matchCache.isPopulated()) { 36 | removalFlag.enable(); 37 | return matchCache.fetch(); 38 | } else { 39 | T next = findNext(); 40 | removalFlag.enable(); 41 | return next; 42 | } 43 | } 44 | 45 | @Override 46 | public void remove() { 47 | if (removalFlag.isEnabled()) { 48 | removalFlag.disable(); 49 | removeLast(); 50 | } else { 51 | throw new IllegalStateException(); 52 | } 53 | } 54 | 55 | protected abstract T findNext(); 56 | 57 | protected abstract void removeLast(); 58 | 59 | private static class IteratorCache { 60 | private T match; 61 | private boolean hasMatch = false; 62 | 63 | void store(T match) { 64 | this.hasMatch = true; 65 | this.match = match; 66 | } 67 | 68 | T fetch() { 69 | this.hasMatch = false; 70 | return this.match; 71 | } 72 | 73 | boolean isPopulated() { 74 | return this.hasMatch; 75 | } 76 | 77 | } 78 | 79 | private static class IteratorRemovalFlag { 80 | private boolean canRemove = false; 81 | 82 | boolean isEnabled() { 83 | return canRemove; 84 | } 85 | 86 | void enable() { 87 | this.canRemove = true; 88 | } 89 | 90 | void disable() { 91 | this.canRemove = false; 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/iterators/ComprehensionIterator.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.iterators; 2 | 3 | import org.apache.commons.lang3.builder.ToStringBuilder; 4 | import org.apache.commons.lang3.builder.ToStringStyle; 5 | import org.javafunk.funk.Eagerly; 6 | import org.javafunk.funk.functors.functions.UnaryFunction; 7 | import org.javafunk.funk.functors.predicates.UnaryPredicate; 8 | 9 | import java.util.Iterator; 10 | import java.util.NoSuchElementException; 11 | 12 | import static com.google.common.base.Preconditions.checkNotNull; 13 | import static org.javafunk.funk.Checks.returnOrThrowIfContainsNull; 14 | 15 | public class ComprehensionIterator extends CachingIterator { 16 | private UnaryFunction mapper; 17 | private Iterator iterator; 18 | private Iterable> predicates; 19 | 20 | public ComprehensionIterator( 21 | UnaryFunction mapper, 22 | Iterator iterator, 23 | Iterable> predicates) { 24 | this.mapper = checkNotNull(mapper); 25 | this.iterator = checkNotNull(iterator); 26 | this.predicates = returnOrThrowIfContainsNull(predicates); 27 | } 28 | 29 | @Override 30 | protected T findNext() { 31 | while (iterator.hasNext()) { 32 | final S next = iterator.next(); 33 | Boolean passesAllPredicates = Eagerly.all( 34 | predicates, 35 | new UnaryPredicate>() { 36 | @Override 37 | public boolean evaluate(UnaryPredicate predicate) { 38 | return predicate.evaluate(next); 39 | } 40 | }); 41 | if (passesAllPredicates) { 42 | return mapper.call(next); 43 | } 44 | 45 | } 46 | throw new NoSuchElementException(); 47 | } 48 | 49 | @Override 50 | protected void removeLast() { 51 | iterator.remove(); 52 | } 53 | 54 | @Override public String toString() { 55 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 56 | .append("mapper", mapper) 57 | .append("iterator", iterator) 58 | .append("predicates", predicates) 59 | .toString(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/iterators/EachIterator.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.iterators; 2 | 3 | import org.apache.commons.lang3.builder.ToStringBuilder; 4 | import org.apache.commons.lang3.builder.ToStringStyle; 5 | import org.javafunk.funk.functors.procedures.UnaryProcedure; 6 | 7 | import java.util.Iterator; 8 | 9 | import static com.google.common.base.Preconditions.checkNotNull; 10 | 11 | public class EachIterator implements Iterator { 12 | private Iterator iterator; 13 | private UnaryProcedure procedure; 14 | 15 | public EachIterator(Iterator iterator, UnaryProcedure procedure) { 16 | this.iterator = checkNotNull(iterator); 17 | this.procedure = checkNotNull(procedure); 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public T next() { 27 | T next = iterator.next(); 28 | procedure.execute(next); 29 | return next; 30 | } 31 | 32 | @Override 33 | public void remove() { 34 | throw new UnsupportedOperationException(); 35 | } 36 | 37 | @Override public String toString() { 38 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 39 | .append("iterator", iterator) 40 | .append("procedure", procedure) 41 | .toString(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/iterators/FilteredIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.iterators; 10 | 11 | import org.apache.commons.lang3.builder.ToStringBuilder; 12 | import org.apache.commons.lang3.builder.ToStringStyle; 13 | import org.javafunk.funk.functors.predicates.UnaryPredicate; 14 | 15 | import java.util.Iterator; 16 | import java.util.NoSuchElementException; 17 | 18 | import static com.google.common.base.Preconditions.checkNotNull; 19 | 20 | public class FilteredIterator extends CachingIterator { 21 | private Iterator iterator; 22 | private UnaryPredicate predicate; 23 | 24 | public FilteredIterator(Iterator iterator, UnaryPredicate predicate) { 25 | this.iterator = checkNotNull(iterator); 26 | this.predicate = checkNotNull(predicate); 27 | } 28 | 29 | @Override 30 | protected T findNext() { 31 | while (iterator.hasNext()) { 32 | T next = iterator.next(); 33 | if (predicate.evaluate(next)) { 34 | return next; 35 | } 36 | } 37 | throw new NoSuchElementException(); 38 | } 39 | 40 | @Override 41 | protected void removeLast() { 42 | iterator.remove(); 43 | } 44 | 45 | @Override public String toString() { 46 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 47 | .append("iterator", iterator) 48 | .append("predicate", predicate) 49 | .toString(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/iterators/FunctionBackedIterator.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.iterators; 2 | 3 | import org.javafunk.funk.functors.functions.NullaryFunction; 4 | 5 | import java.util.Iterator; 6 | 7 | public class FunctionBackedIterator implements Iterator { 8 | private final NullaryFunction function; 9 | 10 | public FunctionBackedIterator(NullaryFunction function) { 11 | this.function = function; 12 | } 13 | 14 | @Override public boolean hasNext() { 15 | return true; 16 | } 17 | 18 | @Override public T next() { 19 | return function.call(); 20 | } 21 | 22 | @Override public void remove() { 23 | throw new UnsupportedOperationException(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/iterators/MappedIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.iterators; 10 | 11 | import org.apache.commons.lang3.builder.ToStringBuilder; 12 | import org.apache.commons.lang3.builder.ToStringStyle; 13 | import org.javafunk.funk.functors.functions.UnaryFunction; 14 | 15 | import java.util.Iterator; 16 | 17 | import static com.google.common.base.Preconditions.checkNotNull; 18 | 19 | public class MappedIterator implements Iterator { 20 | private Iterator iterator; 21 | private UnaryFunction function; 22 | 23 | public MappedIterator(Iterator iterator, UnaryFunction mapper) { 24 | this.iterator = checkNotNull(iterator); 25 | this.function = checkNotNull(mapper); 26 | } 27 | 28 | public boolean hasNext() { 29 | return iterator.hasNext(); 30 | } 31 | 32 | public T next() { 33 | return function.call(iterator.next()); 34 | } 35 | 36 | public void remove() { 37 | iterator.remove(); 38 | } 39 | 40 | @Override public String toString() { 41 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 42 | .append("iterator", iterator) 43 | .append("mappingFunction", function) 44 | .toString(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/iterators/PredicatedIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.iterators; 10 | 11 | import org.apache.commons.lang3.builder.ToStringBuilder; 12 | import org.apache.commons.lang3.builder.ToStringStyle; 13 | import org.javafunk.funk.functors.predicates.UnaryPredicate; 14 | 15 | import java.util.Iterator; 16 | import java.util.NoSuchElementException; 17 | 18 | import static com.google.common.base.Preconditions.checkNotNull; 19 | 20 | public class PredicatedIterator extends CachingIterator { 21 | private Iterator iterator; 22 | private UnaryPredicate predicate; 23 | 24 | public PredicatedIterator(Iterator iterator, UnaryPredicate predicate) { 25 | this.iterator = checkNotNull(iterator); 26 | this.predicate = checkNotNull(predicate); 27 | } 28 | 29 | @Override 30 | protected T findNext() { 31 | if (iterator.hasNext()) { 32 | T next = iterator.next(); 33 | if (predicate.evaluate(next)) { 34 | return next; 35 | } 36 | } 37 | throw new NoSuchElementException(); 38 | } 39 | 40 | @Override 41 | protected void removeLast() { 42 | iterator.remove(); 43 | } 44 | 45 | @Override public String toString() { 46 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 47 | .append("iterator", iterator) 48 | .append("predicate", predicate) 49 | .toString(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/iterators/ZippedIterator.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.iterators; 2 | 3 | import org.apache.commons.lang3.builder.ToStringBuilder; 4 | import org.apache.commons.lang3.builder.ToStringStyle; 5 | import org.javafunk.funk.Eagerly; 6 | import org.javafunk.funk.functors.Mapper; 7 | import org.javafunk.funk.functors.Predicate; 8 | 9 | import java.util.Iterator; 10 | import java.util.NoSuchElementException; 11 | 12 | import static com.google.common.base.Preconditions.checkNotNull; 13 | import static org.javafunk.funk.Checks.returnOrThrowIfContainsNull; 14 | 15 | public class ZippedIterator implements Iterator> { 16 | private final Iterable> iterators; 17 | 18 | public ZippedIterator(Iterable> iterators) { 19 | this.iterators = returnOrThrowIfContainsNull(checkNotNull(iterators)); 20 | } 21 | 22 | public boolean hasNext() { 23 | return Eagerly.all(iterators, new Predicate>() { 24 | public boolean evaluate(Iterator iterator) { 25 | return iterator.hasNext(); 26 | } 27 | }); 28 | } 29 | 30 | public Iterable next() { 31 | if (hasNext()) { 32 | return Eagerly.map(iterators, new Mapper, Object>() { 33 | public Object map(Iterator iterator) { 34 | return iterator.next(); 35 | } 36 | }); 37 | } else { 38 | throw new NoSuchElementException(); 39 | } 40 | } 41 | 42 | public void remove() { 43 | throw new UnsupportedOperationException(); 44 | } 45 | 46 | @Override public String toString() { 47 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 48 | .append("iterators", iterators) 49 | .toString(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/predicates/EqualsPredicate.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.predicates; 2 | 3 | import com.google.common.base.Objects; 4 | import org.apache.commons.lang3.builder.EqualsBuilder; 5 | import org.apache.commons.lang3.builder.HashCodeBuilder; 6 | import org.javafunk.funk.functors.Predicate; 7 | 8 | /** 9 | * A {@code Predicate} implementation that returns {@code true} 10 | * if the evaluated object is equal to the object supplied 11 | * at construction according to that object's {@code equals} 12 | * method. 13 | * 14 | * @param The type of the element this {@code Predicate} 15 | * can evaluate. 16 | */ 17 | public class EqualsPredicate implements Predicate { 18 | private T controlObject; 19 | 20 | /** 21 | * Constructs an {@code EqualsPredicate} over the 22 | * supplied instance of type {@code T}. 23 | * 24 | * @param controlObject The object to which this {@code EqualsPredicate} 25 | * should compare all evaluated objects. 26 | */ 27 | public EqualsPredicate(T controlObject) { 28 | this.controlObject = controlObject; 29 | } 30 | 31 | /** 32 | * Evaluates the supplied instance of type {@code T} 33 | * against the control object with which this {@code Predicate} 34 | * was initialised. 35 | * 36 | * @param other The object to be equated. 37 | * @return A {@code boolean} representing whether or 38 | * not the supplied object is equal to the 39 | * control object specified at construction 40 | * of this {@code Predicate} according to 41 | * that object's {@code equals} method. 42 | */ 43 | @Override public boolean evaluate(T other) { 44 | return Objects.equal(controlObject, other); 45 | } 46 | 47 | @Override public boolean equals(Object other) { 48 | return EqualsBuilder.reflectionEquals(this, other); 49 | } 50 | 51 | @Override public int hashCode() { 52 | return HashCodeBuilder.reflectionHashCode(this); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/predicates/FalsePredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.predicates; 10 | 11 | import org.apache.commons.lang3.builder.EqualsBuilder; 12 | import org.apache.commons.lang3.builder.HashCodeBuilder; 13 | import org.javafunk.funk.functors.Predicate; 14 | 15 | /** 16 | * {@code FalsePredicate} is a {@code Predicate} implementation that 17 | * returns {@code false} regardless of the object being evaluated. 18 | * 19 | *

{@code FalsePredicate} equality is implemented according 20 | * to class alone, i.e., two distinct {@code FalsePredicate} instances 21 | * will be considered equal. Unfortunately, due to type erasure, 22 | * {@code new FalsePredicate().equals(new FalsePredicate())} 23 | * is {@code true} for all types {@code X} and {@code Y} which may 24 | * not be desired.

25 | * 26 | * @param The type of object this {@code FalsePredicate} can evaluate. 27 | */ 28 | public class FalsePredicate implements Predicate { 29 | /** 30 | * Evaluates the supplied instance of type {@code T} 31 | * and always returns {@code false} regardless of 32 | * the instance supplied. 33 | * 34 | * @param item An instance of type {@code T} to evaluate. 35 | * @return {@code false} always. 36 | */ 37 | @Override public boolean evaluate(T item) { 38 | return false; 39 | } 40 | 41 | /** 42 | * Implements class equality for {@code FalsePredicate} instances. 43 | * Two {@code FalsePredicate}s are always considered equal. 44 | * 45 | *

Due to type erasure, {@code new FalsePredicate().equals(new FalsePredicate()} 46 | * will be {@code true} for all types {@code X} and {@code Y}.

47 | * 48 | * @param other The object to check for equality to this {@code FalsePredicate}. 49 | * @return {@code true} if and only if the supplied object is also 50 | * a {@code FalsePredicate}, otherwise {@code false}. 51 | */ 52 | @Override public boolean equals(Object other) { 53 | return EqualsBuilder.reflectionEquals(this, other); 54 | } 55 | 56 | /** 57 | * Two {@code FalsePredicate} instances will always have equal 58 | * hash codes. 59 | * 60 | * @return The hash code of this {@code FalsePredicate}. 61 | */ 62 | @Override 63 | public int hashCode() { 64 | return HashCodeBuilder.reflectionHashCode(this); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /funk-core/src/main/java/org/javafunk/funk/predicates/TruePredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.predicates; 10 | 11 | import org.apache.commons.lang3.builder.EqualsBuilder; 12 | import org.apache.commons.lang3.builder.HashCodeBuilder; 13 | import org.javafunk.funk.functors.Predicate; 14 | 15 | /** 16 | * {@code TruePredicate} is a {@code Predicate} implementation that 17 | * returns {@code true} regardless of the object being evaluated. 18 | * 19 | *

{@code TruePredicate} equality is implemented according 20 | * to class alone, i.e., two distinct {@code TruePredicate} instances 21 | * will be considered equal. Unfortunately, due to type erasure, 22 | * {@code new TruePredicate().equals(new TruePredicate())} 23 | * is {@code true} for all types {@code X} and {@code Y} which may 24 | * not be desired.

25 | * 26 | * @param The type of object this {@code TruePredicate} can evaluate. 27 | */ 28 | public class TruePredicate implements Predicate { 29 | /** 30 | * Evaluates the supplied instance of type {@code T} 31 | * and always returns {@code true} regardless of 32 | * the instance supplied. 33 | * 34 | * @param item An instance of type {@code T} to evaluate. 35 | * @return {@code true} always. 36 | */ 37 | @Override public boolean evaluate(T item) { 38 | return true; 39 | } 40 | 41 | /** 42 | * Implements class equality for {@code TruePredicate} instances. 43 | * Two {@code TruePredicate}s are always considered equal. 44 | * 45 | *

Due to type erasure, {@code new TruePredicate().equals(new TruePredicate()} 46 | * will be {@code true} for all types {@code X} and {@code Y}.

47 | * 48 | * @param other The object to check for equality to this {@code TruePredicate}. 49 | * @return {@code true} if and only if the supplied object is also 50 | * a {@code TruePredicate}, otherwise {@code false}. 51 | */ 52 | @Override public boolean equals(Object other) { 53 | return EqualsBuilder.reflectionEquals(this, other); 54 | } 55 | 56 | /** 57 | * Two {@code TruePredicate} instances will always have equal 58 | * hash codes. 59 | * 60 | * @return The hash code of this {@code TruePredicate}. 61 | */ 62 | @Override public int hashCode() { 63 | return HashCodeBuilder.reflectionHashCode(this); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/ArraysTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import org.junit.Test; 12 | 13 | import java.util.Iterator; 14 | import java.util.List; 15 | 16 | import static org.hamcrest.MatcherAssert.assertThat; 17 | import static org.hamcrest.Matchers.is; 18 | import static org.javafunk.funk.Literals.listWith; 19 | 20 | public class ArraysTest { 21 | @Test 22 | public void shouldConvertTheSuppliedArrayToAList() throws Exception { 23 | // Given 24 | String[] array = {"a", "b", "c"}; 25 | List expectedList = listWith("a", "b", "c"); 26 | 27 | // When 28 | List actualList = Arrays.asList(array); 29 | 30 | // Then 31 | assertThat(actualList, is(expectedList)); 32 | } 33 | 34 | @Test 35 | public void shouldConvertTheSuppliedArrayToAnIterable() throws Exception { 36 | // Given 37 | String[] array = {"a", "b", "c"}; 38 | 39 | // When 40 | Iterable iterable = Arrays.asIterable(array); 41 | Iterator iterator = iterable.iterator(); 42 | 43 | // Then 44 | assertThat(iterator.hasNext(), is(true)); 45 | assertThat(iterator.next(), is("a")); 46 | assertThat(iterator.hasNext(), is(true)); 47 | assertThat(iterator.next(), is("b")); 48 | assertThat(iterator.hasNext(), is(true)); 49 | assertThat(iterator.next(), is("c")); 50 | assertThat(iterator.hasNext(), is(false)); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/BigIntegersTest.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.junit.Test; 4 | 5 | import java.math.BigDecimal; 6 | import java.math.BigInteger; 7 | 8 | import static org.hamcrest.Matchers.is; 9 | import static org.javafunk.funk.BigIntegers.*; 10 | import static org.junit.Assert.assertThat; 11 | 12 | public class BigIntegersTest { 13 | @Test 14 | public void returnsMapperFromStringToBigInteger() { 15 | assertThat(fromStringToBigInteger().map("12345678"), is(new BigInteger("12345678"))); 16 | } 17 | 18 | @Test(expected = NullPointerException.class) 19 | public void throwsNullPointerExceptionWhenMapperFromStringToBigIntegerPassedNull() { 20 | fromStringToBigInteger().map(null); 21 | } 22 | 23 | @Test 24 | public void returnsMapperFromIntegerToBigInteger() { 25 | assertThat(fromIntegerToBigInteger().map(1234), is(new BigInteger("1234"))); 26 | } 27 | 28 | @Test(expected = NullPointerException.class) 29 | public void throwsNullPointerExceptionWhenMapperFromIntegerToBigIntegerPassedNull() { 30 | fromIntegerToBigInteger().map(null); 31 | } 32 | 33 | @Test 34 | public void returnsMapperFromLongToBigInteger() { 35 | assertThat(fromLongToBigInteger().map(1234L), is(new BigInteger("1234"))); 36 | } 37 | 38 | @Test(expected = NullPointerException.class) 39 | public void throwsNullPointerExceptionWhenMapperFromLongToBigIntegerPassedNull() { 40 | fromLongToBigInteger().map(null); 41 | } 42 | 43 | @Test 44 | public void returnsMapperFromFloatToBigInteger() { 45 | assertThat(fromFloatToBigInteger().map(1234.56F), is(new BigInteger("1234"))); 46 | } 47 | 48 | @Test(expected = NullPointerException.class) 49 | public void throwsNullPointerExceptionWhenMapperFromFloatToBigIntegerPassedNull() { 50 | fromFloatToBigInteger().map(null); 51 | } 52 | 53 | @Test 54 | public void returnsMapperFromDoubleToBigInteger() { 55 | assertThat(fromDoubleToBigInteger().map(1234.56D), is(new BigInteger("1234"))); 56 | } 57 | 58 | @Test(expected = NullPointerException.class) 59 | public void throwsNullPointerExceptionWhenMapperFromDoubleToBigIntegerPassedNull() { 60 | fromDoubleToBigInteger().map(null); 61 | } 62 | 63 | @Test 64 | public void returnsMapperFromBigDecimalToBigInteger() { 65 | assertThat(fromBigDecimalToBigInteger().map(new BigDecimal("1234.56")), is(new BigInteger("1234"))); 66 | } 67 | 68 | @Test(expected = NullPointerException.class) 69 | public void throwsNullPointerExceptionWhenMapperFromBigDecimalToBigIntegerPassedNull() { 70 | fromBigDecimalToBigInteger().map(null); 71 | } 72 | } -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/ChecksTest.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.junit.Test; 4 | 5 | import static junit.framework.Assert.fail; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | import static org.hamcrest.Matchers.instanceOf; 8 | import static org.hamcrest.Matchers.is; 9 | import static org.javafunk.funk.Exceptions.illegalArgumentFactory; 10 | import static org.javafunk.funk.Exceptions.runtimeFactory; 11 | import static org.javafunk.funk.Literals.iterable; 12 | import static org.javafunk.funk.Literals.iterableWith; 13 | 14 | public class ChecksTest { 15 | @Test 16 | public void shouldReturnTheIterableIfItIsNotEmpty() throws Exception { 17 | // Given 18 | Iterable input = iterableWith("one", "two"); 19 | 20 | // When 21 | Iterable output = Checks.returnOrThrowIfEmpty(input, runtimeFactory("Oops")); 22 | 23 | // Then 24 | assertThat(output, is(input)); 25 | } 26 | 27 | @Test 28 | public void shouldThrowTheSuppliedExceptionIfEmpty() throws Exception { 29 | // Given 30 | Iterable input = iterable(); 31 | 32 | try { 33 | // When 34 | Checks.returnOrThrowIfEmpty(input, illegalArgumentFactory("Oops")); 35 | fail("Expected the supplied exception to be thrown."); 36 | } catch (Throwable exception) { 37 | // Then 38 | assertThat(exception, instanceOf(IllegalArgumentException.class)); 39 | assertThat(exception.getMessage(), is("Oops")); 40 | } 41 | } 42 | 43 | @Test(expected = NullPointerException.class) 44 | public void shouldThrowANullPointerExceptionIfIterableSuppliedToEmptyCheckIsNull() throws Exception { 45 | // Given 46 | Iterable input = null; 47 | 48 | // When 49 | Checks.returnOrThrowIfEmpty(input, runtimeFactory()); 50 | 51 | // Then a NullPointerException is thrown 52 | } 53 | 54 | @Test 55 | public void shouldReturnIfContainsNoNullsCheckIsSuppliedEmptyIterable() throws Exception { 56 | // Given 57 | Iterable input = iterable(); 58 | 59 | // When 60 | Iterable output = Checks.returnOrThrowIfContainsNull(input); 61 | 62 | // Then 63 | assertThat(output, is(input)); 64 | } 65 | 66 | @Test(expected = NullPointerException.class) 67 | public void shouldThrowANullPointerExceptionIfIterableSuppliedToContainsNoNullsCheckIsNull() throws Exception { 68 | // Given 69 | Iterable input = null; 70 | 71 | // When 72 | Checks.returnOrThrowIfContainsNull(input); 73 | 74 | // Then a NullPointerException is thrown 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/ClassesTest.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.javafunk.funk.functors.functions.UnaryFunction; 4 | import org.junit.Test; 5 | 6 | public class ClassesTest { 7 | @Test(expected = NullPointerException.class) 8 | public void shouldThrowNullPointerExceptionIfNullUnaryFunctionSuppliedToUncheckedInstantiate() throws Exception { 9 | // Given 10 | Class classToInstantiate = String.class; 11 | UnaryFunction function = null; 12 | 13 | // When 14 | Classes.uncheckedInstantiate(classToInstantiate, function); 15 | 16 | // Then a NullPointerException is thrown. 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/DoublesTest.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.junit.Test; 4 | 5 | import java.math.BigDecimal; 6 | import java.math.BigInteger; 7 | 8 | import static org.hamcrest.Matchers.closeTo; 9 | import static org.hamcrest.Matchers.is; 10 | import static org.javafunk.funk.Doubles.*; 11 | import static org.junit.Assert.assertThat; 12 | 13 | public class DoublesTest { 14 | @Test 15 | public void returnsMapperFromStringToDouble() { 16 | assertThat(fromStringToDouble().map("12345678"), is(12345678D)); 17 | } 18 | 19 | @Test(expected = NullPointerException.class) 20 | public void throwsNullPointerExceptionWhenMapperFromStringToDoublePassedNull() { 21 | fromStringToDouble().map(null); 22 | } 23 | 24 | @Test 25 | public void returnsMapperFromIntegerToDouble() { 26 | assertThat(fromIntegerToDouble().map(1234), is(1234D)); 27 | } 28 | 29 | @Test(expected = NullPointerException.class) 30 | public void throwsNullPointerExceptionWhenMapperFromIntegerToDoublePassedNull() { 31 | fromIntegerToDouble().map(null); 32 | } 33 | 34 | @Test 35 | public void returnsMapperFromLongToDouble() { 36 | assertThat(fromLongToDouble().map(1234L), is(1234D)); 37 | } 38 | 39 | @Test(expected = NullPointerException.class) 40 | public void throwsNullPointerExceptionWhenMapperFromLongToDoublePassedNull() { 41 | fromLongToDouble().map(null); 42 | } 43 | 44 | @Test 45 | public void returnsMapperFromBigIntegerToDouble() { 46 | assertThat(fromBigIntegerToDouble().map(new BigInteger("1234")), is(1234D)); 47 | } 48 | 49 | @Test(expected = NullPointerException.class) 50 | public void throwsNullPointerExceptionWhenMapperFromBigIntegerToDoublePassedNull() { 51 | fromBigIntegerToDouble().map(null); 52 | } 53 | 54 | @Test 55 | public void returnsMapperFromFloatToDouble() { 56 | assertThat(fromFloatToDouble().map(1234.56F), is(closeTo(1234.56D, 0.001))); 57 | } 58 | 59 | @Test(expected = NullPointerException.class) 60 | public void throwsNullPointerExceptionWhenMapperFromFloatToDoublePassedNull() { 61 | fromFloatToDouble().map(null); 62 | } 63 | 64 | @Test 65 | public void returnsMapperFromBigDecimalToDouble() { 66 | assertThat(fromBigDecimalToDouble().map(new BigDecimal("1234.56")), is(closeTo(1234.56D, 0.001))); 67 | } 68 | 69 | @Test(expected = NullPointerException.class) 70 | public void throwsNullPointerExceptionWhenMapperFromBigDecimalToDoublePassedNull() { 71 | fromBigDecimalToDouble().map(null); 72 | } 73 | } -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/EagerlyNthRestTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import org.junit.Test; 12 | 13 | import java.util.Collection; 14 | 15 | import static org.javafunk.funk.Literals.collection; 16 | import static org.javafunk.funk.Literals.collectionWith; 17 | import static org.javafunk.matchbox.Matchers.hasOnlyItemsInOrder; 18 | import static org.junit.Assert.assertThat; 19 | 20 | public class EagerlyNthRestTest { 21 | @Test 22 | public void returnsRestOfTheCollectionAfterGivenPosition(){ 23 | Collection collection = collectionWith("a", "b", "c", "d"); 24 | Collection expectedRest = collectionWith("c", "d"); 25 | 26 | Collection rest = Eagerly.nthRest(collection, 2); 27 | 28 | assertThat(rest, hasOnlyItemsInOrder(expectedRest)); 29 | } 30 | 31 | @Test 32 | public void returnsCollectionForPositionZero(){ 33 | Collection collection = collectionWith("a", "b"); 34 | Collection expectedRest = collectionWith("a", "b"); 35 | 36 | Collection rest = Eagerly.nthRest(collection, 0); 37 | 38 | assertThat(rest, hasOnlyItemsInOrder(expectedRest)); 39 | } 40 | 41 | @Test 42 | public void returnsEmptyIterableForAnIterableWithOneElementAndStartIsHigherThanZero(){ 43 | Collection collection = collectionWith("a"); 44 | Collection expectedRest = collection(); 45 | 46 | Collection rest = Eagerly.nthRest(collection, 2); 47 | 48 | assertThat(rest, hasOnlyItemsInOrder(expectedRest)); 49 | } 50 | 51 | @Test 52 | public void returnsEmptyCollectionForAnEmptyCollection(){ 53 | Collection collection = collection(); 54 | Collection expectedRest = collection(); 55 | 56 | Collection rest = Eagerly.nthRest(collection, 3); 57 | 58 | assertThat(rest, hasOnlyItemsInOrder(expectedRest)); 59 | } 60 | 61 | @Test(expected = NullPointerException.class) 62 | public void throwsNullPointerExceptionIfNullIsPassedToRest() throws Exception { 63 | Eagerly.nthRest(null, 2); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/EagerlyRepeatTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import org.junit.Test; 12 | 13 | import java.util.Collection; 14 | 15 | import static org.hamcrest.MatcherAssert.assertThat; 16 | import static org.javafunk.funk.Literals.collectionWith; 17 | import static org.javafunk.funk.Literals.iterableWith; 18 | import static org.javafunk.matchbox.Matchers.hasOnlyItemsInOrder; 19 | 20 | public class EagerlyRepeatTest { 21 | @Test 22 | public void shouldRepeatTheElementsOfTheSpecifiedIterableInOrderTheSpecifiedNumberOfTimes() throws Exception { 23 | // Given 24 | Iterable input = iterableWith(1, 2); 25 | Collection expectedOutput = collectionWith(1, 2, 1, 2, 1, 2); 26 | 27 | // When 28 | Collection actualOutput = Eagerly.repeat(input, 3); 29 | 30 | // Then 31 | assertThat(actualOutput, hasOnlyItemsInOrder(expectedOutput)); 32 | } 33 | 34 | @Test(expected = NullPointerException.class) 35 | public void shouldThrowNullPointerExceptionIfIterablePassedToRepeatIsNull() throws Exception { 36 | // Given 37 | Iterable input = null; 38 | 39 | // When 40 | Eagerly.repeat(input, 3); 41 | 42 | // Then a NullPointerException is thrown. 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/EagerlyReverseTest.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Collection; 7 | import java.util.Iterator; 8 | 9 | import static org.hamcrest.MatcherAssert.assertThat; 10 | import static org.javafunk.funk.Literals.iterableOf; 11 | import static org.javafunk.funk.Literals.iterableWith; 12 | import static org.javafunk.matchbox.Matchers.hasOnlyItemsInOrder; 13 | import static org.junit.Assert.assertTrue; 14 | 15 | public class EagerlyReverseTest { 16 | 17 | @Test 18 | public void shouldReverseTheIterable() throws Exception { 19 | // Given 20 | Iterable iterable = iterableWith("a", "b", "c"); 21 | 22 | // When 23 | Collection actual = Eagerly.reverse(iterable); 24 | 25 | // Then 26 | assertThat(actual, hasOnlyItemsInOrder("c","b","a")); 27 | } 28 | 29 | @Test 30 | public void shouldKeepTheIterableReversed() throws Exception { 31 | // Given 32 | ArrayList listToModify = new ArrayList(); 33 | listToModify.add("a"); 34 | listToModify.add("b"); 35 | listToModify.add("c"); 36 | 37 | // When 38 | Collection reversedListBeforeRemoval = Eagerly.reverse(listToModify); 39 | 40 | Iterator iterator = listToModify.iterator(); 41 | iterator.next(); 42 | iterator.remove(); 43 | 44 | // Then 45 | assertThat(reversedListBeforeRemoval, hasOnlyItemsInOrder("c", "b", "a")); 46 | assertThat(listToModify, hasOnlyItemsInOrder("b", "c")); 47 | } 48 | 49 | @Test 50 | public void shouldReverseNulls() throws Exception { 51 | // Given 52 | Iterable iterable = iterableWith("a", "b", null, "d"); 53 | 54 | // When 55 | Collection actual = Eagerly.reverse(iterable); 56 | 57 | // Then 58 | assertThat(actual, hasOnlyItemsInOrder("d", null, "b", "a")); 59 | } 60 | 61 | @Test 62 | public void shouldReturnEmptyCollectionWhenReversingAnEmptyIterable() throws Exception { 63 | // Given 64 | Iterable iterable = iterableOf(String.class); 65 | 66 | // When 67 | Collection actual = Eagerly.reverse(iterable); 68 | 69 | // Then 70 | assertTrue(actual.isEmpty()); 71 | } 72 | 73 | @Test(expected = NullPointerException.class) 74 | public void shouldThrowNullPointerExceptionIfIterablePassedToReverseIsNull() throws Exception { 75 | // Given 76 | Iterable iterable = null; 77 | 78 | // When 79 | Eagerly.reverse(iterable); 80 | 81 | // Then a NullPointerException is thrown 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/FloatsTest.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.junit.Test; 4 | 5 | import java.math.BigDecimal; 6 | import java.math.BigInteger; 7 | 8 | import static org.hamcrest.Matchers.is; 9 | import static org.javafunk.funk.Floats.*; 10 | import static org.junit.Assert.assertThat; 11 | 12 | public class FloatsTest { 13 | @Test 14 | public void returnsMapperFromStringToFloat() { 15 | assertThat(fromStringToFloat().map("12345678.910"), is(12345678.910F)); 16 | } 17 | 18 | @Test(expected = NullPointerException.class) 19 | public void throwsNullPointerExceptionWhenMapperFromStringToFloatPassedNull() { 20 | fromStringToFloat().map(null); 21 | } 22 | 23 | @Test 24 | public void returnsMapperFromIntegerToFloat() { 25 | assertThat(fromIntegerToFloat().map(1234), is(1234F)); 26 | } 27 | 28 | @Test(expected = NullPointerException.class) 29 | public void throwsNullPointerExceptionWhenMapperFromIntegerToFloatPassedNull() { 30 | fromIntegerToFloat().map(null); 31 | } 32 | 33 | @Test 34 | public void returnsMapperFromLongToFloat() { 35 | assertThat(fromLongToFloat().map(1234L), is(1234F)); 36 | } 37 | 38 | @Test(expected = NullPointerException.class) 39 | public void throwsNullPointerExceptionWhenMapperFromLongToFloatPassedNull() { 40 | fromLongToFloat().map(null); 41 | } 42 | 43 | @Test 44 | public void returnsMapperFromBigIntegerToFloat() { 45 | assertThat(fromBigIntegerToFloat().map(new BigInteger("1234")), is(1234F)); 46 | } 47 | 48 | @Test(expected = NullPointerException.class) 49 | public void throwsNullPointerExceptionWhenMapperFromBigIntegerToFloatPassedNull() { 50 | fromBigIntegerToFloat().map(null); 51 | } 52 | 53 | @Test 54 | public void returnsMapperFromDoubleToFloat() { 55 | assertThat(fromDoubleToFloat().map(1234D), is(1234F)); 56 | } 57 | 58 | @Test(expected = NullPointerException.class) 59 | public void throwsNullPointerExceptionWhenMapperFromDoubleToFloatPassedNull() { 60 | fromDoubleToFloat().map(null); 61 | } 62 | 63 | @Test 64 | public void returnsMapperFromBigDecimalToFloat() { 65 | assertThat(fromBigDecimalToFloat().map(new BigDecimal("1234.56")), is(1234.56F)); 66 | } 67 | 68 | @Test(expected = NullPointerException.class) 69 | public void throwsNullPointerExceptionWhenMapperFromBigDecimalToFloatPassedNull() { 70 | fromBigDecimalToFloat().map(null); 71 | } 72 | } -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/GeneratorsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import org.javafunk.funk.behaviours.Generator; 12 | import org.javafunk.funk.generators.ConstantGenerator; 13 | import org.javafunk.funk.generators.FiniteGenerator; 14 | import org.junit.Test; 15 | 16 | import static org.hamcrest.MatcherAssert.assertThat; 17 | import static org.hamcrest.Matchers.is; 18 | import static org.javafunk.funk.Generators.constantGenerator; 19 | import static org.javafunk.funk.Generators.finiteGenerator; 20 | import static org.javafunk.funk.Literals.iterableWith; 21 | 22 | public class GeneratorsTest { 23 | @Test 24 | public void shouldConstructAConstantGeneratorWithTheSpecifiedValue() throws Exception { 25 | // Given 26 | Generator expectedGenerator = new ConstantGenerator(67463); 27 | 28 | // When 29 | Generator actualGenerator = constantGenerator(67463); 30 | 31 | // Then 32 | assertThat(actualGenerator, is(expectedGenerator)); 33 | } 34 | 35 | @Test 36 | public void shouldConstructAFiniteGeneratorUsingTheSpecifiedIterable() throws Exception { 37 | // Given 38 | Generator expectedGenerator = new FiniteGenerator(iterableWith(1, 2, 3, 4)); 39 | 40 | // When 41 | Generator actualGenerator = finiteGenerator(iterableWith(1, 2, 3, 4)); 42 | 43 | // Then 44 | assertThat(actualGenerator, is(expectedGenerator)); 45 | } 46 | 47 | @Test(expected = NullPointerException.class) 48 | public void shouldThrowANullPointerExceptionIfIterableUsedToConstructFiniteGeneratorIsNull() throws Exception { 49 | // When 50 | finiteGenerator(null); 51 | 52 | // Then a NullPointerException is thrown 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/IntegersTest.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.junit.Test; 4 | 5 | import java.math.BigDecimal; 6 | import java.math.BigInteger; 7 | 8 | import static org.hamcrest.Matchers.is; 9 | import static org.javafunk.funk.Integers.*; 10 | import static org.junit.Assert.assertThat; 11 | 12 | public class IntegersTest { 13 | @Test 14 | public void returnsMapperFromStringToInteger() { 15 | assertThat(fromStringToInteger().map("1234"), is(1234)); 16 | } 17 | 18 | @Test(expected = NullPointerException.class) 19 | public void throwsNullPointerExceptionWhenMapperFromStringToIntegerPassedNull() { 20 | fromStringToInteger().map(null); 21 | } 22 | 23 | @Test 24 | public void returnsMapperFromLongToInteger() { 25 | assertThat(fromLongToInteger().map(1234L), is(1234)); 26 | } 27 | 28 | @Test(expected = NullPointerException.class) 29 | public void throwsNullPointerExceptionWhenMapperFromLongToIntegerPassedNull() { 30 | fromLongToInteger().map(null); 31 | } 32 | 33 | @Test 34 | public void returnsMapperFromBigIntegerToInteger() { 35 | assertThat(fromBigIntegerToInteger().map(new BigInteger("1234")), is(1234)); 36 | } 37 | 38 | @Test(expected = NullPointerException.class) 39 | public void throwsNullPointerExceptionWhenMapperFromBigIntegerToIntegerPassedNull() { 40 | fromBigIntegerToInteger().map(null); 41 | } 42 | 43 | @Test 44 | public void returnsMapperFromFloatToInteger() { 45 | assertThat(fromFloatToInteger().map(1234.56F), is(1234)); 46 | } 47 | 48 | @Test(expected = NullPointerException.class) 49 | public void throwsNullPointerExceptionWhenMapperFromFloatToIntegerPassedNull() { 50 | fromFloatToInteger().map(null); 51 | } 52 | 53 | @Test 54 | public void returnsMapperFromDoubleToInteger() { 55 | assertThat(fromDoubleToInteger().map(1234.56D), is(1234)); 56 | } 57 | 58 | @Test(expected = NullPointerException.class) 59 | public void throwsNullPointerExceptionWhenMapperFromDoubleToIntegerPassedNull() { 60 | fromDoubleToInteger().map(null); 61 | } 62 | 63 | @Test 64 | public void returnsMapperFromBigDecimalToInteger() { 65 | assertThat(fromBigDecimalToInteger().map(new BigDecimal("1234")), is(1234)); 66 | } 67 | 68 | @Test(expected = NullPointerException.class) 69 | public void throwsNullPointerExceptionWhenMapperFromBigDecimalToIntegerPassedNull() { 70 | fromBigDecimalToInteger().map(null); 71 | } 72 | } -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/LazilyNthRestTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import org.junit.Test; 12 | 13 | import static org.javafunk.funk.Iterables.materialize; 14 | import static org.javafunk.funk.Literals.iterableWith; 15 | import static org.javafunk.matchbox.Matchers.hasOnlyItemsInOrder; 16 | import static org.junit.Assert.assertThat; 17 | 18 | public class LazilyNthRestTest { 19 | @Test 20 | public void returnsRestOfTheIterableAfterGivenPosition(){ 21 | Iterable iterable = iterableWith("a", "b", "c", "d"); 22 | Iterable expectedRest = iterableWith("c", "d"); 23 | 24 | Iterable rest = Lazily.nthRest(iterable, 2); 25 | 26 | assertThat(materialize(rest), hasOnlyItemsInOrder(expectedRest)); 27 | } 28 | 29 | @Test 30 | public void returnsIterableForPositionZero(){ 31 | Iterable iterable = iterableWith("a", "b"); 32 | Iterable expectedRest = iterableWith("a", "b"); 33 | 34 | Iterable rest = Lazily.nthRest(iterable, 0); 35 | 36 | assertThat(materialize(rest), hasOnlyItemsInOrder(expectedRest)); 37 | } 38 | 39 | @Test 40 | public void returnsEmptyIterableForAnIterableWithOneElementAndStartIsHigherThanZero(){ 41 | Iterable iterable = iterableWith("a"); 42 | Iterable expectedRest = Iterables.empty(); 43 | 44 | Iterable rest = Lazily.nthRest(iterable, 2); 45 | 46 | assertThat(materialize(rest), hasOnlyItemsInOrder(expectedRest)); 47 | } 48 | 49 | @Test 50 | public void returnsEmptyIterableForAnEmptyIterable(){ 51 | Iterable iterable = Iterables.empty(); 52 | Iterable expectedRest = Iterables.empty(); 53 | 54 | Iterable rest = Lazily.nthRest(iterable, 3); 55 | 56 | assertThat(materialize(rest), hasOnlyItemsInOrder(expectedRest)); 57 | } 58 | 59 | @Test(expected = NullPointerException.class) 60 | public void throwsNullPointerExceptionIfNullIsPassedToRest() throws Exception { 61 | Lazily.nthRest(null, 2); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/LazilyRepeatedlyTest.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.javafunk.funk.functors.functions.NullaryFunction; 4 | import org.junit.Test; 5 | 6 | import java.util.Iterator; 7 | import java.util.concurrent.atomic.AtomicInteger; 8 | 9 | import static org.hamcrest.Matchers.is; 10 | import static org.hamcrest.Matchers.not; 11 | import static org.hamcrest.Matchers.sameInstance; 12 | import static org.javafunk.funk.Literals.iterableWith; 13 | import static org.junit.Assert.assertThat; 14 | 15 | public class LazilyRepeatedlyTest { 16 | @Test 17 | public void shouldReturnALazyIterableWhichRepeatedlyCallsTheSuppliedFunctionForElements() { 18 | // Given 19 | final AtomicInteger callCount = new AtomicInteger(); 20 | final NullaryFunction function = new NullaryFunction() { 21 | @Override public Integer call() { 22 | return callCount.getAndIncrement(); 23 | } 24 | }; 25 | 26 | // When 27 | Iterable repeatedlyIterable = Lazily.repeatedly(function); 28 | Iterator repeatedlyIterator = repeatedlyIterable.iterator(); 29 | 30 | // Then 31 | for(int i = 0; i < 20; i++) { 32 | assertThat(repeatedlyIterator.next(), is(i)); 33 | } 34 | } 35 | 36 | @Test 37 | public void shouldAllowIteratorToBeCalledMultipleTimesOnRepeatedlyIterableReturningDifferentButCoupledIterators() throws Exception { 38 | // Given 39 | final AtomicInteger callCount = new AtomicInteger(); 40 | final NullaryFunction function = new NullaryFunction() { 41 | @Override public Integer call() { 42 | return callCount.getAndIncrement(); 43 | } 44 | }; 45 | 46 | // When 47 | Iterable iterable = Lazily.repeatedly(function); 48 | 49 | Iterator iterator1 = iterable.iterator(); 50 | Iterator iterator2 = iterable.iterator(); 51 | 52 | // Then 53 | assertThat(iterator1.next(), is(0)); 54 | assertThat(iterator1.next(), is(1)); 55 | assertThat(iterator2.next(), is(2)); 56 | assertThat(iterator1.next(), is(3)); 57 | assertThat(iterator2.next(), is(4)); 58 | assertThat(iterator1.next(), is(5)); 59 | assertThat(iterator1, is(not(sameInstance(iterator2)))); 60 | } 61 | 62 | @Test(expected = NullPointerException.class) 63 | public void shouldThrowNullPointerExceptionIfSuppliedFunctionIsNull() { 64 | // Given 65 | NullaryFunction function = null; 66 | 67 | // When 68 | Lazily.repeatedly(function); 69 | 70 | // Then a NullPointerException is thrown 71 | } 72 | } -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/LazilyRestTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk; 10 | 11 | import org.junit.Test; 12 | 13 | import static org.javafunk.funk.Iterables.materialize; 14 | import static org.javafunk.funk.Literals.iterableWith; 15 | import static org.javafunk.matchbox.Matchers.hasOnlyItemsInOrder; 16 | import static org.junit.Assert.assertThat; 17 | 18 | public class LazilyRestTest { 19 | @Test 20 | public void shouldReturnTheRestOfTheIterable(){ 21 | // Given 22 | Iterable iterable = iterableWith("a", "b", "c", "d"); 23 | Iterable expectedRest = iterableWith("b", "c", "d"); 24 | 25 | // When 26 | Iterable rest = Lazily.rest(iterable); 27 | 28 | // Then 29 | assertThat(materialize(rest), hasOnlyItemsInOrder(expectedRest)); 30 | } 31 | 32 | @Test 33 | public void shouldReturnEmptyIterableForAnIterableWithOneElement(){ 34 | // Given 35 | Iterable iterable = iterableWith("a"); 36 | Iterable expectedRest = Iterables.empty(); 37 | 38 | // When 39 | Iterable rest = Lazily.rest(iterable); 40 | 41 | // Then 42 | assertThat(materialize(rest), hasOnlyItemsInOrder(expectedRest)); 43 | } 44 | 45 | @Test 46 | public void shouldReturnEmptyIterableForAnEmptyIterable(){ 47 | // Given 48 | Iterable iterable = Iterables.empty(); 49 | Iterable expectedRest = Iterables.empty(); 50 | 51 | // When 52 | Iterable rest = Lazily.rest(iterable); 53 | 54 | // Then 55 | assertThat(materialize(rest), hasOnlyItemsInOrder(expectedRest)); 56 | } 57 | 58 | @Test(expected = NullPointerException.class) 59 | public void shouldThrowNullPointerExceptionIfAnEmptyIterableIsPassedToRest() throws Exception { 60 | // Given 61 | Iterable iterable = null; 62 | 63 | // When 64 | Lazily.rest(iterable); 65 | 66 | // Then a NullPointerException is thrown 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/MappersTest.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.Iterator; 6 | import java.util.List; 7 | 8 | import static org.hamcrest.Matchers.nullValue; 9 | import static org.hamcrest.core.Is.is; 10 | import static org.javafunk.funk.Literals.listFrom; 11 | import static org.javafunk.funk.Literals.listWith; 12 | import static org.junit.Assert.assertThat; 13 | 14 | public class MappersTest { 15 | @Test 16 | public void identityShouldReturnItself() { 17 | // Given 18 | List eastwood = listWith("the", "good", "the", "bad", "and", "the", "ugly"); 19 | 20 | // When 21 | List mappedEastwood = listFrom(Eagerly.map(eastwood, Mappers.identity())); 22 | 23 | // Then 24 | assertThat(mappedEastwood, is(eastwood)); 25 | } 26 | 27 | @Test 28 | public void identityShouldReturnItselfEvenWhenNull() { 29 | // Given 30 | List eastwood = listWith(null, "good", null, "bad", "and", null, "ugly"); 31 | 32 | // When 33 | List mappedEastwood = listFrom(Eagerly.map(eastwood, Mappers.identity())); 34 | 35 | // Then 36 | assertThat(mappedEastwood, is(eastwood)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/generators/ConstantGeneratorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.generators; 10 | 11 | import org.javafunk.matchbox.SelfDescribingPredicate; 12 | import org.junit.Test; 13 | 14 | import static org.hamcrest.MatcherAssert.assertThat; 15 | import static org.hamcrest.Matchers.is; 16 | import static org.javafunk.funk.Generators.toGeneratable; 17 | import static org.javafunk.funk.Lazily.take; 18 | import static org.javafunk.matchbox.Matchers.hasAllElementsSatisfying; 19 | 20 | public class ConstantGeneratorTest { 21 | @Test 22 | public void shouldAlwaysReturnTheSuppliedValueForNext() throws Exception { 23 | // Given 24 | final String expectedValue = "Hello"; 25 | final ConstantGenerator generator = new ConstantGenerator(expectedValue); 26 | 27 | // When 28 | Iterable values = take(toGeneratable(generator), 1000); 29 | 30 | // Then 31 | assertThat(values, hasAllElementsSatisfying(new SelfDescribingPredicate() { 32 | @Override public boolean evaluate(String actualValue) { 33 | return actualValue.equals(expectedValue); 34 | } 35 | 36 | @Override public String describe() { 37 | return "value equal to: " + expectedValue; 38 | } 39 | })); 40 | } 41 | 42 | @Test 43 | public void shouldBeEqualIfHasTheSameValue() throws Exception { 44 | // Given 45 | ConstantGenerator firstGenerator = new ConstantGenerator(1439); 46 | ConstantGenerator secondGenerator = new ConstantGenerator(1439); 47 | 48 | // When 49 | boolean equal = firstGenerator.equals(secondGenerator); 50 | 51 | // Then 52 | assertThat(equal, is(true)); 53 | } 54 | 55 | @Test 56 | public void shouldNotBeEqualIfHasDifferentValue() throws Exception { 57 | // Given 58 | ConstantGenerator firstGenerator = new ConstantGenerator(1439); 59 | ConstantGenerator secondGenerator = new ConstantGenerator(22984); 60 | 61 | // When 62 | boolean equal = firstGenerator.equals(secondGenerator); 63 | 64 | // Then 65 | assertThat(equal, is(false)); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/iterators/ZippedIteratorTest.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.iterators; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.Iterator; 6 | 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | import static org.hamcrest.Matchers.containsString; 9 | import static org.javafunk.funk.Literals.iterableWith; 10 | import static org.javafunk.funk.Literals.iteratorWith; 11 | import static org.mockito.Mockito.RETURNS_MOCKS; 12 | import static org.mockito.Mockito.mock; 13 | import static org.mockito.Mockito.when; 14 | 15 | public class ZippedIteratorTest { 16 | @Test 17 | @SuppressWarnings("unchecked") 18 | public void shouldIncludeIteratorsInToStringRepresentation() throws Exception { 19 | // Given 20 | Iterable> iterators = (Iterable>) mock(Iterable.class, RETURNS_MOCKS); 21 | ZippedIterator iterator = new ZippedIterator(iterators); 22 | 23 | when(iterators.toString()).thenReturn("the-iterators"); 24 | 25 | // When 26 | String toString = iterator.toString(); 27 | 28 | // Then 29 | assertThat(toString, containsString("the-iterators")); 30 | } 31 | 32 | @Test(expected = NullPointerException.class) 33 | public void shouldThrowANullPointerExceptionIfIterablePassedAtConstructionTimeIsNull() throws Exception { 34 | // Given 35 | Iterable> iterators = null; 36 | 37 | // When 38 | new ZippedIterator(iterators); 39 | 40 | // Then a NullPointerException is thrown 41 | } 42 | 43 | @Test(expected = NullPointerException.class) 44 | public void shouldThrowANullPointerExceptionIfAnyOfTheIteratorsInTheSuppliedIterableAreNull() throws Exception { 45 | // Given 46 | Iterator first = iteratorWith(1, 2, 3); 47 | Iterator second = null; 48 | Iterable> iterators = iterableWith(first, second); 49 | 50 | // When 51 | new ZippedIterator(iterators); 52 | 53 | // Then a NullPointerException is thrown 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/predicates/FalsePredicateTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.predicates; 10 | 11 | import org.javafunk.funk.functors.Predicate; 12 | import org.junit.Test; 13 | 14 | import static org.hamcrest.MatcherAssert.assertThat; 15 | import static org.hamcrest.Matchers.is; 16 | 17 | public class FalsePredicateTest { 18 | @Test 19 | public void shouldReturnFalseForNull() { 20 | // Given 21 | Predicate falsePredicate = new FalsePredicate(); 22 | 23 | // When 24 | Boolean result = falsePredicate.evaluate(null); 25 | 26 | // Then 27 | assertThat(result, is(false)); 28 | } 29 | 30 | @Test 31 | public void shouldReturnFalseForObject() { 32 | // Given 33 | Predicate falsePredicate = new FalsePredicate(); 34 | 35 | // When 36 | Boolean result = falsePredicate.evaluate(new Object()); 37 | 38 | // Then 39 | assertThat(result, is(false)); 40 | } 41 | 42 | @Test 43 | public void shouldBeEqualIfOtherPredicateIsAlsoAFalsePredicate() throws Exception { 44 | // Given 45 | Predicate firstPredicate = new FalsePredicate(); 46 | Predicate secondPredicate = new FalsePredicate(); 47 | 48 | // When 49 | boolean equal = firstPredicate.equals(secondPredicate); 50 | 51 | // Then 52 | assertThat(equal, is(true)); 53 | } 54 | 55 | @Test 56 | public void shouldBeEqualIfOtherPredicateIsAFalsePredicateButTypesAreDifferent() throws Exception { 57 | // Given 58 | Predicate firstPredicate = new FalsePredicate(); 59 | Predicate secondPredicate = new FalsePredicate(); 60 | 61 | // When 62 | boolean equal = firstPredicate.equals(secondPredicate); 63 | 64 | // Then 65 | assertThat(equal, is(true)); 66 | } 67 | 68 | @Test 69 | public void shouldNotBeEqualIfOtherPredicateIsNotAFalsePredicate() throws Exception { 70 | // Given 71 | Predicate firstPredicate = new FalsePredicate(); 72 | Predicate secondPredicate = new TruePredicate(); 73 | 74 | // When 75 | boolean equal = firstPredicate.equals(secondPredicate); 76 | 77 | // Then 78 | assertThat(equal, is(false)); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/predicates/TruePredicateTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.predicates; 10 | 11 | import org.javafunk.funk.functors.Predicate; 12 | import org.junit.Test; 13 | 14 | import static org.hamcrest.MatcherAssert.assertThat; 15 | import static org.hamcrest.Matchers.is; 16 | 17 | public class TruePredicateTest { 18 | 19 | @Test 20 | public void shouldReturnTrueForNull() { 21 | // Given 22 | Predicate truePredicate = new TruePredicate(); 23 | 24 | // When 25 | Boolean result = truePredicate.evaluate(null); 26 | 27 | // Then 28 | assertThat(result, is(true)); 29 | } 30 | 31 | @Test 32 | public void shouldReturnTrueForObject() { 33 | // Given 34 | Predicate truePredicate = new TruePredicate(); 35 | 36 | // When 37 | Boolean result = truePredicate.evaluate(new Object()); 38 | 39 | // Then 40 | assertThat(result, is(true)); 41 | } 42 | 43 | @Test 44 | public void shouldBeEqualIfOtherPredicateIsAlsoATruePredicate() throws Exception { 45 | // Given 46 | Predicate firstPredicate = new TruePredicate(); 47 | Predicate secondPredicate = new TruePredicate(); 48 | 49 | // When 50 | boolean equal = firstPredicate.equals(secondPredicate); 51 | 52 | // Then 53 | assertThat(equal, is(true)); 54 | } 55 | 56 | @Test 57 | public void shouldBeEqualIfOtherPredicateIsATruePredicateButTypesAreDifferent() throws Exception { 58 | // Given 59 | Predicate firstPredicate = new TruePredicate(); 60 | Predicate secondPredicate = new TruePredicate(); 61 | 62 | // When 63 | boolean equal = firstPredicate.equals(secondPredicate); 64 | 65 | // Then 66 | assertThat(equal, is(true)); 67 | } 68 | 69 | @Test 70 | public void shouldNotBeEqualIfOtherPredicateIsNotATruePredicate() throws Exception { 71 | // Given 72 | Predicate firstPredicate = new TruePredicate(); 73 | Predicate secondPredicate = new FalsePredicate(); 74 | 75 | // When 76 | boolean equal = firstPredicate.equals(secondPredicate); 77 | 78 | // Then 79 | assertThat(equal, is(false)); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/Age.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.testclasses; 10 | 11 | import org.apache.commons.lang.builder.EqualsBuilder; 12 | import org.apache.commons.lang.builder.HashCodeBuilder; 13 | import org.apache.commons.lang.builder.ToStringBuilder; 14 | import org.apache.commons.lang.builder.ToStringStyle; 15 | 16 | public class Age { 17 | private Integer value; 18 | 19 | public static Age age(Integer value) { 20 | return new Age(value); 21 | } 22 | 23 | public Age(Integer value) { 24 | this.value = value; 25 | } 26 | 27 | public Integer getValue() { 28 | return value; 29 | } 30 | 31 | @Override 32 | public boolean equals(Object that) { 33 | return EqualsBuilder.reflectionEquals(this, that); 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return HashCodeBuilder.reflectionHashCode(this); 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/Animal.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.testclasses; 10 | 11 | import org.apache.commons.lang.builder.EqualsBuilder; 12 | import org.apache.commons.lang.builder.HashCodeBuilder; 13 | import org.apache.commons.lang.builder.ToStringBuilder; 14 | import org.apache.commons.lang.builder.ToStringStyle; 15 | 16 | public class Animal { 17 | private Colour colour; 18 | private Name name; 19 | 20 | public static Animal animal(Colour colour, Name name) { 21 | return new Animal(colour, name); 22 | } 23 | 24 | public Animal(Colour colour, Name name) { 25 | this.colour = colour; 26 | this.name = name; 27 | } 28 | 29 | public Colour getColour() { 30 | return colour; 31 | } 32 | 33 | public Name getName() { 34 | return name; 35 | } 36 | 37 | @Override 38 | public boolean equals(Object that) { 39 | return EqualsBuilder.reflectionEquals(this, that); 40 | } 41 | 42 | @Override 43 | public int hashCode() { 44 | return HashCodeBuilder.reflectionHashCode(this); 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/Cat.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.testclasses; 10 | 11 | import org.apache.commons.lang.builder.EqualsBuilder; 12 | import org.apache.commons.lang.builder.HashCodeBuilder; 13 | 14 | public class Cat extends Animal { 15 | public static Cat cat(Colour colour, Name name) { 16 | return new Cat(colour, name); 17 | } 18 | 19 | public Cat(Colour colour, Name name) { 20 | super(colour, name); 21 | } 22 | 23 | @Override 24 | public boolean equals(Object that) { 25 | return EqualsBuilder.reflectionEquals(this, that); 26 | } 27 | 28 | @Override 29 | public int hashCode() { 30 | return HashCodeBuilder.reflectionHashCode(this); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/Colour.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.testclasses; 10 | 11 | import org.apache.commons.lang.builder.EqualsBuilder; 12 | import org.apache.commons.lang.builder.HashCodeBuilder; 13 | import org.apache.commons.lang.builder.ToStringBuilder; 14 | import org.apache.commons.lang.builder.ToStringStyle; 15 | 16 | public class Colour { 17 | private String value; 18 | 19 | public static Colour colour(String value) { 20 | return new Colour(value); 21 | } 22 | 23 | public Colour(String value) { 24 | this.value = value; 25 | } 26 | 27 | public String getValue() { 28 | return value; 29 | } 30 | 31 | @Override 32 | public boolean equals(Object that) { 33 | return EqualsBuilder.reflectionEquals(this, that); 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return HashCodeBuilder.reflectionHashCode(this); 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/Dog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.testclasses; 10 | 11 | import org.apache.commons.lang.builder.EqualsBuilder; 12 | import org.apache.commons.lang.builder.HashCodeBuilder; 13 | 14 | public class Dog extends Animal { 15 | public static Dog dog(Colour colour, Name name) { 16 | return new Dog(colour, name); 17 | } 18 | 19 | public Dog(Colour colour, Name name) { 20 | super(colour, name); 21 | } 22 | 23 | @Override 24 | public boolean equals(Object that) { 25 | return EqualsBuilder.reflectionEquals(this, that); 26 | } 27 | 28 | @Override 29 | public int hashCode() { 30 | return HashCodeBuilder.reflectionHashCode(this); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/Location.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.testclasses; 10 | 11 | import org.apache.commons.lang.builder.EqualsBuilder; 12 | import org.apache.commons.lang.builder.HashCodeBuilder; 13 | import org.apache.commons.lang.builder.ToStringBuilder; 14 | import org.apache.commons.lang.builder.ToStringStyle; 15 | 16 | public class Location { 17 | private String value; 18 | 19 | public static Location location(String value) { 20 | return new Location(value); 21 | } 22 | 23 | public Location(String value) { 24 | this.value = value; 25 | } 26 | 27 | public String getValue() { 28 | return value; 29 | } 30 | 31 | @Override 32 | public boolean equals(Object that) { 33 | return EqualsBuilder.reflectionEquals(this, that); 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return HashCodeBuilder.reflectionHashCode(this); 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/Matchers.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.testclasses; 2 | 3 | import org.hamcrest.Description; 4 | import org.hamcrest.Matcher; 5 | import org.hamcrest.TypeSafeDiagnosingMatcher; 6 | import org.javafunk.matchbox.SelfDescribingPredicate; 7 | 8 | import static org.hamcrest.Matchers.instanceOf; 9 | import static org.hamcrest.Matchers.is; 10 | 11 | public class Matchers { 12 | public static Matcher> hasAllElementsEqualTo(final Boolean booleanValue) { 13 | return org.javafunk.matchbox.Matchers.hasAllElementsSatisfying(new SelfDescribingPredicate() { 14 | @Override public String describe() { 15 | return "equal to " + booleanValue.toString(); 16 | } 17 | 18 | @Override public boolean evaluate(Boolean item) { 19 | return item.equals(booleanValue); 20 | } 21 | }); 22 | } 23 | 24 | public static Matcher equalToIncludingConcreteType(final T expected) { 25 | return new TypeSafeDiagnosingMatcher() { 26 | @Override protected boolean matchesSafely(T item, Description mismatchDescription) { 27 | mismatchDescription.appendText("got an object of type: ") 28 | .appendValue(item.getClass().getSimpleName()) 29 | .appendText(" : ") 30 | .appendValue(item); 31 | return instanceOf(expected.getClass()).matches(item) && is(expected).matches(item); 32 | } 33 | 34 | @Override public void describeTo(Description description) { 35 | description.appendText("an object of type: ") 36 | .appendValue(expected.getClass().getSimpleName()) 37 | .appendText(" equal to: ") 38 | .appendValue(expected); 39 | } 40 | }; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/Name.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | package org.javafunk.funk.testclasses; 10 | 11 | import org.apache.commons.lang.builder.EqualsBuilder; 12 | import org.apache.commons.lang.builder.HashCodeBuilder; 13 | import org.apache.commons.lang.builder.ToStringBuilder; 14 | import org.apache.commons.lang.builder.ToStringStyle; 15 | 16 | public class Name { 17 | private String value; 18 | 19 | public static Name name(String value) { 20 | return new Name(value); 21 | } 22 | 23 | public Name(String value) { 24 | this.value = value; 25 | } 26 | 27 | public String getValue() { 28 | return value; 29 | } 30 | 31 | @Override 32 | public boolean equals(Object that) { 33 | return EqualsBuilder.reflectionEquals(this, that); 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return HashCodeBuilder.reflectionHashCode(this); 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/NoArgsConstructorIterator.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.testclasses; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | 6 | import java.util.Iterator; 7 | 8 | public class NoArgsConstructorIterator implements Iterator { 9 | private final String name = "Needed for equality."; 10 | 11 | @Override public boolean hasNext() { 12 | throw new UnsupportedOperationException(); 13 | } 14 | 15 | @Override public T next() { 16 | throw new UnsupportedOperationException(); 17 | } 18 | 19 | @Override public void remove() { 20 | throw new UnsupportedOperationException(); 21 | } 22 | 23 | public boolean equals(Object other) { 24 | return EqualsBuilder.reflectionEquals(this, other); 25 | } 26 | 27 | public int hashCode() { 28 | return HashCodeBuilder.reflectionHashCode(this); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/NoArgsConstructorMultiset.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.testclasses; 2 | 3 | public class NoArgsConstructorMultiset extends StubMultiset { 4 | public NoArgsConstructorMultiset() { } 5 | } 6 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/NoNoArgsConstructorIterator.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.testclasses; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | 6 | import java.util.Iterator; 7 | 8 | public class NoNoArgsConstructorIterator implements Iterator { 9 | private final String name = "Needed for equality."; 10 | 11 | private NoNoArgsConstructorIterator() {} 12 | 13 | @Override public boolean hasNext() { 14 | throw new UnsupportedOperationException(); 15 | } 16 | 17 | @Override public T next() { 18 | throw new UnsupportedOperationException(); 19 | } 20 | 21 | @Override public void remove() { 22 | throw new UnsupportedOperationException(); 23 | } 24 | 25 | public boolean equals(Object other) { 26 | return EqualsBuilder.reflectionEquals(this, other); 27 | } 28 | 29 | public int hashCode() { 30 | return HashCodeBuilder.reflectionHashCode(this); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/NoNoArgsConstructorList.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.testclasses; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | 6 | import java.util.ArrayList; 7 | 8 | public class NoNoArgsConstructorList extends ArrayList { 9 | private final String name = "Needed for equality."; 10 | 11 | public NoNoArgsConstructorList(Throwable argument) { 12 | throw new UnsupportedOperationException("should never throw", argument); 13 | } 14 | 15 | public boolean equals(Object other) { 16 | return EqualsBuilder.reflectionEquals(this, other); 17 | } 18 | 19 | public int hashCode() { 20 | return HashCodeBuilder.reflectionHashCode(this); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/NoNoArgsConstructorMap.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.testclasses; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | 6 | import java.util.HashMap; 7 | 8 | public class NoNoArgsConstructorMap extends HashMap { 9 | private final String name = "Needed for equality."; 10 | 11 | public NoNoArgsConstructorMap(Throwable argument) { 12 | throw new UnsupportedOperationException("should never throw", argument); 13 | } 14 | 15 | public boolean equals(Object other) { 16 | return EqualsBuilder.reflectionEquals(this, other); 17 | } 18 | 19 | public int hashCode() { 20 | return HashCodeBuilder.reflectionHashCode(this); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/NoNoArgsConstructorSet.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.testclasses; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | 6 | import java.util.HashSet; 7 | 8 | public class NoNoArgsConstructorSet extends HashSet { 9 | private final String name = "Needed for equality."; 10 | 11 | public NoNoArgsConstructorSet(Throwable argument) { 12 | throw new UnsupportedOperationException("should never throw", argument); 13 | } 14 | 15 | public boolean equals(Object other) { 16 | return EqualsBuilder.reflectionEquals(this, other); 17 | } 18 | 19 | public int hashCode() { 20 | return HashCodeBuilder.reflectionHashCode(this); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/PrivateAccessConstructorMultiset.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.testclasses; 2 | 3 | public class PrivateAccessConstructorMultiset extends StubMultiset { 4 | private PrivateAccessConstructorMultiset() { } 5 | } 6 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/SomeArgsConstructorMultiset.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.testclasses; 2 | 3 | public class SomeArgsConstructorMultiset extends StubMultiset { 4 | public SomeArgsConstructorMultiset(Object first, Object second) { } 5 | } 6 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/StubMultiset.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.testclasses; 2 | 3 | import com.google.common.collect.Multiset; 4 | import org.apache.commons.lang.builder.EqualsBuilder; 5 | import org.apache.commons.lang.builder.HashCodeBuilder; 6 | 7 | import java.util.Collection; 8 | import java.util.Iterator; 9 | import java.util.Set; 10 | 11 | public class StubMultiset implements Multiset { 12 | private final String name = "Needed for equality."; 13 | 14 | @Override public int count(Object element) { return 0; } 15 | @Override public int add(E element, int occurrences) { return 0; } 16 | @Override public int remove(Object element, int occurrences) { return 0; } 17 | @Override public int setCount(E element, int count) { return 0; } 18 | @Override public boolean setCount(E element, int oldCount, int newCount) { return false; } 19 | @Override public Set elementSet() { return null; } 20 | @Override public Set> entrySet() { return null; } 21 | @Override public Iterator iterator() { return null; } 22 | @Override public Object[] toArray() { return new Object[0]; } 23 | @Override public T[] toArray(T[] ts) { return null; } 24 | @Override public int size() { return 0; } 25 | @Override public boolean isEmpty() { return false; } 26 | @Override public boolean contains(Object element) { return false; } 27 | @Override public boolean containsAll(Collection elements) { return false; } 28 | @Override public boolean addAll(Collection es) { return false; } 29 | @Override public boolean add(E element) { return false; } 30 | @Override public boolean remove(Object element) { return false; } 31 | @Override public boolean removeAll(Collection c) { return false; } 32 | @Override public boolean retainAll(Collection c) { return false; } 33 | @Override public void clear() { } 34 | 35 | public boolean equals(Object other) { 36 | return EqualsBuilder.reflectionEquals(this, other); 37 | } 38 | 39 | public int hashCode() { 40 | return HashCodeBuilder.reflectionHashCode(this); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/TrackingCallable.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.testclasses; 2 | 3 | import java.util.concurrent.Callable; 4 | 5 | public class TrackingCallable implements Callable { 6 | private final R callResult; 7 | 8 | private Boolean wasCalled = false; 9 | 10 | public static TrackingCallable trackingCallable(R result) { 11 | return new TrackingCallable(result); 12 | } 13 | 14 | public TrackingCallable(R callResult) { 15 | this.callResult = callResult; 16 | } 17 | 18 | @Override public R call() throws Exception { 19 | wasCalled = true; 20 | return callResult; 21 | } 22 | 23 | public boolean wasCalled() { 24 | return wasCalled; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /funk-core/src/test/java/org/javafunk/funk/testclasses/TrackingNullaryFunction.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.testclasses; 2 | 3 | import org.javafunk.funk.functors.functions.NullaryFunction; 4 | 5 | public class TrackingNullaryFunction implements NullaryFunction { 6 | private final R result; 7 | 8 | private Boolean wasCalled = false; 9 | 10 | public static TrackingNullaryFunction trackingNullaryFunction(R result) { 11 | return new TrackingNullaryFunction(result); 12 | } 13 | 14 | public TrackingNullaryFunction(R result) { 15 | this.result = result; 16 | } 17 | 18 | @Override public R call() { 19 | this.wasCalled = true; 20 | return result; 21 | } 22 | 23 | public Boolean wasCalled() { 24 | return wasCalled; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /funk-jackson/README.md: -------------------------------------------------------------------------------- 1 | Funk Jackson 2 | ============ 3 | 4 | Simplifying functional programming in Java. 5 | 6 | Description 7 | ----------- 8 | 9 | Project to build Jackson module to support JSON serialization and deserialization of Funk types. 10 | 11 | This project is basically a port of the [Guava Jackson module](https://github.com/FasterXML/jackson-datatype-guava) for Funk. 12 | 13 | Registering module 14 | ------------------ 15 | 16 | Like all standard Jackson modules (libraries that implement Module interface), registration is done as follows: 17 | 18 | ```java 19 | ObjectMapper mapper = new ObjectMapper(); 20 | mapper.registerModule(new FunkModule()); 21 | ``` 22 | 23 | after which functionality is available for all normal Jackson operations. 24 | -------------------------------------------------------------------------------- /funk-jackson/build.gradle: -------------------------------------------------------------------------------- 1 | import org.gradle.api.artifacts.maven.MavenDeployment 2 | 3 | dependencies { 4 | compile project(':funk-core') 5 | 6 | compile 'com.fasterxml.jackson.core:jackson-databind:2.4.4' 7 | 8 | testCompile project(':funk-matchers') 9 | testCompile "org.javafunk.matchbox:matchbox:0.1.4" 10 | } 11 | 12 | uploadArchives { 13 | repositories { 14 | flatDir { 15 | dirs "${rootDir}/repos" 16 | } 17 | 18 | mavenDeployer { 19 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 20 | 21 | repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { 22 | authentication( 23 | userName: project.property("oss-releases.username"), 24 | password: project.property("oss-releases.password")) 25 | } 26 | 27 | snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { 28 | authentication( 29 | userName: project.property("oss-releases.username"), 30 | password: project.property("oss-releases.password")) 31 | } 32 | 33 | pom.project { 34 | name 'Funk Jackson' 35 | description 'Functional utilities for Java: jackson mappers for types in core APIs' 36 | packaging 'jar' 37 | url 'https://github.com/javafunk/funk' 38 | 39 | scm { 40 | url 'scm:git:https://github.com/javafunk/funk.git' 41 | connection 'scm:git:https://github.com/javafunk/funk.git' 42 | developerConnection 'http://github.com/javafunk/funk' 43 | } 44 | 45 | licenses { 46 | license { 47 | name 'BSD License' 48 | url 'http://www.opensource.org/licenses/bsd-license.php' 49 | } 50 | } 51 | 52 | developers { 53 | developer { 54 | id 'toby' 55 | name 'Toby Clemson' 56 | email 'tobyclemson@gmail.com' 57 | } 58 | developer { 59 | id 'andrew' 60 | name 'Andrew Kiellor' 61 | email 'akiellor@gmail.com' 62 | } 63 | developer { 64 | id 'simon' 65 | name 'Simon Brunning' 66 | email 'simon@brunningonline.net' 67 | } 68 | } 69 | } 70 | 71 | pom.whenConfigured { pom -> 72 | rootProject.addAsParentFor(pom) 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /funk-jackson/src/main/java/org/javafunk/funk/jackson/FunkDeserializers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | 10 | package org.javafunk.funk.jackson; 11 | 12 | import com.fasterxml.jackson.databind.BeanDescription; 13 | import com.fasterxml.jackson.databind.DeserializationConfig; 14 | import com.fasterxml.jackson.databind.JavaType; 15 | import com.fasterxml.jackson.databind.JsonDeserializer; 16 | import com.fasterxml.jackson.databind.JsonMappingException; 17 | import com.fasterxml.jackson.databind.deser.Deserializers; 18 | import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; 19 | import org.javafunk.funk.jackson.monad.OptionDeserializer; 20 | import org.javafunk.funk.monads.Option; 21 | 22 | import static org.javafunk.funk.monads.Option.option; 23 | 24 | public class FunkDeserializers extends Deserializers.Base { 25 | @Override 26 | public JsonDeserializer findBeanDeserializer(JavaType type, DeserializationConfig config, BeanDescription beanDesc) throws JsonMappingException { 27 | Class raw = type.getRawClass(); 28 | if (Option.class.isAssignableFrom(raw)) { 29 | TypeDeserializer typeHandler = type.getTypeHandler(); 30 | JsonDeserializer valueHandler = type.getValueHandler(); 31 | return new OptionDeserializer(type, option(typeHandler), Option.>option(valueHandler)); 32 | } 33 | return super.findBeanDeserializer(type, config, beanDesc); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /funk-jackson/src/main/java/org/javafunk/funk/jackson/FunkModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | 10 | package org.javafunk.funk.jackson; 11 | 12 | import com.fasterxml.jackson.core.Version; 13 | import com.fasterxml.jackson.core.json.PackageVersion; 14 | import com.fasterxml.jackson.databind.Module; 15 | 16 | public class FunkModule extends Module { 17 | @Override 18 | public String getModuleName() { 19 | return "FunkModule"; 20 | } 21 | 22 | @Override 23 | public Version version() { 24 | return PackageVersion.VERSION; 25 | } 26 | 27 | @Override 28 | public void setupModule(SetupContext context) { 29 | context.addDeserializers(new FunkDeserializers()); 30 | context.addSerializers(new FunkSerializers()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /funk-jackson/src/main/java/org/javafunk/funk/jackson/FunkSerializers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | 10 | package org.javafunk.funk.jackson; 11 | 12 | import com.fasterxml.jackson.databind.BeanDescription; 13 | import com.fasterxml.jackson.databind.JavaType; 14 | import com.fasterxml.jackson.databind.JsonSerializer; 15 | import com.fasterxml.jackson.databind.SerializationConfig; 16 | import com.fasterxml.jackson.databind.jsontype.TypeSerializer; 17 | import com.fasterxml.jackson.databind.ser.Serializers; 18 | import com.fasterxml.jackson.databind.type.CollectionLikeType; 19 | import org.javafunk.funk.jackson.monad.OptionSerializer; 20 | import org.javafunk.funk.monads.Option; 21 | 22 | import static com.google.common.base.Preconditions.checkNotNull; 23 | 24 | public class FunkSerializers extends Serializers.Base { 25 | @Override 26 | public JsonSerializer findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) { 27 | checkNotNull(type); 28 | Class raw = type.getRawClass(); 29 | if (Option.class.isAssignableFrom(raw)) { 30 | return new OptionSerializer(type); 31 | } 32 | return super.findSerializer(config, type, beanDesc); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /funk-jackson/src/main/java/org/javafunk/funk/jackson/monad/OptionSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | 10 | package org.javafunk.funk.jackson.monad; 11 | 12 | import com.fasterxml.jackson.core.JsonGenerator; 13 | import com.fasterxml.jackson.databind.JavaType; 14 | import com.fasterxml.jackson.databind.JsonMappingException; 15 | import com.fasterxml.jackson.databind.SerializerProvider; 16 | import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper; 17 | import com.fasterxml.jackson.databind.jsontype.TypeSerializer; 18 | import com.fasterxml.jackson.databind.ser.std.StdSerializer; 19 | import org.javafunk.funk.monads.Option; 20 | 21 | import java.io.IOException; 22 | 23 | public class OptionSerializer extends StdSerializer> { 24 | 25 | public OptionSerializer(JavaType type) { 26 | super(type); 27 | } 28 | 29 | @Override 30 | public boolean isEmpty(Option value) { 31 | return value == null || value.hasNoValue(); 32 | } 33 | 34 | @Override 35 | public void serialize(Option value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException { 36 | if (isEmpty(value)) { 37 | provider.defaultSerializeNull(jsonGenerator); 38 | } else { 39 | provider.defaultSerializeValue(value.getValue(), jsonGenerator); 40 | } 41 | } 42 | 43 | @Override 44 | public void serializeWithType(Option value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) throws IOException { 45 | serialize(value, jgen, provider); 46 | } 47 | 48 | @Override 49 | public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException { 50 | JavaType typeParameter = typeHint.containedType(0); 51 | if (typeParameter != null) { 52 | visitor.getProvider().findValueSerializer(typeParameter, null).acceptJsonFormatVisitor(visitor, typeParameter); 53 | return; 54 | } 55 | super.acceptJsonFormatVisitor(visitor, typeHint); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /funk-jackson/src/test/java/org/javafunk/funk/jackson/monad/GenericData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | 10 | package org.javafunk.funk.jackson.monad; 11 | 12 | import org.javafunk.funk.monads.Option; 13 | 14 | public class GenericData { 15 | public Option myData; 16 | } 17 | -------------------------------------------------------------------------------- /funk-jackson/src/test/java/org/javafunk/funk/jackson/monad/OptionData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011-Present Funk committers. 3 | * All rights reserved. 4 | * 5 | * The software in this package is published under the terms of the BSD 6 | * style license a copy of which has been included with this distribution in 7 | * the LICENSE.txt file. 8 | */ 9 | 10 | package org.javafunk.funk.jackson.monad; 11 | 12 | import org.javafunk.funk.monads.Option; 13 | 14 | public class OptionData { 15 | public Option myString; 16 | } 17 | -------------------------------------------------------------------------------- /funk-matchers/build.gradle: -------------------------------------------------------------------------------- 1 | import org.gradle.api.artifacts.maven.MavenDeployment 2 | 3 | dependencies { 4 | compile project(':funk-core') 5 | 6 | compile "org.hamcrest:hamcrest-core:1.3" 7 | compile "org.hamcrest:hamcrest-library:1.3" 8 | 9 | testCompile "org.javafunk.matchbox:matchbox:0.1.4" 10 | } 11 | 12 | uploadArchives { 13 | repositories { 14 | flatDir { 15 | dirs "${rootDir}/repos" 16 | } 17 | 18 | mavenDeployer { 19 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 20 | 21 | repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { 22 | authentication( 23 | userName: project.property("oss-releases.username"), 24 | password: project.property("oss-releases.password")) 25 | } 26 | 27 | snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { 28 | authentication( 29 | userName: project.property("oss-releases.username"), 30 | password: project.property("oss-releases.password")) 31 | } 32 | 33 | pom.project { 34 | name 'Funk Matchers' 35 | description 'Functional utilities for Java: hamcrest matchers for types in core APIs' 36 | packaging 'jar' 37 | url 'https://github.com/javafunk/funk' 38 | 39 | scm { 40 | url 'scm:git:https://github.com/javafunk/funk.git' 41 | connection 'scm:git:https://github.com/javafunk/funk.git' 42 | developerConnection 'http://github.com/javafunk/funk' 43 | } 44 | 45 | licenses { 46 | license { 47 | name 'BSD License' 48 | url 'http://www.opensource.org/licenses/bsd-license.php' 49 | } 50 | } 51 | 52 | developers { 53 | developer { 54 | id 'toby' 55 | name 'Toby Clemson' 56 | email 'tobyclemson@gmail.com' 57 | } 58 | developer { 59 | id 'andrew' 60 | name 'Andrew Kiellor' 61 | email 'akiellor@gmail.com' 62 | } 63 | developer { 64 | id 'simon' 65 | name 'Simon Brunning' 66 | email 'simon@brunningonline.net' 67 | } 68 | } 69 | } 70 | 71 | pom.whenConfigured { pom -> 72 | rootProject.addAsParentFor(pom) 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /funk-matchers/src/main/java/org/javafunk/funk/matchers/EitherMatchers.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.matchers; 2 | 3 | import org.hamcrest.Matcher; 4 | import org.javafunk.funk.matchers.implementations.EitherAnyLeftMatcher; 5 | import org.javafunk.funk.matchers.implementations.EitherAnyRightMatcher; 6 | import org.javafunk.funk.matchers.implementations.EitherSpecificLeftMatcher; 7 | import org.javafunk.funk.matchers.implementations.EitherSpecificRightMatcher; 8 | import org.javafunk.funk.monads.Either; 9 | 10 | public class EitherMatchers { 11 | public static Matcher> left() { 12 | return new EitherAnyLeftMatcher(); 13 | } 14 | 15 | public static Matcher> left(S leftValue) { 16 | return new EitherSpecificLeftMatcher(leftValue); 17 | } 18 | 19 | public static Matcher> leftOver(Class leftClass, Class rightClass) { 20 | return left(); 21 | } 22 | 23 | public static Matcher> leftOverValueAndType(S leftValue, Class rightClass) { 24 | return left(leftValue); 25 | } 26 | 27 | public static Matcher> right() { 28 | return new EitherAnyRightMatcher(); 29 | } 30 | 31 | public static Matcher> right(T rightValue) { 32 | return new EitherSpecificRightMatcher(rightValue); 33 | } 34 | 35 | public static Matcher> rightOver(Class leftClass, Class rightClass) { 36 | return right(); 37 | } 38 | 39 | public static Matcher> rightOverTypeAndValue(Class leftClass, T rightValue) { 40 | return right(rightValue); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /funk-matchers/src/main/java/org/javafunk/funk/matchers/OptionMatchers.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.matchers; 2 | 3 | import org.hamcrest.Matcher; 4 | import org.javafunk.funk.matchers.implementations.OptionHasAnyValueMatcher; 5 | import org.javafunk.funk.matchers.implementations.OptionHasNoValueMatcher; 6 | import org.javafunk.funk.matchers.implementations.OptionHasSpecificValueMatcher; 7 | import org.javafunk.funk.monads.Option; 8 | 9 | public class OptionMatchers { 10 | public static Matcher> hasValue() { 11 | return new OptionHasAnyValueMatcher(); 12 | } 13 | 14 | public static Matcher> hasValue(final T value) { 15 | return new OptionHasSpecificValueMatcher(value); 16 | } 17 | 18 | public static Matcher> hasValueOf(Class valueClass) { 19 | return hasValue(); 20 | } 21 | 22 | public static Matcher> hasNoValue() { 23 | return new OptionHasNoValueMatcher(); 24 | } 25 | 26 | public static Matcher> hasNoValueOf(Class valueClass) { 27 | return hasNoValue(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /funk-matchers/src/main/java/org/javafunk/funk/matchers/implementations/EitherAnyLeftMatcher.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.matchers.implementations; 2 | 3 | import org.apache.commons.lang3.builder.EqualsBuilder; 4 | import org.apache.commons.lang3.builder.HashCodeBuilder; 5 | import org.hamcrest.Description; 6 | import org.hamcrest.TypeSafeDiagnosingMatcher; 7 | import org.javafunk.funk.monads.Either; 8 | 9 | public class EitherAnyLeftMatcher extends TypeSafeDiagnosingMatcher> { 10 | @Override public void describeTo(Description description) { 11 | description.appendText("Either to be Left."); 12 | } 13 | 14 | @Override protected boolean matchesSafely(Either item, Description mismatchDescription) { 15 | mismatchDescription.appendText("got Right."); 16 | return item.isLeft(); 17 | } 18 | 19 | @Override public boolean equals(Object other) { 20 | return EqualsBuilder.reflectionEquals(this, other); 21 | } 22 | 23 | @Override public int hashCode() { 24 | return HashCodeBuilder.reflectionHashCode(this); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /funk-matchers/src/main/java/org/javafunk/funk/matchers/implementations/EitherAnyRightMatcher.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.matchers.implementations; 2 | 3 | import org.apache.commons.lang3.builder.EqualsBuilder; 4 | import org.apache.commons.lang3.builder.HashCodeBuilder; 5 | import org.hamcrest.Description; 6 | import org.hamcrest.TypeSafeDiagnosingMatcher; 7 | import org.javafunk.funk.monads.Either; 8 | 9 | public class EitherAnyRightMatcher extends TypeSafeDiagnosingMatcher> { 10 | @Override public void describeTo(Description description) { 11 | description.appendText("Either to be Right."); 12 | } 13 | 14 | @Override protected boolean matchesSafely(Either item, Description mismatchDescription) { 15 | mismatchDescription.appendText("got Left."); 16 | return item.isRight(); 17 | } 18 | 19 | @Override public boolean equals(Object other) { 20 | return EqualsBuilder.reflectionEquals(this, other); 21 | } 22 | 23 | @Override public int hashCode() { 24 | return HashCodeBuilder.reflectionHashCode(this); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /funk-matchers/src/main/java/org/javafunk/funk/matchers/implementations/EitherSpecificLeftMatcher.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.matchers.implementations; 2 | 3 | import org.apache.commons.lang3.builder.EqualsBuilder; 4 | import org.apache.commons.lang3.builder.HashCodeBuilder; 5 | import org.hamcrest.Description; 6 | import org.hamcrest.TypeSafeDiagnosingMatcher; 7 | import org.javafunk.funk.monads.Either; 8 | 9 | import static org.hamcrest.Matchers.is; 10 | 11 | public class EitherSpecificLeftMatcher extends TypeSafeDiagnosingMatcher> { 12 | private S expectedLeftValue; 13 | 14 | public EitherSpecificLeftMatcher(S expectedLeftValue) { 15 | this.expectedLeftValue = expectedLeftValue; 16 | } 17 | 18 | @Override protected boolean matchesSafely(Either item, Description mismatchDescription) { 19 | if (item.isRight()) { 20 | mismatchDescription.appendText("got Right with value: ").appendValue(item.getRight()); 21 | return false; 22 | } 23 | 24 | mismatchDescription.appendText("got Left with value: ").appendValue(item.getLeft()); 25 | return is(expectedLeftValue).matches(item.getLeft()); 26 | } 27 | 28 | @Override public void describeTo(Description description) { 29 | description.appendText("Either to be Left with value: ").appendValue(expectedLeftValue); 30 | } 31 | 32 | @Override public boolean equals(Object other) { 33 | return EqualsBuilder.reflectionEquals(this, other); 34 | } 35 | 36 | @Override public int hashCode() { 37 | return HashCodeBuilder.reflectionHashCode(this); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /funk-matchers/src/main/java/org/javafunk/funk/matchers/implementations/EitherSpecificRightMatcher.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.matchers.implementations; 2 | 3 | import org.apache.commons.lang3.builder.EqualsBuilder; 4 | import org.apache.commons.lang3.builder.HashCodeBuilder; 5 | import org.hamcrest.Description; 6 | import org.hamcrest.TypeSafeDiagnosingMatcher; 7 | import org.javafunk.funk.monads.Either; 8 | 9 | import static org.hamcrest.Matchers.is; 10 | 11 | public class EitherSpecificRightMatcher extends TypeSafeDiagnosingMatcher> { 12 | private T expectedRightValue; 13 | 14 | public EitherSpecificRightMatcher(T expectedRightValue) { 15 | this.expectedRightValue = expectedRightValue; 16 | } 17 | 18 | @Override protected boolean matchesSafely(Either item, Description mismatchDescription) { 19 | if (item.isLeft()) { 20 | mismatchDescription.appendText("got Left with value: ").appendValue(item.getLeft()); 21 | return false; 22 | } 23 | 24 | mismatchDescription.appendText("got Right with value: ").appendValue(item.getRight()); 25 | return is(expectedRightValue).matches(item.getRight()); 26 | } 27 | 28 | @Override public void describeTo(Description description) { 29 | description.appendText("Either to be Right with value: ").appendValue(expectedRightValue); 30 | } 31 | 32 | @Override public boolean equals(Object other) { 33 | return EqualsBuilder.reflectionEquals(this, other); 34 | } 35 | 36 | @Override public int hashCode() { 37 | return HashCodeBuilder.reflectionHashCode(this); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /funk-matchers/src/main/java/org/javafunk/funk/matchers/implementations/OptionHasAnyValueMatcher.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.matchers.implementations; 2 | 3 | import org.apache.commons.lang3.builder.EqualsBuilder; 4 | import org.apache.commons.lang3.builder.HashCodeBuilder; 5 | import org.hamcrest.Description; 6 | import org.hamcrest.TypeSafeDiagnosingMatcher; 7 | import org.javafunk.funk.monads.Option; 8 | 9 | public class OptionHasAnyValueMatcher extends TypeSafeDiagnosingMatcher> { 10 | @Override protected boolean matchesSafely(Option option, Description mismatchDescription) { 11 | mismatchDescription.appendText("Option with no value."); 12 | return option.hasValue(); 13 | } 14 | 15 | @Override public void describeTo(Description description) { 16 | description.appendText("Option with value."); 17 | } 18 | 19 | @Override public boolean equals(Object other) { 20 | return EqualsBuilder.reflectionEquals(this, other); 21 | } 22 | 23 | @Override public int hashCode() { 24 | return HashCodeBuilder.reflectionHashCode(this); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /funk-matchers/src/main/java/org/javafunk/funk/matchers/implementations/OptionHasNoValueMatcher.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.matchers.implementations; 2 | 3 | import org.apache.commons.lang3.builder.EqualsBuilder; 4 | import org.apache.commons.lang3.builder.HashCodeBuilder; 5 | import org.hamcrest.Description; 6 | import org.hamcrest.TypeSafeDiagnosingMatcher; 7 | import org.javafunk.funk.monads.Option; 8 | 9 | public class OptionHasNoValueMatcher extends TypeSafeDiagnosingMatcher> { 10 | @Override protected boolean matchesSafely(Option option, Description mismatchDescription) { 11 | if (option.hasValue()) { 12 | mismatchDescription.appendText("Option with value: ").appendValue(option.get()); 13 | return false; 14 | } 15 | return true; 16 | } 17 | 18 | @Override public void describeTo(Description description) { 19 | description.appendText("Option with no value."); 20 | } 21 | 22 | @Override public boolean equals(Object other) { 23 | return EqualsBuilder.reflectionEquals(this, other); 24 | } 25 | 26 | @Override public int hashCode() { 27 | return HashCodeBuilder.reflectionHashCode(this); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /funk-matchers/src/main/java/org/javafunk/funk/matchers/implementations/OptionHasSpecificValueMatcher.java: -------------------------------------------------------------------------------- 1 | package org.javafunk.funk.matchers.implementations; 2 | 3 | import org.apache.commons.lang3.builder.EqualsBuilder; 4 | import org.apache.commons.lang3.builder.HashCodeBuilder; 5 | import org.hamcrest.Description; 6 | import org.hamcrest.TypeSafeDiagnosingMatcher; 7 | import org.javafunk.funk.monads.Option; 8 | 9 | import static org.hamcrest.Matchers.is; 10 | 11 | public class OptionHasSpecificValueMatcher extends TypeSafeDiagnosingMatcher> { 12 | private final T value; 13 | 14 | public OptionHasSpecificValueMatcher(T value) { 15 | this.value = value; 16 | } 17 | 18 | @Override protected boolean matchesSafely(Option item, Description mismatchDescription) { 19 | if (item.hasNoValue()) { 20 | mismatchDescription.appendText("Option with no value."); 21 | return false; 22 | } 23 | 24 | mismatchDescription.appendText("Option with value: ").appendValue(item.get()); 25 | return is(value).matches(item.get()); 26 | } 27 | 28 | @Override public void describeTo(Description description) { 29 | description.appendText("Option with value: ").appendValue(value); 30 | } 31 | 32 | @Override public boolean equals(Object other) { 33 | return EqualsBuilder.reflectionEquals(this, other); 34 | } 35 | 36 | @Override public int hashCode() { 37 | return HashCodeBuilder.reflectionHashCode(this); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | signing.keyId= 2 | signing.password= 3 | signing.secretKeyRingFile= 4 | 5 | oss-releases.username= 6 | oss-releases.password= 7 | oss-releases.url= -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javafunk/funk/1020e94463516377bd5cbacbf6393a819db2df88/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Nov 12 15:53:18 EST 2014 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-bin.zip 7 | -------------------------------------------------------------------------------- /scripts/release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [[ "$DEBUG" == "true" ]]; then 4 | set -x 5 | fi 6 | 7 | set -e 8 | set -o pipefail 9 | 10 | sudo yum install -y gnupg 11 | 12 | if ! gpg --list-keys 44CE9D82; then 13 | gpg --import /var/go/tclemson.gpg.key 14 | fi 15 | 16 | cp -f /var/go/gradle.properties . 17 | 18 | ./gradlew uploadArchives 19 | 20 | REPO_ID=$(./gradlew nexusStagingList | grep "Repository ID" | cut -d ' ' -f 3 | cut -d ',' -f 1 | sort -r | head -n 1) 21 | 22 | ./gradlew nexusStagingClose -PrepoId=$REPO_ID 23 | sleep 1m 24 | ./gradlew nexusStagingPromote -PrepoId=$REPO_ID -------------------------------------------------------------------------------- /scripts/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [[ "$DEBUG" == "true" ]]; then 4 | set -x 5 | fi 6 | 7 | set -e 8 | set -o pipefail 9 | 10 | ./gradlew test -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'funk' 2 | include( 3 | "funk-core", 4 | "funk-matchers", 5 | "funk-jackson" 6 | ) 7 | --------------------------------------------------------------------------------