Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * 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 com.google.common.base;
 
 
 
 import  javax.annotation.Nullable;

Useful suppliers.

All methods return serializable suppliers as long as they're given serializable parameters.

Author(s):
Laurence Gonsalves
Harry Heymann
Since:
2.0 (imported from Google Collections Library)
 
 public final class Suppliers {
   private Suppliers() {}

  
Returns a new supplier which is the composition of the provided function and supplier. In other words, the new supplier's value will be computed by retrieving the value from supplier, and then applying function to that value. Note that the resulting supplier will not call supplier or invoke function until it is called.
 
   public static <F, T> Supplier<T> compose(
       Function<? super F, T> functionSupplier<F> supplier) {
     Preconditions.checkNotNull(function);
     Preconditions.checkNotNull(supplier);
     return new SupplierComposition<F, T>(functionsupplier);
   }
 
   private static class SupplierComposition<F, T>
       implements Supplier<T>, Serializable {
     final Function<? super F, T> function;
     final Supplier<F> supplier;
 
     SupplierComposition(Function<? super F, T> functionSupplier<F> supplier) {
       this. = function;
       this. = supplier;
     }
 
     @Override public T get() {
       return .apply(.get());
     }
 
     @Override public boolean equals(@Nullable Object obj) {
       if (obj instanceof SupplierComposition) {
         SupplierComposition<?, ?> that = (SupplierComposition<?, ?>) obj;
         return .equals(that.function) && .equals(that.supplier);
       }
       return false;
     }
 
     @Override public int hashCode() {
       return Objects.hashCode();
     }
 
     @Override public String toString() {
       return "Suppliers.compose(" +  + ", " +  + ")";
     }
 
     private static final long serialVersionUID = 0;
   }

  
Returns a supplier which caches the instance retrieved during the first call to get() and returns that value on subsequent calls to get(). See: memoization

The returned supplier is thread-safe. The supplier's serialized form does not contain the cached value, which will be recalculated when get() is called on the reserialized instance.

If delegate is an instance created by an earlier call to memoize, it is returned directly.

  public static <T> Supplier<T> memoize(Supplier<T> delegate) {
    return (delegate instanceof MemoizingSupplier)
        ? delegate
        : new MemoizingSupplier<T>(Preconditions.checkNotNull(delegate));
  }
  static class MemoizingSupplier<T> implements Supplier<T>, Serializable {
    final Supplier<T> delegate;
    transient volatile boolean initialized;
    // "value" does not need to be volatile; visibility piggy-backs
    // on volatile read of "initialized".
    transient T value;
    MemoizingSupplier(Supplier<T> delegate) {
      this. = delegate;
    }
    @Override public T get() {
      // A 2-field variant of Double Checked Locking.
      if (!) {
        synchronized (this) {
          if (!) {
            T t = .get();
             = t;
             = true;
            return t;
          }
        }
      }
      return ;
    }
    @Override public String toString() {
      return "Suppliers.memoize(" +  + ")";
    }
    private static final long serialVersionUID = 0;
  }

  
Returns a supplier that caches the instance supplied by the delegate and removes the cached value after the specified time has passed. Subsequent calls to get() return the cached value if the expiration time has not passed. After the expiration time, a new value is retrieved, cached, and returned. See: memoization

The returned supplier is thread-safe. The supplier's serialized form does not contain the cached value, which will be recalculated when get() is called on the reserialized instance.

Parameters:
duration the length of time after a value is created that it should stop being returned by subsequent get() calls
unit the unit that duration is expressed in
Throws:
IllegalArgumentException if duration is not positive
Since:
2.0
  public static <T> Supplier<T> memoizeWithExpiration(
      Supplier<T> delegatelong durationTimeUnit unit) {
    return new ExpiringMemoizingSupplier<T>(delegatedurationunit);
  }
      implements Supplier<T>, Serializable {
    final Supplier<T> delegate;
    final long durationNanos;
    transient volatile T value;
    // The special value 0 means "not yet initialized".
    transient volatile long expirationNanos;
        Supplier<T> delegatelong durationTimeUnit unit) {
      this. = Preconditions.checkNotNull(delegate);
      this. = unit.toNanos(duration);
      Preconditions.checkArgument(duration > 0);
    }
    @Override public T get() {
      // Another variant of Double Checked Locking.
      //
      // We use two volatile reads.  We could reduce this to one by
      // putting our fields into a holder class, but (at least on x86)
      // the extra memory consumption and indirection are more
      // expensive than the extra volatile reads.
      long nanos = ;
      long now = Platform.systemNanoTime();
      if (nanos == 0 || now - nanos >= 0) {
        synchronized (this) {
          if (nanos == ) {  // recheck for lost race
            T t = .get();
             = t;
            nanos = now + ;
            // In the very unlikely event that nanos is 0, set it to 1;
            // no one will notice 1 ns of tardiness.
             = (nanos == 0) ? 1 : nanos;
            return t;
          }
        }
      }
      return ;
    }
    @Override public String toString() {
      // This is a little strange if the unit the user provided was not NANOS,
      // but we don't want to store the unit just for toString
      return "Suppliers.memoizeWithExpiration(" +  + ", " +
           + ", NANOS)";
    }
    private static final long serialVersionUID = 0;
  }

  
Returns a supplier that always supplies instance.
  public static <T> Supplier<T> ofInstance(@Nullable T instance) {
    return new SupplierOfInstance<T>(instance);
  }
  private static class SupplierOfInstance<T>
      implements Supplier<T>, Serializable {
    final T instance;
    SupplierOfInstance(@Nullable T instance) {
      this. = instance;
    }
    @Override public T get() {
      return ;
    }
    @Override public boolean equals(@Nullable Object obj) {
      if (obj instanceof SupplierOfInstance) {
        SupplierOfInstance<?> that = (SupplierOfInstance<?>) obj;
        return Objects.equal(that.instance);
      }
      return false;
    }
    @Override public int hashCode() {
      return Objects.hashCode();
    }
    @Override public String toString() {
      return "Suppliers.ofInstance(" +  + ")";
    }
    private static final long serialVersionUID = 0;
  }

  
Returns a supplier whose get() method synchronizes on delegate before calling it, making it thread-safe.
  public static <T> Supplier<T> synchronizedSupplier(Supplier<T> delegate) {
    return new ThreadSafeSupplier<T>(Preconditions.checkNotNull(delegate));
  }
  private static class ThreadSafeSupplier<T>
      implements Supplier<T>, Serializable {
    final Supplier<T> delegate;
    ThreadSafeSupplier(Supplier<T> delegate) {
      this. = delegate;
    }
    @Override public T get() {
      synchronized () {
        return .get();
      }
    }
    @Override public String toString() {
      return "Suppliers.synchronizedSupplier(" +  + ")";
    }
    private static final long serialVersionUID = 0;
  }

  
Returns a function that accepts a supplier and returns the result of invoking Supplier.get on that supplier.

Since:
8.0
  @Beta
  //SupplierFunction works for any T.
  @SuppressWarnings({"unchecked""rawtypes"})
  public static <T> Function<Supplier<T>, T> supplierFunction() {
  }
  private enum SupplierFunction implements Function<Supplier<?>, Object> {
    INSTANCE;
    @Override public Object apply(Supplier<?> input) {
      return input.get();
    }
    @Override public String toString() {
      return "Suppliers.supplierFunction()";
    }
  }
New to GrepCode? Check out our FAQ X