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 */
017package org.apache.juneau.internal;
018
019import java.util.function.*;
020
021/**
022 * Utilities when working with {@link Predicate Predicates} and {@link Consumer Consumers}.
023 */
024public class ConsumerUtils {
025
026   /**
027    * Returns <jk>true</jk> if the specified predicate is <jk>null</jk> or matches the specified value.
028
029    * @param <T> The type being tested.
030    * @param predicate The predicate.
031    * @param value The value to test.
032    * @return <jk>true</jk> if the specified predicate is <jk>null</jk> or matches the specified value.
033    */
034   public static <T> boolean test(Predicate<T> predicate, T value) {
035      return (predicate == null || predicate.test(value));
036   }
037
038   /**
039    * 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.
040    *
041    * @param <T> The type being tested.
042    * @param type The expected type.
043    * @param predicate The predicate.
044    * @param value The value.
045    * @return <jk>true</jk> if the specified predicate is <jk>null</jk> or matches the specified value.
046    */
047   public static <T> boolean test(Class<T> type, Predicate<T> predicate, Object value) {
048      return type.isInstance(value) && (predicate == null || predicate.test(type.cast(value)));
049   }
050
051   /**
052    * Consumes the specified value if the predicate is <jk>null</jk> or matches the specified value.
053    *
054    * @param <T> The type being consumed.
055    * @param predicate The predicate.
056    * @param consumer The consumer.
057    * @param value The value.
058    */
059   public static <T> void consume(Predicate<T> predicate, Consumer<T> consumer, T value) {
060      if (test(predicate, value))
061         consumer.accept(value);
062   }
063
064   /**
065    * Consumes the specified value if it's the specified type and the predicate is <jk>null</jk> or matches the specified value.
066    *
067    * @param <T> The type being consumed.
068    * @param type The expected type.
069    * @param predicate The predicate.
070    * @param consumer The consumer.
071    * @param value The value.
072    */
073   public static <T> void consume(Class<T> type, Predicate<T> predicate, Consumer<T> consumer, Object value) {
074      if (test(type, predicate, value))
075         consumer.accept(type.cast(value));
076   }
077}