001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.functor.core.algorithm; 018 019 import java.io.Serializable; 020 021 import org.apache.commons.functor.BinaryFunction; 022 import org.apache.commons.functor.UnaryPredicate; 023 import org.apache.commons.functor.UnaryProcedure; 024 import org.apache.commons.functor.generator.Generator; 025 026 /** 027 * Return the index of the first Object in a {@link Generator} matching a {@link UnaryPredicate}, or -1 if not found. 028 * 029 * @version $Revision: 1156337 $ $Date: 2011-08-10 21:44:54 +0200 (Wed, 10 Aug 2011) $ 030 */ 031 public final class IndexOfInGenerator<T> 032 implements BinaryFunction<Generator<? extends T>, UnaryPredicate<? super T>, Number>, Serializable { 033 /** 034 * serialVersionUID declaration. 035 */ 036 private static final long serialVersionUID = -11365986575536471L; 037 private static final IndexOfInGenerator<Object> INSTANCE = new IndexOfInGenerator<Object>(); 038 039 /** 040 * Helper procedure. 041 */ 042 private static class IndexProcedure<T> implements UnaryProcedure<T> { 043 private final Generator<? extends T> generator; 044 private final UnaryPredicate<? super T> pred; 045 private long index = -1L; 046 private long current = 0L; 047 048 /** 049 * Create a new IndexProcedure. 050 * @pred test 051 */ 052 IndexProcedure(Generator<? extends T> generator, UnaryPredicate<? super T> pred) { 053 this.generator = generator; 054 this.pred = pred; 055 } 056 057 /** 058 * {@inheritDoc} 059 */ 060 public void run(T obj) { 061 if (index < 0 && pred.test(obj)) { 062 index = current; 063 generator.stop(); 064 } 065 current++; 066 } 067 } 068 069 /** 070 * {@inheritDoc} 071 * @param left Generator 072 * @param right UnaryPredicate 073 */ 074 public Number evaluate(Generator<? extends T> left, UnaryPredicate<? super T> right) { 075 IndexProcedure<T> findProcedure = new IndexProcedure<T>(left, right); 076 left.run(findProcedure); 077 return findProcedure.index; 078 } 079 080 /** 081 * {@inheritDoc} 082 */ 083 public boolean equals(Object obj) { 084 return obj == this || obj != null && obj.getClass().equals(getClass()); 085 } 086 087 /** 088 * {@inheritDoc} 089 */ 090 public int hashCode() { 091 return System.identityHashCode(INSTANCE); 092 } 093 094 /** 095 * Get a static {@link IndexOfInGenerator} instance. 096 * @return {@link IndexOfInGenerator} 097 */ 098 public static IndexOfInGenerator<Object> instance() { 099 return INSTANCE; 100 } 101 }