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.BinaryPredicate; 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 * Tests whether a {@link Generator} contains an element that matches a {@link UnaryPredicate}. 028 * 029 * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $ 030 */ 031 public final class GeneratorContains<T> implements BinaryPredicate<Generator<? extends T>, UnaryPredicate<? super T>>, 032 Serializable { 033 /** 034 * serialVersionUID declaration. 035 */ 036 private static final long serialVersionUID = -1539983619621733276L; 037 private static final GeneratorContains<Object> INSTANCE = new GeneratorContains<Object>(); 038 039 /** 040 * Helper procedure. 041 */ 042 private static class ContainsProcedure<T> implements UnaryProcedure<T> { 043 private final UnaryPredicate<? super T> pred; 044 private boolean found; 045 046 /** 047 * Create a new ContainsProcedure. 048 * @pred test 049 */ 050 public ContainsProcedure(UnaryPredicate<? super T> pred) { 051 this.pred = pred; 052 } 053 054 /** 055 * {@inheritDoc} 056 */ 057 public void run(T obj) { 058 found |= pred.test(obj); 059 } 060 } 061 062 /** 063 * {@inheritDoc} 064 * @param left Generator 065 * @param right UnaryPredicate 066 */ 067 public boolean test(Generator<? extends T> left, UnaryPredicate<? super T> right) { 068 ContainsProcedure<T> findProcedure = new ContainsProcedure<T>(right); 069 left.run(findProcedure); 070 return findProcedure.found; 071 } 072 073 /** 074 * {@inheritDoc} 075 */ 076 public boolean equals(Object obj) { 077 return obj == this || obj != null && obj.getClass().equals(getClass()); 078 } 079 080 /** 081 * {@inheritDoc} 082 */ 083 public int hashCode() { 084 return System.identityHashCode(INSTANCE); 085 } 086 087 /** 088 * Get a static {@link GeneratorContains} instance. 089 * @return {@link GeneratorContains} 090 */ 091 public static GeneratorContains<Object> instance() { 092 return INSTANCE; 093 } 094 }