Code Monkey home page Code Monkey logo

Comments (2)

ciskeboekelo avatar ciskeboekelo commented on June 16, 2024

I would appreciate this functionality too.

Workaround: Create copies of Reservoir.java and SimpleDiskCache.java in your own project and add the mentioned methods. (You don't need to modify SimpleDiskCache for this, but that class is not public and your copy of Reservoir needs it).

I've already done this, but I could create a PR for this.

    /**
     * Get an object from Reservoir with the given key. This a blocking IO operation.
     *
     * @param key      the key string.
     * @param type the Type of the expected return object.
     * @return the object of the given type if it exists, otherwise <code>null</code>.
     * @throws java.lang.NullPointerException if key does not exist or java.lang.IOException if reading from file fails.
     */
    public static <T> T getByType(String key, Type type) throws Exception {
        String json = cache.getString(key).getString();
        return new Gson().fromJson(json, type);
    }


    /**
     * Get an object from Reservoir with the given key asynchronously.
     *
     * @param key      the key string.
     * @param type the Type of the expected return object.
     * @param callback a callback of type {@link com.anupcowkur.reservoir.ReservoirGetCallback}
     *                 which is called upon completion.
     */
    public static <T> void getAsyncByType(String key, Type type, ReservoirGetCallback<T> callback) {
        new GetByKeyAndTypeTask<>(key, type, callback).execute();
    }


    /**
     * AsyncTask to perform get operation in a background thread.
     */
    private static class GetByKeyAndTypeTask<T> extends AsyncTask<Void, Void, T> {

        private final String key;
        private final ReservoirGetCallback<T> callback;
        private final Type type;
        private Exception e;

        private GetByKeyAndTypeTask(String key, Type type, ReservoirGetCallback<T> callback) {
            this.key = key;
            this.callback = callback;
            this.type = type;
            this.e = null;
        }

        @Override
        protected T doInBackground(Void... params) {
            try {
                String json = cache.getString(key).getString();
                T value = new Gson().fromJson(json, type);
                if (value == null)
                    throw new NullPointerException();
                return value;
            } catch (Exception e) {
                this.e = e;
                return null;
            }
        }

        @Override
        protected void onPostExecute(T object) {
            if (callback != null) {
                if (e == null) {
                    callback.onSuccess(object);
                } else {
                    callback.onFailure(e);
                }
            }
        }
    }

(I haven't created versions for RxJava)

from reservoir.

robertoestivill avatar robertoestivill commented on June 16, 2024

@ciskeboekelo Yes, I guess that will work too, but as you said, is far from ideal.

Thanks for sharing your solution!

from reservoir.

Related Issues (20)

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.