GithubHelp home page GithubHelp logo

memoization.java's Introduction

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.

memoization.java's People

Contributors

sebhoss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

memoization.java's Issues

Enable Guava module

The Guava cache offers more control over how memoized values are stored (and evicted)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.