CachedPictureManager.java :  » Game » easy-game-client » egc » res » Java Open Source

Java Open Source » Game » easy game client 
easy game client » egc » res » CachedPictureManager.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package egc.res;

import easy.egc.common.ArrayKey;
import easy.egc.common.Cache;
import easy.egc.common.CacheWrapper;
import easy.egc.common.Key;
import easy.egc.common.KeyFactory;
import java.awt.Image;
import java.util.ArrayList;

/**
 *
 * @author easy
 */
public class CachedPictureManager implements PictureManager {

    @Override
    public Image getImage(Object key, int index) {
        if (key == null || index < 0) {
            return null;
        }
        Key k = createKey(key, index);
        Image img = (Image) getCacheWrapper().get(k);
        if (img == null) {
            img = mgr.getImage(key, index);
            if (img != null) {
                getCacheWrapper().set(k, img, img.getWidth(null) * img.getHeight(null));
            }
        }
        return img;
    }

    @Override
    public Image[] getImages(Object key) {
        int size = getImageSize(key);
        if (size <= 0) {
            return new Image[0];
        }
        Image[] arr = new Image[size];
        for (int i = 0; i < size; i++) {
            arr[i] = getImage(key, i);
        }
        return arr;
    }

    @Override
    public Image[] getImages() {
        Object[] keys = getImageKeys();
        if (keys == null || keys.length <= 0) {
            return new Image[0];
        }
        ArrayList<Image> list = new ArrayList<Image>();
        for (Object key : keys) {
            if (key != null) {
                Image[] imgs = getImages(key);
                for (Image img : imgs) {
                    list.add(img);
                }
            }
        }
        return list.toArray(new Image[list.size()]);
    }

    @Override
    public int getImageSize(Object key) {
        return mgr.getImageSize(key);
    }

    @Override
    public Object[] getImageKeys() {
        return mgr.getImageKeys();
    }

    @Override
    public int getImageSize() {
        return mgr.getImageSize();
    }

    @Override
    public ResourceManager getResourceManager() {
        return mgr.getResourceManager();
    }

    @Override
    public void clear() {
        mgr.clear();
        wrap.clearAll();
    }

//    public static class Key {
//
//        final Object key;
//        final int index;
//
//        public Key(Object key, int index) {
//            this.key = key;
//            this.index = index;
//        }
//
//        @Override
//        public int hashCode() {
//            int hash = 5;
//            hash = 41 * hash + (this.key != null ? this.key.hashCode() : 0);
//            hash = 41 * hash + this.index;
//            return hash;
//        }
//
//        @Override
//        public boolean equals(Object obj) {
//            if (obj == null) {
//                return false;
//            }
//            if (getClass() != obj.getClass()) {
//                return false;
//            }
//            final Key other = (Key) obj;
//            if (this.key != other.key && (this.key == null || !this.key.equals(other.key))) {
//                return false;
//            }
//            if (this.index != other.index) {
//                return false;
//            }
//            return true;
//        }
//    }
    public static Key createKey(Object key, int idx) {
        return KeyFactory.createKey(key, idx);
    }
    PictureManager mgr;
    CacheWrapper wrap;

    public CachedPictureManager(PictureManager mgr, Cache cache) {
        this.mgr = mgr;
        this.wrap = new CacheWrapper(cache);
    }

    public CachedPictureManager(PictureManager mgr) {
        this.mgr = mgr;
        this.wrap = new CacheWrapper();
    }

    public CacheWrapper getCacheWrapper() {
        return wrap;
    }

    public PictureManager getPictureManager() {
        return mgr;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.