/** * Convenience method that calls * {@link ManagedLocks#weakManagedLockFactory(Function)} using the * {@link Functions#identity() identity function} for striping, essentially * meaning that unique input will have its own lock. * * @param <T> the type of the thing used to look up locks * @see #weakManagedLockFactory(Function, Supplier) */ public static @NotNull <T> com.atlassian.util.concurrent.Function<T, ManagedLock> weakManagedLockFactory() { return weakManagedLockFactory(Functions.<T> identity()); }
/** * Create a new {@link CopyOnWriteMap} with the supplied {@link Map} to * initialize the values. * * @param map the initial map to initialize with * @param viewType for writable or read-only key, value and entrySet views */ protected <N extends Map<? extends K, ? extends V>> AbstractCopyOnWriteMap(final N map, final View.Type viewType) { this.delegate = notNull("delegate", copy(notNull("map", map))); this.view = notNull("viewType", viewType).get(this); }
/** * Set the value of this reference. This method is lock-free. A thread waiting * in {@link #take()} or {@link #take(long, TimeUnit)} will be released and * given this value. * * @param value the new value. */ public void set(@NotNull final V value) { notNull("value", value); internalSet(value); }
/** * Get a {@link ThreadFactory} with the required name prefix and type (user or * daemon). The produced threads have normal priority. * * @param name the prefix to use for naming the threads. * @param type whether they are User or Daemon threads. * @return a configured {@link ThreadFactory} */ public static ThreadFactory namedThreadFactory(@NotNull final String name, @NotNull final Type type) { return named(name).type(type).build(); }
/** * A {@link Supplier} of {@link ManagedLock managed locks}. * * @return lock factory */ static @NotNull com.atlassian.util.concurrent.Supplier<ManagedLock> managedLockFactory(final @NotNull Supplier<Lock> supplier) { notNull("supplier", supplier); return new com.atlassian.util.concurrent.Supplier<ManagedLock>() { public ManagedLock get() { return new ManagedLockImpl(supplier.get()); } }; }
/** * Get a {@link ThreadFactory} with the required name prefix. The produced * threads are user threads and have normal priority. * * @param name the prefix to use for naming the threads. * @return a configured {@link ThreadFactory} */ public static ThreadFactory namedThreadFactory(@NotNull final String name) { return named(name).build(); }
/** * Get a {@link ManagedLock} that manages the supplied {@link Lock}. * * @param lock the lock to use. * @return a managed lock */ public static @NotNull ManagedLock manage(final @NotNull Lock lock) { return new ManagedLockImpl(lock); }
/** * Get a {@link ManagedLock.ReadWrite} that manages the supplied * {@link ReadWriteLock}. * * @param lock the lock to use. * @return a managed read write lock */ public static @NotNull ManagedLock.ReadWrite manageReadWrite(final @NotNull ReadWriteLock lock) { return new ReadWriteManagedLock(lock); }
/** * Get a function that uses the Supplier as a factory for all inputs. * * @param <D> the key type, ignored * @param <R> the result type * @param supplier called for all inputs * @return the function */ public static <D, R> Function<D, R> fromSupplier(final @NotNull Supplier<R> supplier) { return new FromSupplier<D, R>(supplier); }
void await(final Awaitable waitable) throws TimeoutException, InterruptedException { if (!waitable.await(getTime(), getUnit())) { throwTimeoutException(); } }
/** * Create a {@link FutureCallback} from an Effect to be run if there is a * success. * * @param effect To be passed the produced value if it happens * @return The FutureCallback with a no-op onFailure */ public static <A> FutureCallback<A> onSuccessDo(final Effect<? super A> effect) { return futureCallback(effect, Effects.<Throwable> noop()); }
/** * Creates a new {@link CopyOnWriteMap} with an underlying * {@link LinkedHashMap} using the supplied map as the initial values. * Iterators for this map will be return elements in insertion order. * <p> * This map has {@link View.Type.STABLE stable} views. */ public static <K, V> CopyOnWriteMap<K, V> newLinkedMap(final Map<? extends K, ? extends V> map) { final Builder<K, V> builder = builder(); return builder.addAll(map).newLinkedMap(); }
/** * Creates a new {@link CopyOnWriteMap} with an underlying {@link HashMap} * using the supplied map as the initial values. * <p> * This map has {@link View.Type.STABLE stable} views. */ public static <K, V> CopyOnWriteMap<K, V> newHashMap(final Map<? extends K, ? extends V> map) { final Builder<K, V> builder = builder(); return builder.addAll(map).newHashMap(); }
/** * Create a new {@link CopyOnWriteSortedMap} where the underlying map * instances are {@link TreeMap} and the sort uses the key's natural order. * <p> * This map has {@link View.Type.STABLE stable} views. */ public static <K, V> CopyOnWriteSortedMap<K, V> newTreeMap() { final Builder<K, V> builder = builder(); return builder.newTreeMap(); }
/** * Creates a new {@link CopyOnWriteMap} with an underlying {@link HashMap}. * <p> * This map has {@link View.Type.STABLE stable} views. */ public static <K, V> CopyOnWriteMap<K, V> newHashMap() { final Builder<K, V> builder = builder(); return builder.newHashMap(); }
/** * Create a BlockingReference best suited to single-reader/single-writer * situations. In a MRSW case this instance may get missed signals if multiple * reader threads are all waiting on the value. * * @param initialValue the initial value */ public static <V> BlockingReference<V> newSRSW(final V initialValue) { return new BlockingReference<V>(new BooleanLatch(), initialValue); }
/** * Creates a new {@link CopyOnWriteMap} with an underlying * {@link LinkedHashMap}. Iterators for this map will be return elements in * insertion order. * <p> * This map has {@link View.Type.STABLE stable} views. */ public static <K, V> CopyOnWriteMap<K, V> newLinkedMap() { final Builder<K, V> builder = builder(); return builder.newLinkedMap(); }
/** * Get a {@link ThreadFactory} with the required name prefix, type and * priority. * * @param name the prefix to use for naming the threads. * @param type whether they are User or Daemon threads. * @param priority the thread priority, must not be lower than * {@link Thread#MIN_PRIORITY} or greater than {@link Thread#MAX_PRIORITY} * @return a configured {@link ThreadFactory} */ public static ThreadFactory namedThreadFactory(@NotNull final String name, @NotNull final Type type, final int priority) { return named(name).type(type).priority(priority).build(); }
/** * Create a {@link FutureCallback} from an Effect to be run if there is a * failure. * * @param effect To be passed an exception if it happens * @return The FutureCallback with a no-op onSuccess */ public static <A> FutureCallback<A> onFailureDo(final Effect<Throwable> effect) { return futureCallback(Effects.<A> noop(), effect); }