package leigh.mecha.javafx;

import javafx.scene.image.Image;

import java.util.HashMap;
import java.util.Objects;

/**
 * Relatively simple implementation of the {@link ImageBinder} specification.
 */
public class RamImageBinder implements ImageBinder {
    // TODO: How about a LRU cache here instead
    private final HashMap<String, Image> images = new HashMap<>();

    @Override
    public Image getImage(String name) {
        return images.get(name);
    }

    public void putImage(String name, Image image) {
        if (image == null) images.remove(name);
        images.put(name, image);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RamImageBinder that = (RamImageBinder) o;
        return Objects.equals(images, that.images);
    }

    @Override
    public int hashCode() {
        return Objects.hash(images);
    }

    @Override
    public String toString() {
        return "RamImageBinder{" +
                "images=" + images +
                '}';
    }
}
