001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.internal;
014
015import java.util.function.*;
016
017/**
018 * Utilities when working with {@link Predicate Predicates} and {@link Consumer Consumers}.
019 */
020public final class ConsumerUtils {
021
022   /**
023    * Returns <jk>true</jk> if the specified predicate is <jk>null</jk> or matches the specified value.
024
025    * @param <T> The type being tested.
026    * @param predicate The predicate.
027    * @param value The value to test.
028    * @return <jk>true</jk> if the specified predicate is <jk>null</jk> or matches the specified value.
029    */
030   public static <T> boolean test(Predicate<T> predicate, T value) {
031      return (predicate == null || predicate.test(value));
032   }
033
034   /**
035    * Returns <jk>true</jk> if the specified object is the specified type and the specified predicate is <jk>null</jk> or matches the specified value.
036    *
037    * @param <T> The type being tested.
038    * @param type The expected type.
039    * @param predicate The predicate.
040    * @param value The value.
041    * @return <jk>true</jk> if the specified predicate is <jk>null</jk> or matches the specified value.
042    */
043   public static <T> boolean test(Class<T> type, Predicate<T> predicate, Object value) {
044      return type.isInstance(value) && (predicate == null || predicate.test(type.cast(value)));
045   }
046
047   /**
048    * Consumes the specified value if the predicate is <jk>null</jk> or matches the specified value.
049    *
050    * @param <T> The type being consumed.
051    * @param predicate The predicate.
052    * @param consumer The consumer.
053    * @param value The value.
054    */
055   public static <T> void consume(Predicate<T> predicate, Consumer<T> consumer, T value) {
056      if (test(predicate, value))
057         consumer.accept(value);
058   }
059
060   /**
061    * Consumes the specified value if it's the specified type and the predicate is <jk>null</jk> or matches the specified value.
062    *
063    * @param <T> The type being consumed.
064    * @param type The expected type.
065    * @param predicate The predicate.
066    * @param consumer The consumer.
067    * @param value The value.
068    */
069   public static <T> void consume(Class<T> type, Predicate<T> predicate, Consumer<T> consumer, Object value) {
070      if (test(type, predicate, value))
071         consumer.accept(type.cast(value));
072   }
073}