All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nucleus.factory.PresenterStorage Maven / Gradle / Ivy

Go to download

Nucleus is an Android library, which helps to use the Model-View-Presenter pattern

The newest version!
package nucleus.factory;

import java.util.HashMap;

import nucleus.presenter.Presenter;

/**
 * This is the singleton where all presenters are stored.
 */
public enum PresenterStorage {

    INSTANCE;

    private HashMap idToPresenter = new HashMap<>();
    private HashMap presenterToId = new HashMap<>();

    /**
     * Adds a presenter to the storage
     *
     * @param presenter a presenter to add
     */
    public void add(final Presenter presenter) {
        String id = presenter.getClass().getSimpleName() + "/" + System.nanoTime() + "/" + (int)(Math.random() * Integer.MAX_VALUE);
        idToPresenter.put(id, presenter);
        presenterToId.put(presenter, id);
        presenter.addOnDestroyListener(new Presenter.OnDestroyListener() {
            @Override
            public void onDestroy() {
                idToPresenter.remove(presenterToId.remove(presenter));
            }
        });
    }

    /**
     * Returns a presenter by id or null if such presenter does not exist.
     *
     * @param id  id of a presenter that has been received by calling {@link #getId(Presenter)}
     * @param 

a type of presenter * @return a presenter or null */ public

P getPresenter(String id) { //noinspection unchecked return (P)idToPresenter.get(id); } /** * Returns id of a given presenter. * * @param presenter a presenter to get id for. * @return if of the presenter. */ public String getId(Presenter presenter) { return presenterToId.get(presenter); } /** * Removes all presenters from the storage. * Use this method for testing purposes only. */ public void clear() { idToPresenter.clear(); presenterToId.clear(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy