All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.atlassian.util.concurrent.Functions Maven / Gradle / Ivy

Go to download

This project contains utility classes that are used by various products and projects inside Atlassian and may have some utility to the world at large.

The newest version!
/**
 * Copyright 2009 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.atlassian.util.concurrent;

import java.util.function.Function;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

public final class Functions {
  /**
   * Get a function that uses the Supplier as a factory for all inputs.
   *
   * @param  the key type, ignored
   * @param  the result type
   * @param supplier called for all inputs
   * @return the function
   */
  public static  Function fromSupplier(final @NotNull Supplier supplier) {
    requireNonNull(supplier, "supplier");
    return d -> supplier.get();
  }

  /**
   * Get the value from a supplier.
   *
   * @param  the type returned, note the Supplier can be covariant.
   * @return a function that extracts the value from a supplier
   */
  static  Function, T> fromSupplier() {
    return Supplier::get;
  }

  /**
   * Get a function that weakly memoizes the output – ie. subsequent calls for
   * the same input value will return the same reference if it has not been
   * garbage collected because there are no external strong referents to it.
   *
   * @param  the input or key type for the function. Must be a value
   * (immutable) and have a well behaved hashcode implementation.
   * @param  the output type of the for the function.
   * @param f the function to call if the value is not already cached.
   * @return the function that will .
   */
  public static  Function weakMemoize(Function f) {
    return WeakMemoizer.weakMemoizer(f);
  }

  /**
   * Function that can be used to ignore any RuntimeExceptions that a
   * {@link Supplier} may produce and return null instead.
   * 
   * @param  the result type
   * @return a Function that transforms an exception into a null
   */
  static  Function, Supplier> ignoreExceptions() {
    return new ExceptionIgnorer<>();
  }

  static class ExceptionIgnorer implements Function, Supplier> {
    public Supplier apply(final Supplier from) {
      return new IgnoreAndReturnNull<>(from);
    }
  }

  static class IgnoreAndReturnNull implements Supplier {
    private final Supplier delegate;

    IgnoreAndReturnNull(final Supplier delegate) {
      this.delegate = requireNonNull(delegate, "delegate");
    }

    public T get() {
      try {
        return delegate.get();
      } catch (final RuntimeException ignore) {
        return null;
      }
    }
  }

  // /CLOVER:OFF
  private Functions() {}
  // /CLOVER:ON
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy