| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| IndexOfInGenerator |
|
| 1.6666666666666667;1.667 | ||||
| IndexOfInGenerator$IndexProcedure |
|
| 1.6666666666666667;1.667 |
| 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 java.io.Serializable; | |
| 20 | ||
| 21 | import org.apache.commons.functor.BinaryFunction; | |
| 22 | import org.apache.commons.functor.UnaryPredicate; | |
| 23 | import org.apache.commons.functor.UnaryProcedure; | |
| 24 | import org.apache.commons.functor.generator.Generator; | |
| 25 | ||
| 26 | /** | |
| 27 | * Return the index of the first Object in a {@link Generator} matching a {@link UnaryPredicate}, or -1 if not found. | |
| 28 | * | |
| 29 | * @version $Revision: 1156337 $ $Date: 2011-08-10 21:44:54 +0200 (Wed, 10 Aug 2011) $ | |
| 30 | */ | |
| 31 | 0 | public final class IndexOfInGenerator<T> |
| 32 | implements BinaryFunction<Generator<? extends T>, UnaryPredicate<? super T>, Number>, Serializable { | |
| 33 | /** | |
| 34 | * serialVersionUID declaration. | |
| 35 | */ | |
| 36 | private static final long serialVersionUID = -11365986575536471L; | |
| 37 | 0 | private static final IndexOfInGenerator<Object> INSTANCE = new IndexOfInGenerator<Object>(); |
| 38 | ||
| 39 | /** | |
| 40 | * Helper procedure. | |
| 41 | */ | |
| 42 | 0 | private static class IndexProcedure<T> implements UnaryProcedure<T> { |
| 43 | private final Generator<? extends T> generator; | |
| 44 | private final UnaryPredicate<? super T> pred; | |
| 45 | 0 | private long index = -1L; |
| 46 | 0 | private long current = 0L; |
| 47 | ||
| 48 | /** | |
| 49 | * Create a new IndexProcedure. | |
| 50 | * @pred test | |
| 51 | */ | |
| 52 | 0 | IndexProcedure(Generator<? extends T> generator, UnaryPredicate<? super T> pred) { |
| 53 | 0 | this.generator = generator; |
| 54 | 0 | this.pred = pred; |
| 55 | 0 | } |
| 56 | ||
| 57 | /** | |
| 58 | * {@inheritDoc} | |
| 59 | */ | |
| 60 | public void run(T obj) { | |
| 61 | 0 | if (index < 0 && pred.test(obj)) { |
| 62 | 0 | index = current; |
| 63 | 0 | generator.stop(); |
| 64 | } | |
| 65 | 0 | current++; |
| 66 | 0 | } |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * {@inheritDoc} | |
| 71 | * @param left Generator | |
| 72 | * @param right UnaryPredicate | |
| 73 | */ | |
| 74 | public Number evaluate(Generator<? extends T> left, UnaryPredicate<? super T> right) { | |
| 75 | 0 | IndexProcedure<T> findProcedure = new IndexProcedure<T>(left, right); |
| 76 | 0 | left.run(findProcedure); |
| 77 | 0 | return findProcedure.index; |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * {@inheritDoc} | |
| 82 | */ | |
| 83 | public boolean equals(Object obj) { | |
| 84 | 0 | return obj == this || obj != null && obj.getClass().equals(getClass()); |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * {@inheritDoc} | |
| 89 | */ | |
| 90 | public int hashCode() { | |
| 91 | 0 | return System.identityHashCode(INSTANCE); |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * Get a static {@link IndexOfInGenerator} instance. | |
| 96 | * @return {@link IndexOfInGenerator} | |
| 97 | */ | |
| 98 | public static IndexOfInGenerator<Object> instance() { | |
| 99 | 0 | return INSTANCE; |
| 100 | } | |
| 101 | } |