Skip to content

Latest commit

 

History

History
210 lines (166 loc) · 8.82 KB

README.md

File metadata and controls

210 lines (166 loc) · 8.82 KB

memoization.java Chat

Java memoization library - trade space for time

Features

  • Memoize calls to JDK interfaces like Consumer, Function, Predicate, Supplier, and more
  • Memoize calls to jOOL interfaces like Consumer0..16 and Function0..16
  • Memoize calls to lambda interfaces like Fn0..8
  • Memoize calls to RxJava interfaces like Action, Cancellable, and more
  • Use custom caches like Caffeine, Guava, cache2k, or any ConcurrentMap.
  • Use custom cache keys for fine-tuning

Usage

Memoize any of the supported types by using the static factory methods supplied by:

  • Memoize if you want to memoize JDK interfaces.
  • MemoizeJool if you want to memoize jOOL interfaces.
  • MemoizeLambda if you want to memoize lambda interfaces.
  • MemoizeRx if you want to memoize RxJava interfaces.

Default cache with default cache keys

wtf.metio.memoization.jdk.Memoize;
wtf.metio.memoization.jool.MemoizeJool;
wtf.metio.memoization.lambda.MemoizeLambda;
wtf.metio.memoization.rxjava.MemoizeRx;

Function<INPUT, OUTPUT> function         = ...;
Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function);

Supplier<OUTPUT> supplier                = ...;
Supplier<OUTPUT> memoizedSupplier        = MemoizeRx.supplier(supplier);

Function3<T1, T2, T3, OUTPUT> function         = ...;
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function);

Fn4<T1, T2, T3, T4, OUTPUT> function         = ...;
Fn4<T1, T2, T3, T4, OUTPUT> memoizedFunction = MemoizeLambda.fn4(function);

Default cache with custom cache keys

wtf.metio.memoization.jdk.Memoize;
wtf.metio.memoization.jool.MemoizeJool;
wtf.metio.memoization.lambda.MemoizeLambda;
wtf.metio.memoization.rxjava.MemoizeRx;

Function<INPUT, OUTPUT> function         = ...;
Function<INPUT, KEY> keyFunction         = ...;
Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function, keyFunction);

Supplier<OUTPUT> supplier                = ...;
Supplier<KEY> keySupplier                = ...;
Supplier<OUTPUT> memoizedSupplier        = MemoizeRx.supplier(supplier, keySupplier);

Function3<T1, T2, T3, OUTPUT> function         = ...;
Function3<T1, T2, T3, KEY> keyFunction         = ...;
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function, keyFunction);

Fn4<T1, T2, T3, T4, OUTPUT> function         = ...;
Fn4<T1, T2, T3, T4, KEY> keyFunction         = ...;
Fn4<T1, T2, T3, T4, OUTPUT> memoizedFunction = MemoizeLambda.fn4(function, keyFunction);

Custom cache with default cache keys

wtf.metio.memoization.jdk.Memoize;
wtf.metio.memoization.jool.MemoizeJool;
wtf.metio.memoization.lambda.MemoizeLambda;
wtf.metio.memoization.rxjava.MemoizeRx;

// memoize in cache2k cache
Function<INPUT, OUTPUT> function         = ...;
Cache<INPUT, OUTPUT> cache               = ...; // org.cache2k.Cache
Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function, cache.asMap());

// memoize in Caffeine cache
Supplier<OUTPUT> supplier                = ...;
Cache<Integer, OUTPUT> cache             = ...; // com.github.benmanes.caffeine.cache.Cache
Supplier<OUTPUT> memoizedSupplier        = MemoizeRx.supplier(supplier, cache.asMap());

// memoize in Guava cache
Function3<T1, T2, T3, OUTPUT> function         = ...;
Cache<Integer, OUTPUT> cache                   = ...; // com.google.common.cache.Cache
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function, cache.asMap());

// memoize in ConcurrentMap
Fn4<T1, T2, T3, T4, OUTPUT> function         = ...;
Map<Integer, OUTPUT> cache                   = ...;
Fn4<T1, T2, T3, T4, OUTPUT> memoizedFunction = MemoizeLambda.fn4(function, cache);

Custom cache with custom cache keys

wtf.metio.memoization.jdk.Memoize;
wtf.metio.memoization.jool.MemoizeJool;
wtf.metio.memoization.lambda.MemoizeLambda;
wtf.metio.memoization.rxjava.MemoizeRx;

// memoize in cache2k cache
Function<INPUT, OUTPUT> function         = ...;
Function<INPUT, KEY> keyFunction         = ...;
Cache<KEY, OUTPUT> cache                 = ...; // org.cache2k.Cache
Function<INPUT, OUTPUT> memoizedFunction = Memoize.function(function, keyFunction, cache.asMap());

// memoize in Caffeine cache
Supplier<OUTPUT> supplier                = ...;
Supplier<KEY> keySupplier                = ...;
Cache<KEY, OUTPUT> cache                 = ...; // com.github.benmanes.caffeine.cache.Cache
Supplier<OUTPUT> memoizedSupplier        = MemoizeRx.supplier(supplier, keySupplier, cache.asMap());

// memoize in Guava cache
Function3<T1, T2, T3, OUTPUT> function         = ...;
Function3<T1, T2, T3, KEY> keyFunction         = ...;
Cache<KEY, OUTPUT> cache                       = ...; // com.google.common.cache.Cache
Function3<T1, T2, T3, OUTPUT> memoizedFunction = MemoizeJool.function3(function, keyFunction, cache.asMap());

// memoize in ConcurrentMap
Fn4<T1, T2, T3, T4, OUTPUT> function         = ...;
Fn4<T1, T2, T3, T4, KEY> keyFunction         = ...;
Map<KEY, OUTPUT> cache                       = ...;
Fn4<T1, T2, T3, T4, OUTPUT> memoizedFunction = MemoizeLambda.fn4(function, keyFunction, cache);

Note that the static factory methods do accept any Map, however they copy the entries in the map to a new ConcurrentHashMap in case the provided Map is not a ConcurrentMap. This is done in order to ensure atomic computeIfAbsent behavior.

Integration

In order to use this project, declare the following dependencies in your project:

<dependencies>
    <!-- support for JDK interfaces -->
    <dependency>
        <groupId>wtf.metio.memoization</groupId>
        <artifactId>memoization-jdk</artifactId>
        <version>${version.memoization}</version>
    </dependency>
    <!-- support for JDK interfaces -->

    <!-- support for jOOL interfaces -->
    <dependency>
        <groupId>wtf.metio.memoization</groupId>
        <artifactId>memoization-jool</artifactId>
        <version>${version.memoization}</version>
    </dependency>
    <!-- support for jOOL interfaces -->

    <!-- support for lambda interfaces -->
    <dependency>
        <groupId>wtf.metio.memoization</groupId>
        <artifactId>memoization-lambda</artifactId>
        <version>${version.memoization}</version>
    </dependency>
    <!-- support for lambda interfaces -->

    <!-- support for RxJava interfaces -->
    <dependency>
        <groupId>wtf.metio.memoization</groupId>
        <artifactId>memoization-rxjava</artifactId>
        <version>${version.memoization}</version>
    </dependency>
    <!-- support for RxJava interfaces -->
</dependencies>

Replace ${version.memoization} with the latest release.

Alternatives

License

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.