instance() {
79 | return INSTANCE;
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/core/algorithm/UntilDo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.algorithm;
18 |
19 | import org.apache.commons.functor.NullaryPredicate;
20 | import org.apache.commons.functor.NullaryProcedure;
21 |
22 | /**
23 | * Until-do algorithm (test before).
24 | */
25 | public class UntilDo extends PredicatedLoop {
26 |
27 | /**
28 | * Create a new UntilDo.
29 | * @param test whether to keep going
30 | * @param body to execute
31 | */
32 | public UntilDo(NullaryPredicate test, NullaryProcedure body) {
33 | super(body, test);
34 | }
35 |
36 | /**
37 | * {@inheritDoc}
38 | */
39 | public final void run() {
40 | while (!getTest().test()) {
41 | getBody().run();
42 | }
43 | }
44 |
45 | /**
46 | * {@inheritDoc}
47 | */
48 | @Override
49 | public String toString() {
50 | return "UntilDo<" + getTest() + "," + getBody() + ">";
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/core/algorithm/WhileDo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.algorithm;
18 |
19 | import org.apache.commons.functor.NullaryPredicate;
20 | import org.apache.commons.functor.NullaryProcedure;
21 |
22 | /**
23 | * While-do algorithm (test before).
24 | */
25 | public class WhileDo extends PredicatedLoop {
26 |
27 | /**
28 | * Create a new WhileDo.
29 | * @param test whether to keep going
30 | * @param body to execute
31 | */
32 | public WhileDo(NullaryPredicate test, NullaryProcedure body) {
33 | super(body, test);
34 | }
35 |
36 | /**
37 | * {@inheritDoc}
38 | */
39 | public final void run() {
40 | while (getTest().test()) {
41 | getBody().run();
42 | }
43 | }
44 |
45 | /**
46 | * {@inheritDoc}
47 | */
48 | @Override
49 | public String toString() {
50 | return "WhileDo<" + getTest() + "," + getBody() + ">";
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/core/algorithm/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | /**
19 | *
20 | * Various algorithm-esque functors.
21 | *
22 | */
23 | package org.apache.commons.functor.core.algorithm;
24 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/core/collection/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | /**
19 | *
20 | * {@link java.util.Collection Collection}-based functors, algorithms and utilities.
21 | *
22 | */
23 | package org.apache.commons.functor.core.collection;
24 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/core/comparator/ComparableComparator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.comparator;
18 |
19 | import java.util.Comparator;
20 |
21 | /**
22 | * A {@link Comparator Comparator} that compares {@link Comparable Comparable}
23 | * objects.
24 | *
25 | * This class was created based on commons-collection's ComparableComparator.
26 | *
27 | * @param the comparable type
28 | */
29 | final class ComparableComparator> implements Comparator {
30 |
31 | /** Singleton. */
32 | @SuppressWarnings("rawtypes")
33 | public static final ComparableComparator> INSTANCE = new ComparableComparator();
34 |
35 | /**
36 | * Create a new ComparableComparator.
37 | */
38 | public ComparableComparator() {
39 | super();
40 | }
41 |
42 | /**
43 | * {@inheritDoc}
44 | */
45 | public int compare(E o1, E o2) {
46 | return o1.compareTo(o2);
47 | }
48 |
49 | /**
50 | * {@inheritDoc}
51 | */
52 | @Override
53 | public boolean equals(Object obj) {
54 | return (obj instanceof ComparableComparator);
55 | }
56 |
57 | /**
58 | * {@inheritDoc}
59 | */
60 | @Override
61 | public int hashCode() {
62 | return toString().hashCode();
63 | }
64 |
65 | /**
66 | * {@inheritDoc}
67 | */
68 | @Override
69 | public String toString() {
70 | return "ComparableComparator";
71 | }
72 |
73 | /**
74 | * Get a ComparableComparator instance.
75 | * @param the comparable type
76 | * @return ComparableComparator
77 | */
78 | @SuppressWarnings("unchecked")
79 | public static > ComparableComparator instance() {
80 | return (ComparableComparator) INSTANCE;
81 | }
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/core/comparator/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | /**
19 | *
20 | * {@link java.util.Comparator Comparator}- and
21 | * {@link java.lang.Comparable Comparable}-based functors, algorithms and utilities.
22 | *
23 | */
24 | package org.apache.commons.functor.core.comparator;
25 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/core/composite/DoWhileNullaryProcedure.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.composite;
18 |
19 | import org.apache.commons.functor.NullaryPredicate;
20 | import org.apache.commons.functor.NullaryProcedure;
21 |
22 | /**
23 | * A {@link NullaryProcedure} implementation of a while loop. Given a {@link NullaryPredicate}
24 | * c and an {@link NullaryProcedure} p , {@link #run runs}
25 | * do { p.run(); } while(c.test())
.
26 | */
27 | public class DoWhileNullaryProcedure extends AbstractLoopNullaryProcedure {
28 |
29 | /**
30 | * Create a new DoWhileNullaryProcedure.
31 | * @param action to do
32 | * @param condition while true
33 | */
34 | public DoWhileNullaryProcedure(NullaryProcedure action, NullaryPredicate condition) {
35 | super(condition, action);
36 | }
37 |
38 | /**
39 | * {@inheritDoc}
40 | */
41 | public final void run() {
42 | do {
43 | getAction().run();
44 | } while (getCondition().test());
45 | }
46 |
47 | /**
48 | * {@inheritDoc}
49 | */
50 | @Override
51 | public String toString() {
52 | return "DoWhileNullaryProcedure";
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/core/composite/WhileDoNullaryProcedure.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.composite;
18 |
19 | import org.apache.commons.functor.NullaryPredicate;
20 | import org.apache.commons.functor.NullaryProcedure;
21 |
22 | /**
23 | * A {@link NullaryProcedure} implementation of a while loop. Given a {@link NullaryPredicate}
24 | * c and an {@link NullaryProcedure} p , {@link #run runs}
25 | * while(c.test()) { p.run(); }
.
26 | */
27 | public class WhileDoNullaryProcedure extends AbstractLoopNullaryProcedure {
28 | /**
29 | * Create a new WhileDoNullaryProcedure.
30 | * @param condition while
31 | * @param action to do
32 | */
33 | public WhileDoNullaryProcedure(NullaryPredicate condition, NullaryProcedure action) {
34 | super(condition, action);
35 | }
36 |
37 | /**
38 | * {@inheritDoc}
39 | */
40 | public void run() {
41 | while (getCondition().test()) {
42 | getAction().run();
43 | }
44 | }
45 |
46 | /**
47 | * {@inheritDoc}
48 | */
49 | @Override
50 | public String toString() {
51 | return "WhileDoNullaryProcedure";
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/core/composite/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | /**
19 | *
20 | * Functors composed of other functors.
21 | *
22 | */
23 | package org.apache.commons.functor.core.composite;
24 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/core/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | /**
19 | *
20 | * Commonly used functor implementations.
21 | *
22 | */
23 | package org.apache.commons.functor.core;
24 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/generator/BaseGenerator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed under the Apache License, Version 2.0 (the "License");
3 | * you may not use this file except in compliance with the License.
4 | * You may obtain a copy of the License at
5 | *
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | *
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package org.apache.commons.functor.generator;
16 |
17 | import java.util.Collection;
18 |
19 | import org.apache.commons.functor.Function;
20 | import org.apache.commons.functor.generator.util.CollectionTransformer;
21 |
22 | /**
23 | * Base class for generators. Adds support for all of the Algorithms to
24 | * each subclass.
25 | *
26 | * @param the type of elements held in this generator.
27 | * @since 1.0
28 | */
29 | public abstract class BaseGenerator implements Generator {
30 |
31 | /** Create a new generator. */
32 | public BaseGenerator() {
33 | super();
34 | }
35 |
36 | /**
37 | * {@inheritDoc}
38 | * Transforms this generator using the passed in
39 | * Function. An example function might turn the contents of the
40 | * generator into a {@link Collection} of elements.
41 | */
42 | public final T to(Function, ? extends T> transformer) {
43 | return transformer.evaluate(this);
44 | }
45 |
46 | /**
47 | * {@inheritDoc}
48 | * Same as to(new CollectionTransformer(collection)).
49 | */
50 | public final > C to(C collection) {
51 | return to(new CollectionTransformer(collection));
52 | }
53 |
54 | /**
55 | * {@inheritDoc}
56 | * Same as to(new CollectionTransformer()).
57 | */
58 | public final Collection toCollection() {
59 | return to(CollectionTransformer. toCollection());
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/generator/Generator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed under the Apache License, Version 2.0 (the "License");
3 | * you may not use this file except in compliance with the License.
4 | * You may obtain a copy of the License at
5 | *
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | *
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 | package org.apache.commons.functor.generator;
15 |
16 | import java.util.Collection;
17 |
18 | import org.apache.commons.functor.Function;
19 | import org.apache.commons.functor.Procedure;
20 |
21 | /**
22 | * The Generator interface defines a number of useful actions applying Procedures
23 | * to each in a series of argument Objects.
24 | *
25 | * @param the type of elements held in this generator.
26 | */
27 | public interface Generator {
28 | /**
29 | * Generators must implement this method.
30 | * @param proc Procedure to run
31 | */
32 | void run(Procedure super E> proc);
33 |
34 | /**
35 | * Transforms this generator using the passed in
36 | * transformer. An example transformer might turn the contents of the
37 | * generator into a {@link Collection} of elements.
38 | * @param the returned value type of the input {@link Function}
39 | * @param transformer Function to apply to this
40 | * @return transformation result
41 | */
42 | Z to(Function, ? extends Z> transformer);
43 |
44 | /**
45 | * Same as to(new CollectionTransformer(collection)).
46 | * @param the returned collection type
47 | * @param collection Collection to which my elements should be added
48 | * @return collection
49 | */
50 | > C to(C collection);
51 |
52 | /**
53 | * Same as to(new CollectionTransformer()).
54 | * @return Collection
55 | */
56 | Collection super E> toCollection();
57 | }
58 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/generator/loop/LoopGenerator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed under the Apache License, Version 2.0 (the "License");
3 | * you may not use this file except in compliance with the License.
4 | * You may obtain a copy of the License at
5 | *
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | *
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package org.apache.commons.functor.generator.loop;
16 |
17 | import org.apache.commons.functor.generator.BaseGenerator;
18 | import org.apache.commons.functor.generator.Generator;
19 |
20 | /**
21 | * Base class for generators that control execution flow, and may need to
22 | * stop the generation.
23 | *
24 | * @param the type of elements held in this generator.
25 | * @since 1.0
26 | */
27 | public abstract class LoopGenerator extends BaseGenerator {
28 |
29 | /** A generator can wrap another generator. */
30 | private final Generator extends E> wrappedGenerator;
31 |
32 | /** Set to true when the generator is {@link #stop stopped}. */
33 | private boolean stopped = false;
34 |
35 | /** Create a new generator. */
36 | public LoopGenerator() {
37 | this(null);
38 | }
39 |
40 | /**
41 | * A generator can wrap another generator. When wrapping generators you
42 | * should use probably this constructor since doing so will cause the
43 | * {@link #stop} method to stop the wrapped generator as well.
44 | * @param generator Generator to wrap
45 | */
46 | public LoopGenerator(Generator extends E> generator) {
47 | this.wrappedGenerator = generator;
48 | }
49 |
50 | /**
51 | * Get the generator that is being wrapped.
52 | * @return Generator
53 | */
54 | protected Generator extends E> getWrappedGenerator() {
55 | return wrappedGenerator;
56 | }
57 |
58 | /**
59 | * Stop the generator. Will stop the wrapped generator if one was set.
60 | */
61 | public void stop() {
62 | if (wrappedGenerator != null && wrappedGenerator instanceof LoopGenerator>) {
63 | ((LoopGenerator>) wrappedGenerator).stop();
64 | }
65 | stopped = true;
66 | }
67 |
68 | /**
69 | * Check if the generator is stopped.
70 | * @return true
if the generator is stopped, false
71 | * otherwise
72 | */
73 | public boolean isStopped() {
74 | return stopped;
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/generator/loop/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed under the Apache License, Version 2.0 (the "License");
3 | * you may not use this file except in compliance with the License.
4 | * You may obtain a copy of the License at
5 | *
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | *
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 | /**
15 | *
16 | * Contains code related to Generators that control execution flow.
17 | *
18 | */
19 | package org.apache.commons.functor.generator.loop;
20 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/generator/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | /**
19 | *
20 | * Contains code related to Generators.
21 | *
22 | */
23 | package org.apache.commons.functor.generator;
24 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/generator/util/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | /**
19 | *
20 | * Contains utility code for Generators.
21 | *
22 | */
23 | package org.apache.commons.functor.generator.util;
24 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/range/BoundType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.range;
18 |
19 | /**
20 | * Determine the bound type of a range.
21 | *
22 | * @see org.apache.commons.functor.range.Range
23 | * @see org.apache.commons.functor.range.Endpoint
24 | * @since 1.0
25 | */
26 | public enum BoundType {
27 | // values
28 | // ---------------------------------------------------------------
29 | /**
30 | * Represents an open bound, which value is not included in
31 | * the range.
32 | */
33 | OPEN,
34 | /**
35 | * Represents a closed bound, which value is included in the
36 | * range.
37 | */
38 | CLOSED;
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/core/src/main/java/org/apache/commons/functor/range/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | /**
19 | *
20 | * Contains code related to Ranges.
21 | *
22 | */
23 | package org.apache.commons.functor.range;
24 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/adapter/TestBinaryProcedureProcedure.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.adapter;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static org.junit.Assert.assertNotNull;
21 | import static org.junit.Assert.assertNull;
22 | import static org.junit.Assert.assertTrue;
23 |
24 | import org.apache.commons.functor.BaseFunctorTest;
25 | import org.apache.commons.functor.Procedure;
26 | import org.apache.commons.functor.core.NoOp;
27 | import org.junit.Test;
28 |
29 | /**
30 | */
31 | public class TestBinaryProcedureProcedure extends BaseFunctorTest {
32 |
33 | // Functor Testing Framework
34 | // ------------------------------------------------------------------------
35 |
36 | @Override
37 | protected Object makeFunctor() {
38 | return new BinaryProcedureProcedure(NoOp.INSTANCE);
39 | }
40 |
41 | // Tests
42 | // ------------------------------------------------------------------------
43 |
44 | @Test
45 | public void testRun() throws Exception {
46 | Procedure p = new BinaryProcedureProcedure(NoOp.INSTANCE);
47 | p.run(null);
48 | }
49 |
50 | @Test
51 | public void testEquals() throws Exception {
52 | Procedure p = new BinaryProcedureProcedure(NoOp.INSTANCE);
53 | assertEquals(p, p);
54 | assertObjectsAreEqual(p, new BinaryProcedureProcedure(NoOp.INSTANCE));
55 | assertObjectsAreNotEqual(p, NoOp.INSTANCE);
56 | assertObjectsAreNotEqual(p, new BinaryProcedureProcedure(IgnoreLeftProcedure.adapt(NoOp.INSTANCE)));
57 | assertTrue(!p.equals(null));
58 | }
59 |
60 | @Test
61 | public void testAdaptNull() throws Exception {
62 | assertNull(BinaryProcedureProcedure.adapt(null));
63 | }
64 |
65 | @Test
66 | public void testAdapt() throws Exception {
67 | assertNotNull(BinaryProcedureProcedure.adapt(NoOp.INSTANCE));
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/adapter/TestNullaryPredicatePredicate.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.adapter;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static org.junit.Assert.assertNotNull;
21 | import static org.junit.Assert.assertNull;
22 | import static org.junit.Assert.assertTrue;
23 |
24 | import org.apache.commons.functor.BaseFunctorTest;
25 | import org.apache.commons.functor.Predicate;
26 | import org.apache.commons.functor.core.Constant;
27 | import org.junit.Test;
28 |
29 | /**
30 | */
31 | public class TestNullaryPredicatePredicate extends BaseFunctorTest {
32 |
33 | // Functor Testing Framework
34 | // ------------------------------------------------------------------------
35 |
36 | @Override
37 | protected Object makeFunctor() {
38 | return new NullaryPredicatePredicate(Constant.TRUE);
39 | }
40 |
41 | // Tests
42 | // ------------------------------------------------------------------------
43 |
44 | @Test
45 | public void testEvaluate() throws Exception {
46 | Predicate p = new NullaryPredicatePredicate(Constant.TRUE);
47 | assertTrue(p.test(null));
48 | }
49 |
50 | @Test
51 | public void testEquals() throws Exception {
52 | Predicate p = new NullaryPredicatePredicate(Constant.TRUE);
53 | assertEquals(p,p);
54 | assertObjectsAreEqual(p,new NullaryPredicatePredicate(Constant.TRUE));
55 | assertObjectsAreNotEqual(p,Constant.TRUE);
56 | assertObjectsAreNotEqual(p,new NullaryPredicatePredicate(Constant.FALSE));
57 | assertObjectsAreNotEqual(p,Constant.FALSE);
58 | assertTrue(!p.equals(null));
59 | }
60 |
61 | @Test
62 | public void testAdaptNull() throws Exception {
63 | assertNull(NullaryPredicatePredicate.adapt(null));
64 | }
65 |
66 | @Test
67 | public void testAdapt() throws Exception {
68 | assertNotNull(NullaryPredicatePredicate.adapt(Constant.TRUE));
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorBinaryFunctionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.aggregator.functions;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static org.junit.Assert.assertNull;
21 |
22 | import java.util.Random;
23 |
24 | import org.apache.commons.functor.BaseFunctorTest;
25 | import org.apache.commons.functor.aggregator.functions.DoubleSumAggregatorBinaryFunction;
26 | import org.junit.Test;
27 |
28 | /**
29 | * Unit test for {@link DoubleSumAggregatorBinaryFunction}.
30 | */
31 | public class DoubleSumAggregatorBinaryFunctionTest extends BaseFunctorTest {
32 | private static final double DELTA = 0.01; //make room for some poor floating point support
33 |
34 | @Override
35 | protected Object makeFunctor() throws Exception {
36 | return new DoubleSumAggregatorBinaryFunction();
37 | }
38 |
39 | @Test
40 | public void testNulls() throws Exception {
41 | DoubleSumAggregatorBinaryFunction fct = (DoubleSumAggregatorBinaryFunction)makeFunctor();
42 | Double d = fct.evaluate(null, null);
43 | assertNull( d );
44 | d = fct.evaluate( null, 1.0 );
45 | assertEquals( 1.0, d.doubleValue(), DELTA );
46 | d = fct.evaluate( 2.0, null );
47 | assertEquals( 2.0, d.doubleValue(), DELTA );
48 | }
49 |
50 | @Test
51 | public void testSum() throws Exception {
52 | DoubleSumAggregatorBinaryFunction fct = (DoubleSumAggregatorBinaryFunction)makeFunctor();
53 | double total = 0.0;
54 | double result = 0.0;
55 | int calls = 31;
56 | Random rnd = new Random();
57 | for( int i = 0; i < calls; i++ ) {
58 | double number = rnd.nextDouble();
59 | total += number;
60 | result = fct.evaluate(result, number);
61 | assertEquals( result, total, DELTA );
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/aggregator/functions/IntCountAggregatorBinaryFunctionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.aggregator.functions;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static org.junit.Assert.assertNotNull;
21 | import static org.junit.Assert.assertNull;
22 |
23 | import org.apache.commons.functor.BaseFunctorTest;
24 | import org.apache.commons.functor.aggregator.functions.IntegerCountAggregatorBinaryFunction;
25 | import org.junit.Test;
26 |
27 | /**
28 | * Unit test for {@link IntegerCountAggregatorBinaryFunction}.
29 | */
30 | public class IntCountAggregatorBinaryFunctionTest extends BaseFunctorTest {
31 | @Override
32 | protected Object makeFunctor() throws Exception {
33 | return new IntegerCountAggregatorBinaryFunction();
34 | }
35 |
36 | @Test
37 | public void testNull() throws Exception {
38 | IntegerCountAggregatorBinaryFunction fct = (IntegerCountAggregatorBinaryFunction)makeFunctor();
39 | Integer i = fct.evaluate(null, null);
40 | assertNull( i );
41 | i = fct.evaluate( null, 1 );
42 | assertNull( i );
43 | i = fct.evaluate( 2, null );
44 | assertNotNull( i );
45 | assertEquals( i.intValue(), 3 );
46 | }
47 |
48 | @Test
49 | public void testCount() throws Exception {
50 | IntegerCountAggregatorBinaryFunction fct = (IntegerCountAggregatorBinaryFunction)makeFunctor();
51 | int count = 0;
52 | int calls = 31;
53 | for( int i = 1; i <= calls; i++ ) {
54 | count = fct.evaluate(count, 12345);
55 | assertEquals( i, count );
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorBinaryFunctionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.aggregator.functions;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static org.junit.Assert.assertNull;
21 |
22 | import java.util.Random;
23 |
24 | import org.apache.commons.functor.BaseFunctorTest;
25 | import org.apache.commons.functor.aggregator.functions.IntegerMaxAggregatorBinaryFunction;
26 | import org.junit.Test;
27 |
28 | /**
29 | * Unit test for {@link IntegerMaxAggregatorBinaryFunction}.
30 | */
31 | public class IntMaxAggregatorBinaryFunctionTest extends BaseFunctorTest {
32 | @Override
33 | protected Object makeFunctor() throws Exception {
34 | return new IntegerMaxAggregatorBinaryFunction();
35 | }
36 |
37 | @Test
38 | public void testNulls() throws Exception {
39 | IntegerMaxAggregatorBinaryFunction fct = (IntegerMaxAggregatorBinaryFunction) makeFunctor();
40 | Integer d = fct.evaluate(null, null);
41 | assertNull(d);
42 | d = fct.evaluate(null, 1);
43 | assertEquals(1, d.intValue());
44 | d = fct.evaluate(2, null);
45 | assertEquals(2, d.intValue());
46 | }
47 |
48 | @Test
49 | public void testMax() throws Exception {
50 | IntegerMaxAggregatorBinaryFunction fct = (IntegerMaxAggregatorBinaryFunction) makeFunctor();
51 | int max = 0;
52 | int result = 0;
53 | int number1 = 0;
54 | int number2 = 0;
55 | int calls = 31;
56 | Random rnd = new Random();
57 | for (int i = 0; i < calls; i++) {
58 | number1 = rnd.nextInt();
59 | number2 = rnd.nextInt();
60 | max = Math.max(number1, number2);
61 | result = fct.evaluate(number1, number2);
62 | assertEquals(result, max);
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorBinaryFunctionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.aggregator.functions;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static org.junit.Assert.assertNull;
21 |
22 | import java.util.Random;
23 |
24 | import org.apache.commons.functor.BaseFunctorTest;
25 | import org.apache.commons.functor.aggregator.functions.IntegerSumAggregatorBinaryFunction;
26 | import org.junit.Test;
27 |
28 | /**
29 | * Unit test for {@link IntegerSumAggregatorBinaryFunction}.
30 | */
31 | public class IntSumAggregatorBinaryFunctionTest extends BaseFunctorTest {
32 | @Override
33 | protected Object makeFunctor() throws Exception {
34 | return new IntegerSumAggregatorBinaryFunction();
35 | }
36 |
37 | @Test
38 | public void testNulls() throws Exception {
39 | IntegerSumAggregatorBinaryFunction fct = (IntegerSumAggregatorBinaryFunction) makeFunctor();
40 | Integer d = fct.evaluate(null, null);
41 | assertNull(d);
42 | d = fct.evaluate(null, 1);
43 | assertEquals(1, d.intValue());
44 | d = fct.evaluate(2, null);
45 | assertEquals(2, d.intValue());
46 | }
47 |
48 | @Test
49 | public void testSum() throws Exception {
50 | IntegerSumAggregatorBinaryFunction fct = (IntegerSumAggregatorBinaryFunction) makeFunctor();
51 | int total = 0;
52 | int result = 0;
53 | int calls = 31;
54 | Random rnd = new Random();
55 | for (int i = 0; i < calls; i++) {
56 | int number = rnd.nextInt();
57 | total += number;
58 | result = fct.evaluate(result, number);
59 | assertEquals(result, total);
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/core/TestIsNotNull.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static org.junit.Assert.assertTrue;
21 |
22 | import org.apache.commons.functor.BaseFunctorTest;
23 | import org.apache.commons.functor.Predicate;
24 | import org.junit.Test;
25 |
26 | /**
27 | */
28 | public class TestIsNotNull extends BaseFunctorTest {
29 |
30 | // Functor Testing Framework
31 | // ------------------------------------------------------------------------
32 |
33 | @Override
34 | protected Object makeFunctor() {
35 | return new IsNotNull();
36 | }
37 |
38 | // Tests
39 | // ------------------------------------------------------------------------
40 |
41 | @Test
42 | public void testTest() throws Exception {
43 | Predicate p = new IsNotNull();
44 | assertTrue(!p.test(null));
45 | assertTrue(p.test("foo"));
46 | assertTrue(p.test(Integer.valueOf(3)));
47 | }
48 |
49 | @Test
50 | public void testEquals() throws Exception {
51 | Predicate p = new IsNotNull();
52 | assertEquals(p, p);
53 | assertObjectsAreEqual(p, new IsNotNull());
54 | assertObjectsAreEqual(p, IsNotNull.instance());
55 | assertObjectsAreNotEqual(p, Constant.TRUE);
56 | }
57 |
58 | @Test
59 | public void testConstant() throws Exception {
60 | assertEquals(IsNotNull.instance(), IsNotNull.instance());
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/core/TestIsNull.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static org.junit.Assert.assertFalse;
21 | import static org.junit.Assert.assertTrue;
22 |
23 | import org.apache.commons.functor.BaseFunctorTest;
24 | import org.apache.commons.functor.Predicate;
25 | import org.junit.Test;
26 |
27 | /**
28 | */
29 | public class TestIsNull extends BaseFunctorTest {
30 |
31 | // Functor Testing Framework
32 | // ------------------------------------------------------------------------
33 |
34 | @Override
35 | protected Object makeFunctor() {
36 | return new IsNull();
37 | }
38 |
39 | // Tests
40 | // ------------------------------------------------------------------------
41 |
42 | @Test
43 | public void testTest() throws Exception {
44 | Predicate p = new IsNull();
45 | assertTrue(p.test(null));
46 | assertFalse(p.test("foo"));
47 | assertFalse(p.test(Integer.valueOf(3)));
48 | }
49 |
50 | @Test
51 | public void testAsBinary() throws Exception {
52 | assertTrue(IsNull.left().test(null, "not null"));
53 | assertFalse(IsNull.left().test("not null", null));
54 | assertTrue(IsNull.right().test("not null", null));
55 | assertFalse(IsNull.right().test(null, "not null"));
56 | }
57 |
58 | @Test
59 | public void testEquals() throws Exception {
60 | Predicate p = new IsNull();
61 | assertEquals(p, p);
62 | assertObjectsAreEqual(p, new IsNull());
63 | assertObjectsAreEqual(p, IsNull.instance());
64 | assertObjectsAreNotEqual(p, Constant.TRUE);
65 | }
66 |
67 | @Test
68 | public void testConstant() throws Exception {
69 | assertEquals(IsNull.instance(), IsNull.instance());
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/core/algorithm/TestDoUntil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.algorithm;
18 |
19 | import static org.junit.Assert.assertEquals;
20 |
21 | import org.apache.commons.functor.BaseFunctorTest;
22 | import org.apache.commons.functor.NullaryProcedure;
23 | import org.apache.commons.functor.core.Offset;
24 | import org.junit.Test;
25 |
26 | /**
27 | * Tests {@link DoUntil} algorithm.
28 | */
29 | public class TestDoUntil extends BaseFunctorTest {
30 |
31 | // Functor Testing Framework
32 | // ------------------------------------------------------------------------
33 |
34 | @Override
35 | protected Object makeFunctor() throws Exception {
36 | Counter counter = new Counter();
37 | return new DoUntil(counter, new Offset(10));
38 | }
39 |
40 | @Test
41 | public void testDoUntil() {
42 | for(int i=0;i<3;++i){
43 | Counter counter = new Counter();
44 | new DoUntil(counter, new Offset(i)).run();
45 | assertEquals(i+1,counter.count);
46 | }
47 | }
48 |
49 | // Classes
50 | // ------------------------------------------------------------------------
51 |
52 | static class Counter implements NullaryProcedure {
53 | public void run() {
54 | count++;
55 | }
56 | public int count = 0;
57 |
58 | @Override
59 | public boolean equals(Object obj) {
60 | if(this == obj) {
61 | return true;
62 | }
63 | if (obj == null || !obj.getClass().equals(getClass())) {
64 | return false;
65 | }
66 | Counter that = (Counter)obj;
67 | return this.count == that.count;
68 | }
69 |
70 | @Override
71 | public int hashCode() {
72 | int hash = "Counter".hashCode();
73 | hash <<= 2;
74 | hash ^= this.count;
75 | return hash;
76 | }
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/core/algorithm/TestDoWhile.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.algorithm;
18 |
19 | import static org.junit.Assert.assertEquals;
20 |
21 | import org.apache.commons.functor.BaseFunctorTest;
22 | import org.apache.commons.functor.NullaryProcedure;
23 | import org.apache.commons.functor.core.Limit;
24 | import org.junit.Test;
25 |
26 | /**
27 | * Tests {@link DoWhile} algorithm.
28 | */
29 | public class TestDoWhile extends BaseFunctorTest {
30 |
31 | // Functor Testing Framework
32 | // ------------------------------------------------------------------------
33 |
34 | @Override
35 | protected Object makeFunctor() throws Exception {
36 | Counter counter = new Counter();
37 | return new DoWhile(counter, new Limit(10));
38 | }
39 |
40 | @Test
41 | public void testDoWhile() {
42 | for(int i=0;i<3;i++){
43 | Counter counter = new Counter();
44 | new DoWhile(counter, new Limit(i)).run();
45 | assertEquals(i+1,counter.count);
46 | }
47 | }
48 |
49 | // Classes
50 | // ------------------------------------------------------------------------
51 |
52 | static class Counter implements NullaryProcedure {
53 | public void run() {
54 | count++;
55 | }
56 | public int count = 0;
57 |
58 | @Override
59 | public boolean equals(Object obj) {
60 | if(this == obj) {
61 | return true;
62 | }
63 | if (obj == null || !obj.getClass().equals(getClass())) {
64 | return false;
65 | }
66 | Counter that = (Counter)obj;
67 | return this.count == that.count;
68 | }
69 |
70 | @Override
71 | public int hashCode() {
72 | int hash = "Counter".hashCode();
73 | hash <<= 2;
74 | hash ^= this.count;
75 | return hash;
76 | }
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/core/algorithm/TestGeneratorContains.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.algorithm;
18 |
19 | import static org.junit.Assert.assertFalse;
20 | import static org.junit.Assert.assertTrue;
21 |
22 | import java.util.Arrays;
23 | import java.util.List;
24 |
25 | import org.apache.commons.functor.BaseFunctorTest;
26 | import org.apache.commons.functor.Predicate;
27 | import org.apache.commons.functor.adapter.LeftBoundPredicate;
28 | import org.apache.commons.functor.core.IsEqual;
29 | import org.apache.commons.functor.core.algorithm.GeneratorContains;
30 | import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Tests {@link GeneratorContains} algorithm.
35 | */
36 | public class TestGeneratorContains extends BaseFunctorTest {
37 |
38 | @Override
39 | protected Object makeFunctor() throws Exception {
40 | return new GeneratorContains();
41 | }
42 |
43 | @Test
44 | public void testContains() {
45 | assertTrue(new GeneratorContains()
46 | .test(IteratorToGeneratorAdapter.adapt(list.iterator()), equalsThree));
47 | assertFalse(new GeneratorContains().test(IteratorToGeneratorAdapter.adapt(list.iterator()),
48 | equalsTwentyThree));
49 | }
50 |
51 | // Attributes
52 | // ------------------------------------------------------------------------
53 |
54 | private List list = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
55 | private Predicate equalsThree = LeftBoundPredicate.bind(IsEqual.instance(), Integer.valueOf(3));
56 | private Predicate equalsTwentyThree = LeftBoundPredicate.bind(IsEqual.instance(), Integer.valueOf(23));
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/core/algorithm/TestIndexOfInGenerator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.algorithm;
18 |
19 | import static org.junit.Assert.assertEquals;
20 |
21 | import java.util.Arrays;
22 | import java.util.List;
23 |
24 | import org.apache.commons.functor.BaseFunctorTest;
25 | import org.apache.commons.functor.Predicate;
26 | import org.apache.commons.functor.adapter.LeftBoundPredicate;
27 | import org.apache.commons.functor.core.IsEqual;
28 | import org.apache.commons.functor.generator.loop.IteratorToGeneratorAdapter;
29 | import org.junit.Test;
30 |
31 | /**
32 | * Tests {@link IndexOfInGenerator} algorithm.
33 | */
34 | public class TestIndexOfInGenerator extends BaseFunctorTest {
35 |
36 | @Override
37 | protected Object makeFunctor() throws Exception {
38 | return IndexOfInGenerator.instance();
39 | }
40 |
41 | @Test
42 | public void testIndexOfInGenerator() {
43 | assertEquals(3L, new IndexOfInGenerator().evaluate(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsThree));
44 | }
45 |
46 | // Attributes
47 | // ------------------------------------------------------------------------
48 |
49 | private List list = Arrays.asList(0,1,2,3,4,5,6,7,8,9);
50 | private Predicate equalsThree = LeftBoundPredicate.bind(IsEqual.instance(),Integer.valueOf(3));
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/core/algorithm/TestUntilDo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.algorithm;
18 |
19 | import static org.junit.Assert.assertEquals;
20 |
21 | import org.apache.commons.functor.BaseFunctorTest;
22 | import org.apache.commons.functor.NullaryProcedure;
23 | import org.apache.commons.functor.core.Offset;
24 | import org.junit.Test;
25 |
26 | /**
27 | * Tests {@link UntilDo} algorithm.
28 | */
29 | public class TestUntilDo extends BaseFunctorTest {
30 |
31 | // Functor Testing Framework
32 | // ------------------------------------------------------------------------
33 |
34 | @Override
35 | protected Object makeFunctor() throws Exception {
36 | Counter counter = new Counter();
37 | return new UntilDo(new Offset(10), counter);
38 | }
39 |
40 | @Test
41 | public void testUntilDo() {
42 | for (int i=0;i<3;i++){
43 | Counter counter = new Counter();
44 | new UntilDo(new Offset(i), counter).run();
45 | assertEquals(i,counter.count);
46 | }
47 | }
48 |
49 | // Classes
50 | // ------------------------------------------------------------------------
51 |
52 | static class Counter implements NullaryProcedure {
53 | public void run() {
54 | count++;
55 | }
56 | public int count = 0;
57 |
58 | @Override
59 | public boolean equals(Object obj) {
60 | if(this == obj) {
61 | return true;
62 | }
63 | if (obj == null || !obj.getClass().equals(getClass())) {
64 | return false;
65 | }
66 | Counter that = (Counter)obj;
67 | return this.count == that.count;
68 | }
69 |
70 | @Override
71 | public int hashCode() {
72 | int hash = "Counter".hashCode();
73 | hash <<= 2;
74 | hash ^= this.count;
75 | return hash;
76 | }
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/core/comparator/TestComparableComparator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.comparator;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static org.junit.Assert.assertTrue;
21 |
22 | import org.junit.Test;
23 |
24 | /**
25 | */
26 | public class TestComparableComparator {
27 |
28 | // Tests
29 | // ------------------------------------------------------------------------
30 |
31 | @Test
32 | public void testCompareIntegers() {
33 | assertTrue(ComparableComparator. instance().compare(Integer.valueOf(Integer.MIN_VALUE),
34 | Integer.valueOf(Integer.MIN_VALUE)) == 0);
35 | assertTrue(ComparableComparator. instance().compare(Integer.valueOf(-1), Integer.valueOf(-1)) == 0);
36 | assertTrue(ComparableComparator. instance().compare(Integer.valueOf(0), Integer.valueOf(0)) == 0);
37 | assertTrue(ComparableComparator. instance().compare(Integer.valueOf(Integer.MAX_VALUE),
38 | Integer.valueOf(Integer.MAX_VALUE)) == 0);
39 | assertTrue(ComparableComparator. instance().compare(Integer.valueOf(1), Integer.valueOf(1)) == 0);
40 | }
41 |
42 | @Test(expected = NullPointerException.class)
43 | public void testCompareNull() {
44 | ComparableComparator. instance().compare(null, Integer.valueOf(2));
45 | }
46 |
47 | @Test
48 | public void testEqualsAndHashCode() {
49 | assertEquals(new ComparableComparator(), new ComparableComparator());
50 | assertEquals(new ComparableComparator().hashCode(), new ComparableComparator().hashCode());
51 | assertTrue(!new ComparableComparator().equals(null));
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/core/comparator/TestIsEquivalent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.comparator;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static org.junit.Assert.assertFalse;
21 | import static org.junit.Assert.assertTrue;
22 |
23 | import org.apache.commons.functor.core.Constant;
24 | import org.junit.Test;
25 |
26 | /**
27 | */
28 | public class TestIsEquivalent extends BaseComparisonPredicateTest {
29 |
30 | // Functor Testing Framework
31 | // ------------------------------------------------------------------------
32 |
33 | @Override
34 | protected Object makeFunctor() {
35 | return IsEquivalent.INSTANCE;
36 | }
37 |
38 | // Tests
39 | // ------------------------------------------------------------------------
40 |
41 | @Test
42 | public void testTest() throws Exception {
43 | IsEquivalent p = IsEquivalent. instance();
44 | assertTrue(!p.test(Integer.valueOf(2), Integer.valueOf(4)));
45 | assertTrue(!p.test(Integer.valueOf(3), Integer.valueOf(4)));
46 | assertTrue(p.test(Integer.valueOf(4), Integer.valueOf(4)));
47 | assertTrue(!p.test(Integer.valueOf(5), Integer.valueOf(4)));
48 | assertTrue(!p.test(Integer.valueOf(6), Integer.valueOf(4)));
49 | }
50 |
51 | @Test
52 | public void testInstance() {
53 | assertTrue(IsEquivalent.instance("Xyzzy").test("Xyzzy"));
54 | assertTrue(!IsEquivalent.instance("Xyzzy").test("z"));
55 | }
56 |
57 | @Test
58 | public void testEquals() throws Exception {
59 | IsEquivalent> p = IsEquivalent.instance();
60 | assertEquals(p, p);
61 |
62 | assertObjectsAreEqual(p, new IsEquivalent>());
63 | assertObjectsAreEqual(p, new IsEquivalent(ComparableComparator. instance()));
64 | assertObjectsAreNotEqual(p, Constant.FALSE);
65 | assertFalse(p.equals(null));
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/core/composite/TestAbstractLoopNullaryProcedure.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.composite;
18 |
19 | import static org.junit.Assert.assertEquals;
20 |
21 | import org.apache.commons.functor.BaseFunctorTest;
22 | import org.apache.commons.functor.NullaryPredicate;
23 | import org.apache.commons.functor.NullaryProcedure;
24 | import org.apache.commons.functor.core.Constant;
25 | import org.apache.commons.functor.core.NoOp;
26 | import org.junit.Test;
27 |
28 | /**
29 | */
30 | public class TestAbstractLoopNullaryProcedure extends BaseFunctorTest {
31 |
32 | // Functor Testing Framework
33 | // ------------------------------------------------------------------------
34 |
35 | @Override
36 | protected Object makeFunctor() {
37 | return new MockLoopProcedure(Constant.FALSE, NoOp.INSTANCE);
38 | }
39 |
40 | // Tests
41 | // ------------------------------------------------------------------------
42 |
43 | @Test
44 | public void testEquals() {
45 | MockLoopProcedure p = new MockLoopProcedure(Constant.FALSE, NoOp.INSTANCE);
46 | assertEquals(p,p);
47 | assertObjectsAreEqual(p,new MockLoopProcedure(Constant.FALSE, NoOp.INSTANCE));
48 | assertObjectsAreNotEqual(p,new MockLoopProcedure(Constant.TRUE, NoOp.INSTANCE));
49 | assertObjectsAreNotEqual(p,new MockLoopProcedure(Constant.FALSE, new NullarySequence()));
50 | }
51 | }
52 |
53 | class MockLoopProcedure extends AbstractLoopNullaryProcedure {
54 | public MockLoopProcedure(NullaryPredicate condition, NullaryProcedure action) {
55 | super(condition,action);
56 | }
57 |
58 | public void run() {
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/core/composite/TestComposite.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.composite;
18 |
19 | import static org.junit.Assert.assertNotNull;
20 |
21 | import org.apache.commons.functor.core.Identity;
22 | import org.apache.commons.functor.core.LeftIdentity;
23 | import org.apache.commons.functor.core.NoOp;
24 | import org.apache.commons.functor.core.comparator.IsGreaterThan;
25 | import org.junit.Test;
26 |
27 | /**
28 | */
29 | public class TestComposite {
30 |
31 | // Tests
32 | // ------------------------------------------------------------------------
33 |
34 | @Test
35 | public void testUnaryMethods() {
36 | assertNotNull(Composite.procedure(NoOp.instance()));
37 | assertNotNull(Composite.procedure(NoOp.instance(),Identity.instance()));
38 | assertNotNull(Composite.predicate(Identity.instance()));
39 | assertNotNull(Composite.predicate(Identity.instance(),Identity.instance()));
40 | assertNotNull(Composite.function(Identity.instance()));
41 | assertNotNull(Composite.function(Identity.instance(),Identity.instance()));
42 | }
43 |
44 | @Test
45 | public void testBinaryMethods() {
46 | assertNotNull(Composite.function(LeftIdentity.function(),LeftIdentity.function(),LeftIdentity.function()));
47 | assertNotNull(Composite.predicate(IsGreaterThan.instance(),new Identity>(),new Identity>()));
48 | assertNotNull(Composite.function(LeftIdentity.function(),Identity.instance(),Identity.instance()));
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/core/composite/TestNot.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.core.composite;
18 |
19 | import static org.junit.Assert.assertEquals;
20 | import static org.junit.Assert.assertNotNull;
21 | import static org.junit.Assert.assertNull;
22 | import static org.junit.Assert.assertTrue;
23 |
24 | import org.apache.commons.functor.BaseFunctorTest;
25 | import org.apache.commons.functor.Predicate;
26 | import org.apache.commons.functor.core.Constant;
27 | import org.junit.Test;
28 |
29 | /**
30 | */
31 | public class TestNot extends BaseFunctorTest {
32 |
33 | // Functor Testing Framework
34 | // ------------------------------------------------------------------------
35 |
36 | @Override
37 | protected Object makeFunctor() {
38 | return new Not(Constant.TRUE);
39 | }
40 |
41 | // Tests
42 | // ------------------------------------------------------------------------
43 |
44 | @Test
45 | public void testTest() throws Exception {
46 | Predicate truePred = new Not(Constant.FALSE);
47 | assertTrue(truePred.test(null));
48 | assertTrue(truePred.test("xyzzy"));
49 | assertTrue(truePred.test(Integer.valueOf(3)));
50 | }
51 |
52 | @Test
53 | public void testEquals() throws Exception {
54 | Not p = new Not(Constant.TRUE);
55 | assertEquals(p, p);
56 | assertObjectsAreEqual(p, new Not(Constant.TRUE));
57 | assertObjectsAreEqual(p, Not.not(Constant.TRUE));
58 | assertObjectsAreNotEqual(p, new Not(Constant.FALSE));
59 | assertObjectsAreNotEqual(p, Constant.TRUE);
60 | assertTrue(!p.equals(null));
61 | }
62 |
63 | @Test
64 | public void testNotNull() throws Exception {
65 | assertNull(Not.not(null));
66 | }
67 |
68 | @Test
69 | public void testNotNotNull() throws Exception {
70 | assertNotNull(Not.not(Constant.truePredicate()));
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/aggregator/list/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | /**
18 | * Package which groups a set of examples for list-backed {@link org.apache.commons.functor.aggregator.Aggregator}.
19 | */
20 | package org.apache.commons.functor.example.aggregator.list;
21 |
22 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/aggregator/nostore/AggregatedFunctionSample.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.commons.functor.example.aggregator.nostore;
19 |
20 | import org.apache.commons.functor.BinaryFunction;
21 | import org.apache.commons.functor.aggregator.AbstractNoStoreAggregator;
22 |
23 | /**
24 | * Shows how to implement own aggregated function to use with
25 | * {@link AbstractNoStoreAggregator}.
26 | */
27 | public class AggregatedFunctionSample {
28 | /**
29 | * Uses a custom function together with a nostore aggregator to provide a
30 | * continuous logical OR in between all the values added to the aggregator.
31 | */
32 | public void useOwnFunction() throws Exception {
33 | AbstractNoStoreAggregator or = new AbstractNoStoreAggregator( new OwnBinaryFunction() ) {
34 | @Override
35 | protected Boolean initialValue() {
36 | return false;
37 | }
38 | };
39 | or.add( false );
40 | System.out.println( "OR : " + or.evaluate() );
41 | or.add( true );
42 | System.out.println( "OR : " + or.evaluate() );
43 | or.add( false );
44 | System.out.println( "OR : " + or.evaluate() );
45 | }
46 |
47 | /**
48 | * This class implements a logical OR: it OR's the 2 parameters passed in
49 | * and returns the result.
50 | * (There are similar implementations already in functor, this is just to
51 | * be used as an example for doing this with a nostore aggregator.
52 | */
53 | static class OwnBinaryFunction implements BinaryFunction {
54 | public Boolean evaluate(Boolean left, Boolean right) {
55 | return left.booleanValue() || right.booleanValue();
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/aggregator/nostore/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | /**
19 | * Package which groups a set of examples for {@link org.apache.commons.functor.aggregator.AbstractNoStoreAggregator}.
20 | */
21 | package org.apache.commons.functor.example.aggregator.nostore;
22 |
23 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/aggregator/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | /**
18 | * Package which groups a set of examples for {@link org.apache.commons.functor.aggregator.Aggregator}.
19 | */
20 | package org.apache.commons.functor.example.aggregator;
21 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/four/Abs.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.four;
18 |
19 | import org.apache.commons.functor.Function;
20 |
21 | /**
22 | * Evaluates to the absolute Integer value of the Number-valued
23 | * input parameter.
24 | */
25 | public final class Abs implements Function {
26 |
27 | public Integer evaluate(Number num) {
28 | return Integer.valueOf(Math.abs(num.intValue()));
29 | }
30 |
31 | public static final Abs instance() {
32 | return INSTANCE;
33 | }
34 |
35 | private static final Abs INSTANCE = new Abs();
36 | }
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/four/IsInteger.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.four;
18 |
19 | import org.apache.commons.functor.Predicate;
20 |
21 | /**
22 | * Tests to true iff the input object can be converted to
23 | * an Integer by {@link ToInteger}.
24 | */
25 | public final class IsInteger implements Predicate {
26 | public boolean test(String obj) {
27 | try {
28 | ToInteger.instance().evaluate(obj);
29 | return true;
30 | } catch (RuntimeException e){
31 | return false;
32 | }
33 | }
34 |
35 | public static final IsInteger instance() {
36 | return INSTANCE;
37 | }
38 |
39 | private static final IsInteger INSTANCE = new IsInteger();
40 | }
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/four/NthColumn.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.four;
18 |
19 | import java.util.StringTokenizer;
20 |
21 | import org.apache.commons.functor.Function;
22 |
23 | /**
24 | * Evaluates the input String to extrace the nth whitespace
25 | * delmited column.
26 | */
27 | public final class NthColumn implements Function {
28 | public NthColumn(int n) {
29 | this.n = n;
30 | }
31 |
32 | public String evaluate(String obj) {
33 | StringTokenizer toker = new StringTokenizer(obj);
34 | for (int count = 0; count < n && toker.hasMoreTokens();count++) {
35 | toker.nextToken();
36 | }
37 | return toker.hasMoreTokens() ? toker.nextToken() : null;
38 | }
39 |
40 | private final int n;
41 |
42 | public static final NthColumn instance(int n) {
43 | return new NthColumn(n);
44 | }
45 | }
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/four/TestSoccer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.four;
18 |
19 | import static org.junit.Assert.assertEquals;
20 |
21 | import org.junit.Test;
22 |
23 | /**
24 | * See http://pragprog.com/pragdave/Practices/Kata/KataFour.rdoc,v
25 | * for more information on this Kata.
26 | */
27 | public class TestSoccer {
28 |
29 | @Test
30 | public void testProcess() {
31 | // for our soccer example, we want to select the second column of the
32 | // line with the minimal difference between the seventh and ninth columns.
33 | assertEquals(
34 | "Aston_Villa",
35 | DataMunger.process(getClass().getResourceAsStream("soccer.txt"),1,6,8));
36 | }
37 |
38 | }
39 |
40 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/four/TestWeather.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.four;
18 |
19 | import junit.framework.Test;
20 | import junit.framework.TestCase;
21 | import junit.framework.TestSuite;
22 |
23 | /**
24 | * See http://pragprog.com/pragdave/Practices/Kata/KataFour.rdoc,v
25 | * for more information on this Kata.
26 | */
27 | public class TestWeather extends TestCase {
28 | public TestWeather(String testName) {
29 | super(testName);
30 | }
31 |
32 | public static Test suite() {
33 | return new TestSuite(TestWeather.class);
34 | }
35 |
36 | public void testProcess() {
37 | // for our soccer example, we want to select the first column of the
38 | // line with the minimal difference between the second and third columns.
39 | assertEquals(
40 | "14",
41 | DataMunger.process(getClass().getResourceAsStream("weather.txt"),0,1,2));
42 | }
43 |
44 | }
45 |
46 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/four/ToInteger.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.four;
18 |
19 | import org.apache.commons.functor.Function;
20 |
21 | /**
22 | * Converts a String value to an Integer, throwing an exception if no such conversion can be made.
23 | *
24 | * Trailing, non-{@link Character#isDigit digit} characters are ignored.
25 | *
26 | */
27 | public final class ToInteger implements Function {
28 |
29 | public Integer evaluate(String str) {
30 | StringBuilder buf = new StringBuilder();
31 | for (int i = 0; i < str.length(); i++) {
32 | if (Character.isDigit(str.charAt(i))) {
33 | buf.append(str.charAt(i));
34 | } else {
35 | break;
36 | }
37 | }
38 | try {
39 | return Integer.valueOf(buf.toString());
40 | } catch (NumberFormatException e) {
41 | throw new NumberFormatException(str);
42 | }
43 | }
44 |
45 | public static final ToInteger instance() {
46 | return INSTANCE;
47 | }
48 |
49 | private static final ToInteger INSTANCE = new ToInteger();
50 | }
51 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/one/Add.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.one;
18 |
19 | import org.apache.commons.functor.Function;
20 | import org.apache.commons.functor.adapter.LeftBoundFunction;
21 |
22 | /**
23 | */
24 | public class Add extends ArithmeticOperation {
25 | public Number evaluate(Number left, Number right) {
26 | return Integer.valueOf(left.intValue() + right.intValue());
27 | }
28 |
29 | public static Add instance() {
30 | return INSTANCE;
31 | }
32 |
33 | public static Function to(int factor) {
34 | return LeftBoundFunction.bind(INSTANCE, factor);
35 | }
36 |
37 | private static Add INSTANCE = new Add();
38 | }
39 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/one/ArithmeticOperation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.one;
18 |
19 | import org.apache.commons.functor.BinaryFunction;
20 |
21 | public abstract class ArithmeticOperation implements BinaryFunction {
22 | }
23 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/one/Divide.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.one;
18 |
19 | import org.apache.commons.functor.Function;
20 | import org.apache.commons.functor.adapter.RightBoundFunction;
21 |
22 | /**
23 | */
24 | public class Divide extends ArithmeticOperation {
25 |
26 | public Number evaluate(Number left, Number right) {
27 | return Integer.valueOf(left.intValue() / right.intValue());
28 | }
29 |
30 | public static Divide instance() {
31 | return INSTANCE;
32 | }
33 |
34 | public static Function by(int factor) {
35 | return RightBoundFunction.bind(INSTANCE, factor);
36 | }
37 |
38 | private static Divide INSTANCE = new Divide();
39 | }
40 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/one/Mod.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.one;
18 |
19 | import org.apache.commons.functor.Function;
20 | import org.apache.commons.functor.adapter.RightBoundFunction;
21 |
22 | /**
23 | */
24 | public class Mod extends ArithmeticOperation {
25 | public Number evaluate(Number left, Number right) {
26 | return Integer.valueOf(left.intValue() % right.intValue());
27 | }
28 |
29 | public static Mod instance() {
30 | return INSTANCE;
31 | }
32 |
33 | public static Function by(int factor) {
34 | return RightBoundFunction.bind(instance(),factor);
35 | }
36 |
37 | private static Mod INSTANCE = new Mod();
38 | }
39 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/one/Money.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.one;
18 |
19 | /**
20 | */
21 | public class Money {
22 | public Money(int cents) {
23 | this.cents = cents;
24 | }
25 |
26 | public int getValueAsCents() {
27 | return cents;
28 | }
29 |
30 | @Override
31 | public boolean equals(Object obj) {
32 | if (obj instanceof Money) {
33 | Money that = (Money) obj;
34 | return getValueAsCents() == that.getValueAsCents();
35 | } else {
36 | return false;
37 | }
38 | }
39 |
40 | @Override
41 | public int hashCode() {
42 | return getValueAsCents();
43 | }
44 |
45 | @Override
46 | public String toString() {
47 | return getValueAsCents() + " cents";
48 | }
49 |
50 | private int cents;
51 | }
52 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/one/Multiply.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.one;
18 |
19 | import org.apache.commons.functor.Function;
20 | import org.apache.commons.functor.adapter.LeftBoundFunction;
21 |
22 | /**
23 | */
24 | public class Multiply extends ArithmeticOperation {
25 |
26 | public Number evaluate(Number left, Number right) {
27 | return Integer.valueOf(left.intValue() * right.intValue());
28 | }
29 |
30 | public static Multiply instance() {
31 | return INSTANCE;
32 | }
33 |
34 | public static Function by(int factor) {
35 | return LeftBoundFunction.bind(INSTANCE, factor);
36 | }
37 |
38 | private static Multiply INSTANCE = new Multiply();
39 | }
40 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/one/Product.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.one;
18 |
19 | import org.apache.commons.functor.Function;
20 |
21 |
22 | /**
23 | */
24 | public class Product {
25 | public Product(String name, String sku, int cost) {
26 | this(name,sku,ToMoney.from(Multiply.by(cost)));
27 | }
28 |
29 | public Product(String name, String sku, Function super Integer, Money> price) {
30 | this.name = name;
31 | this.sku = sku;
32 | this.priceFunction = price;
33 | }
34 |
35 | public String getName() {
36 | return name;
37 | }
38 |
39 | public Function super Integer, Money> getPriceFunction() {
40 | return priceFunction;
41 | }
42 |
43 | public String getSku() {
44 | return sku;
45 | }
46 |
47 | public void setName(String string) {
48 | name = string;
49 | }
50 |
51 | public void setPriceFunction(Function super Integer, Money> function) {
52 | priceFunction = function;
53 | }
54 |
55 | public void setSku(String string) {
56 | sku = string;
57 | }
58 |
59 | public Money getPrice(int quantity) {
60 | return priceFunction.evaluate(quantity);
61 | }
62 |
63 | private String name;
64 | private String sku;
65 | private Function super Integer, Money> priceFunction;
66 | }
67 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/one/Subtract.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.one;
18 |
19 | import org.apache.commons.functor.Function;
20 | import org.apache.commons.functor.adapter.LeftBoundFunction;
21 |
22 | /**
23 | */
24 | public class Subtract extends ArithmeticOperation {
25 | public Number evaluate(Number left, Number right) {
26 | return Integer.valueOf(left.intValue() - right.intValue());
27 | }
28 |
29 | public static Subtract instance() {
30 | return INSTANCE;
31 | }
32 |
33 | public static Function from(int factor) {
34 | return LeftBoundFunction.bind(INSTANCE, factor);
35 | }
36 |
37 | private static Subtract INSTANCE = new Subtract();
38 | }
39 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/one/ToMoney.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.one;
18 |
19 | import org.apache.commons.functor.Function;
20 | import org.apache.commons.functor.core.composite.Composite;
21 |
22 | /**
23 | */
24 | public class ToMoney implements Function {
25 |
26 | public Money evaluate(Number cents) {
27 | return new Money(cents.intValue());
28 | }
29 |
30 | public static ToMoney instance() {
31 | return INSTANCE;
32 | }
33 |
34 | public static Function from(Function super X, ? extends Number> fn) {
35 | return Composite.function(INSTANCE, fn);
36 | }
37 |
38 | private static ToMoney INSTANCE = new ToMoney();
39 | }
40 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/two/BaseBinaryChop.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.two;
18 |
19 | import java.util.Arrays;
20 | import java.util.List;
21 |
22 | /**
23 | * See http://pragprog.com/pragdave/Practices/Kata/KataTwo.rdoc,v for more information on this Kata.
24 | *
25 | */
26 | public abstract class BaseBinaryChop implements BinaryChop {
27 | public int find(int seeking, int[] in) {
28 | Integer[] In = new Integer[in.length];
29 | for (int i = 0; i < in.length; i++) {
30 | In[i] = Integer.valueOf(in[i]);
31 | }
32 | return find(Integer.valueOf(seeking), In);
33 | }
34 |
35 | public int find(Integer seeking, Integer[] in) {
36 | return find(seeking, Arrays.asList(in));
37 | }
38 |
39 | protected static int compare(List list, int index, Integer obj) {
40 | return ((Comparable) list.get(index)).compareTo(obj);
41 | }
42 |
43 | protected static boolean greaterThan(List list, int index, Integer obj) {
44 | return compare(list, index, obj) > 0;
45 | }
46 |
47 | protected static boolean equals(List list, int index, Integer obj) {
48 | return compare(list, index, obj) == 0;
49 | }
50 |
51 | protected static final Integer NEGATIVE_ONE = Integer.valueOf(-1);
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/kata/two/BinaryChop.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.kata.two;
18 |
19 | import java.util.List;
20 |
21 | /**
22 | * See http://pragprog.com/pragdave/Practices/Kata/KataTwo.rdoc,v
23 | * for more information on this Kata.
24 | */
25 | public interface BinaryChop {
26 | int find(int seeking, int[] in);
27 | int find(Integer seeking, Integer[] in);
28 | int find(Integer seeking, List in);
29 | }
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/lines/Contains.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.lines;
18 |
19 | import org.apache.commons.functor.Predicate;
20 |
21 |
22 | /**
23 | */
24 | public class Contains implements Predicate {
25 | public Contains(String str) {
26 | this.str = str;
27 | }
28 |
29 | public boolean test(T obj) {
30 | return null != obj && obj.toString().indexOf(str) != -1;
31 | }
32 |
33 | private String str = null;
34 | }
35 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/lines/Count.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.lines;
18 |
19 | import org.apache.commons.functor.NullaryProcedure;
20 |
21 | /**
22 | */
23 | public class Count implements NullaryProcedure {
24 | public void run() {
25 | count++;
26 | }
27 |
28 | public int getCount() {
29 | return count;
30 | }
31 |
32 | private int count = 0;
33 | }
34 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/lines/StartsWith.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.lines;
18 |
19 | import org.apache.commons.functor.Predicate;
20 |
21 |
22 | /**
23 | */
24 | public class StartsWith implements Predicate {
25 | public StartsWith(String prefix) {
26 | this.prefix = prefix;
27 | }
28 |
29 | public boolean test(T obj) {
30 | return null != obj && obj.toString().startsWith(prefix);
31 | }
32 |
33 | private String prefix = null;
34 | }
35 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/lines/Sum.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.lines;
18 |
19 | import org.apache.commons.functor.BinaryFunction;
20 |
21 | /**
22 | */
23 | public class Sum implements BinaryFunction {
24 | public Integer evaluate(Number left, Number right) {
25 | return left.intValue() + right.intValue();
26 | }
27 |
28 | public static final Sum instance() {
29 | return INSTANCE;
30 | }
31 |
32 | private static final Sum INSTANCE = new Sum();
33 | }
34 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/lines/TestAll.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.lines;
18 |
19 | import junit.framework.Test;
20 | import junit.framework.TestCase;
21 | import junit.framework.TestSuite;
22 |
23 | /**
24 | */
25 | public class TestAll extends TestCase {
26 | public TestAll(String testName) {
27 | super(testName);
28 | }
29 |
30 | public static Test suite() {
31 | TestSuite suite = new TestSuite();
32 |
33 | suite.addTest(TestLines.suite());
34 |
35 | return suite;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/lines/WordCount.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.lines;
18 |
19 | import java.util.StringTokenizer;
20 |
21 | import org.apache.commons.functor.Function;
22 |
23 | /**
24 | */
25 | public class WordCount implements Function {
26 | public Integer evaluate(String obj) {
27 | return new StringTokenizer(obj).countTokens();
28 | }
29 |
30 | public static WordCount instance() {
31 | return INSTANCE;
32 | }
33 |
34 | private static final WordCount INSTANCE = new WordCount();
35 | }
36 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/map/LazyMap.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.map;
18 |
19 | import java.util.Map;
20 |
21 | import org.apache.commons.functor.BinaryFunction;
22 | import org.apache.commons.functor.Function;
23 |
24 | /**
25 | */
26 | public class LazyMap extends FunctoredMap {
27 | public LazyMap(Map map, final Function factory) {
28 | super(map);
29 | setOnGet(new BinaryFunction, K, V>() {
30 | public V evaluate(Map map, K key) {
31 | if (map.containsKey(key)) {
32 | return map.get(key);
33 | } else {
34 | V value = factory.evaluate(key);
35 | map.put(key,value);
36 | return value;
37 | }
38 | }
39 | });
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/map/PredicatedMap.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.map;
18 |
19 | import java.lang.reflect.Array;
20 | import java.util.Iterator;
21 | import java.util.Map;
22 |
23 | import org.apache.commons.functor.BinaryPredicate;
24 | import org.apache.commons.functor.BinaryProcedure;
25 | import org.apache.commons.functor.Predicate;
26 | import org.apache.commons.functor.adapter.BinaryProcedureBinaryFunction;
27 | import org.apache.commons.functor.core.composite.ConditionalBinaryFunction;
28 |
29 | /**
30 | */
31 | public class PredicatedMap extends FunctoredMap {
32 | public PredicatedMap(Map map, final Predicate keyPredicate, final Predicate valuePredicate) {
33 | super(map);
34 | setOnPut(new ConditionalBinaryFunction, Object[], V>(
35 | new BinaryPredicate, Object[]>() {
36 | @SuppressWarnings("unchecked")
37 | public boolean test(Map a, Object[] b) {
38 | return keyPredicate.test((K)Array.get(b,0)) &&
39 | valuePredicate.test((V)Array.get(b,1));
40 | }
41 | },
42 | DEFAULT_ON_PUT,
43 | BinaryProcedureBinaryFunction., Object[], V>adapt(new Throw, Object>(new IllegalArgumentException()))));
44 |
45 | setOnPutAll(new BinaryProcedure, Map>() {
46 | public void run(Map dest, Map src) {
47 | for (Iterator> iter = src.entrySet().iterator(); iter.hasNext(); ) {
48 | Map.Entry pair = iter.next();
49 | if (keyPredicate.test(pair.getKey()) &&
50 | valuePredicate.test(pair.getValue())) {
51 | dest.put(pair.getKey(),pair.getValue());
52 | }
53 | }
54 | }
55 | });
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/core/src/test/java/org/apache/commons/functor/example/map/TestAll.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.commons.functor.example.map;
18 |
19 | import junit.framework.Test;
20 | import junit.framework.TestCase;
21 | import junit.framework.TestSuite;
22 |
23 | /**
24 | */
25 | public class TestAll extends TestCase {
26 | public TestAll(String testName) {
27 | super(testName);
28 | }
29 |
30 | public static Test suite() {
31 | TestSuite suite = new TestSuite();
32 |
33 | suite.addTest(TestPredicatedMap.suite());
34 | suite.addTest(TestFixedSizeMap.suite());
35 | suite.addTest(TestLazyMap.suite());
36 |
37 | return suite;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/core/src/test/resources/org/apache/commons/functor/example/kata/four/soccer.txt:
--------------------------------------------------------------------------------
1 | # Licensed to the Apache Software Foundation (ASF) under one or more
2 | # contributor license agreements. See the NOTICE file distributed with
3 | # this work for additional information regarding copyright ownership.
4 | # The ASF licenses this file to You under the Apache License, Version 2.0
5 | # (the "License"); you may not use this file except in compliance with
6 | # the License. You may obtain a copy of the License at
7 |
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 |
16 | English Premier League, 2001/2002 Tables
17 |
18 | Source http://sunsite.tut.fi/rec/riku/soccer_data/tab/93_94/table.eng0.01_02.html
19 | Team P W L D F A Pts
20 | 1. Arsenal 38 26 9 3 79 - 36 87
21 | 2. Liverpool 38 24 8 6 67 - 30 80
22 | 3. Manchester_U 38 24 5 9 87 - 45 77
23 | 4. Newcastle 38 21 8 9 74 - 52 71
24 | 5. Leeds 38 18 12 8 53 - 37 66
25 | 6. Chelsea 38 17 13 8 66 - 38 64
26 | 7. West_Ham 38 15 8 15 48 - 57 53
27 | 8. Aston_Villa 38 12 14 12 46 - 47 50
28 | 9. Tottenham 38 14 8 16 49 - 53 50
29 | 10. Blackburn 38 12 10 16 55 - 51 46
30 | 11. Southampton 38 12 9 17 46 - 54 45
31 | 12. Middlesbrough 38 12 9 17 35 - 47 45
32 | 13. Fulham 38 10 14 14 36 - 44 44
33 | 14. Charlton 38 10 14 14 38 - 49 44
34 | 15. Everton 38 11 10 17 45 - 57 43
35 | 16. Bolton 38 9 13 16 44 - 62 40
36 | 17. Sunderland 38 10 10 18 29 - 51 40
37 | -------------------------------------------------------
38 | 18. Ipswich 38 9 9 20 41 - 64 36
39 | 19. Derby 38 8 6 24 33 - 63 30
40 | 20. Leicester 38 5 13 20 30 - 64 28
41 |
42 |
--------------------------------------------------------------------------------
/src/conf/checkstyle.properties:
--------------------------------------------------------------------------------
1 | # Licensed to the Apache Software Foundation (ASF) under one or more
2 | # contributor license agreements. See the NOTICE file distributed with
3 | # this work for additional information regarding copyright ownership.
4 | # The ASF licenses this file to You under the Apache License, Version 2.0
5 | # (the "License"); you may not use this file except in compliance with
6 | # the License. You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 |
16 | # http://checkstyle.sf.net/config_javadoc.html
17 | checkstyle.javadoc.scope = public
18 | checkstyle.require.packagehtml = true
19 | checkstyle.require.version = false
20 | checkstyle.allow.noauthor = true
21 |
22 | # http://checkstyle.sf.net/config_naming.html
23 | checkstyle.pattern.const = ^[A-Z](_?[A-Z0-9]+)*$
24 | checkstyle.pattern.static = ^[a-z][a-zA-Z0-9]*$
25 | checkstyle.pattern.parameter = ^[a-z][a-zA-Z0-9]*$
26 | checkstyle.pattern.package = ^[a-z]+(\.[a-z][a-z0-9]*)*$
27 | checkstyle.pattern.type = ^[A-Z][a-zA-Z0-9]*$
28 | checkstyle.pattern.method = ^[a-z][a-zA-Z0-9]*$
29 | checkstyle.pattern.localvar = ^[a-z][a-zA-Z0-9]*$
30 | checkstyle.pattern.localfinalvar = ^[a-z][a-zA-Z0-9]*$
31 |
32 | # http://checkstyle.sf.net/config_header.html
33 | checkstyle.header.ignore.line = 1,2,3,4,5,6
34 |
35 | # http://checkstyle.sf.net/config_import.html
36 | checkstyle.illegal.imports = sun,com
37 |
38 | # http://checkstyle.sf.net/config_sizes.html
39 | checkstyle.maxlinelen = 120
40 | # be overly strict here for while
41 | checkstyle.maxmethodlen = 25
42 | checkstyle.maxconstructorlen = 25
43 | checkstyle.maxfilelen = 200
44 | checkstyle.maxparameters = 4
45 |
46 | # http://checkstyle.sf.net/config_whitespace.html
47 | checkstyle.allow.tabs = false
48 | checkstyle.ignore.whitespace = true
49 | checkstyle.paren.pad = nopad
50 | checkstyle.wrap.operator = eol
51 |
52 | # http://checkstyle.sf.net/config_modifiers.html
53 | checkstyle.pattern.publicmember = ^$
54 |
55 | # http://checkstyle.sf.net/config_blocks.html
56 | checkstyle.block.try = stmt
57 | checkstyle.block.catch = stmt
58 | checkstyle.block.finally = stmt
59 | checkstyle.lcurly.type = eol
60 | checkstyle.lcurly.method = eol
61 | checkstyle.lcurly.other = eol
62 | checkstyle.rcurly = same
63 |
64 | # http://checkstyle.sf.net/config_misc.html
65 | checkstyle.illegal.instantiations = java.lang.Boolean,java.lang.String
66 |
--------------------------------------------------------------------------------
/src/conf/jdepend.properties:
--------------------------------------------------------------------------------
1 | # Licensed to the Apache Software Foundation (ASF) under one or more
2 | # contributor license agreements. See the NOTICE file distributed with
3 | # this work for additional information regarding copyright ownership.
4 | # The ASF licenses this file to You under the Apache License, Version 2.0
5 | # (the "License"); you may not use this file except in compliance with
6 | # the License. You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 |
16 | # http://www.clarkware.com/software/JDepend.html
17 |
18 | # ignore JDK classes for dependency purposes
19 | ignore.java=java.*
20 |
21 | # in theory, the following lines should be sufficient, but don't seem to have any effect
22 | java.lang=0
23 | java.io=0
24 | java.util=0
25 |
--------------------------------------------------------------------------------
/src/site/resources/download_functor.cgi:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # Just call the standard mirrors.cgi script. It will use download.html
3 | # as the input template.
4 | exec /www/www.apache.org/dyn/mirrors/mirrors.cgi $*
--------------------------------------------------------------------------------
/src/site/resources/images/functor-logo-white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/apache/commons-functor/9552c00ea3741c2240f3bea9d48ea9ea2fa53ec4/src/site/resources/images/functor-logo-white.png
--------------------------------------------------------------------------------
/src/site/resources/images/functor-logo-white.xcf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/apache/commons-functor/9552c00ea3741c2240f3bea9d48ea9ea2fa53ec4/src/site/resources/images/functor-logo-white.xcf
--------------------------------------------------------------------------------
/src/site/site.xml:
--------------------------------------------------------------------------------
1 |
2 |
18 |
19 |
20 | Commons Functor
21 | /images/functor-logo-white.png
22 | /index.html
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/src/site/xdoc/building.xml:
--------------------------------------------------------------------------------
1 |
2 |
18 |
19 |
20 | Building
21 | Apache Commons Development Team
22 | Rodney Waldhoff
23 |
24 |
25 |
26 |
27 |
28 | To build Commons Functor from scratch, you must first obtain the source,
29 | from
30 | the SVN server .
31 |
32 |
33 | In order to build Commons Functor you will need Maven .
34 | Install a recent Maven release.
35 |
42 |
43 |
44 | With Maven installed, you should be able to run an arbitrary maven goal from the root Commons Functor
45 | directory. Commonly used goals include
46 | clean
, test
, compile
, package
, install
and site
.
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------