MarkerCache.java :  » Graphic-Library » apollo » org » apollo » trackmodel » Java Open Source

Java Open Source » Graphic Library » apollo 
apollo » org » apollo » trackmodel » MarkerCache.java
/*
 * Apollo - Motion capture and animation system
 * Copyright (c) 2005 Apollo
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @author Giovane.Kuhn - brain@netuno.com.br
 *
 */
package org.apollo.trackmodel;

import java.awt.Color;
import java.util.HashMap;
import java.util.Map;

import org.apollo.datamodel.EffectingFrame;
import org.apollo.datamodel.EffectingVideo;
import org.apollo.datamodel.Marker;

/**
 * Class to maintain a cache for all markers in effecting video
 * @author Giovane.Kuhn on 29/05/2005
 */
public final class MarkerCache {

    /** Effecting video to cache markers */
    private final EffectingVideo ev;

    /** Cache of markers, for each frame */
    private Map<Integer, Map<Color, Marker>> cache;

    public MarkerCache(EffectingVideo ev) {
        this.ev = ev;
    }

    private Map<Integer, Map<Color, Marker>> getCache() {
        if (cache == null) {
            cache = new HashMap<Integer, Map<Color, Marker>>();
        }
        return cache;
    }

    /**
     * Refresh whole cache
     */
    public void refresh() {
        for (EffectingFrame f : ev.getFrames().values()) {
            refresh(f.getSequence());
        }
    }

    /**
     * Refresh cache for specified frame 
     * @param frame Frame to be refreshed
     */
    public void refresh(int frame) {
        cache = getCache();

        // get video frame
        EffectingFrame f = ev.getFrame(frame);
        if (f == null) {
            assert cache.get(frame) == null;
            return;
        }

        Map<Color, Marker> map = new HashMap<Color, Marker>();
        // cache only identified markers
        for (Marker m : f.getMarkers()) {
            if (m.getColor() == null) {
                continue;
            }
            assert !map.containsKey(m.getColor());
            map.put(m.getColor(), m);
        }
        cache.put(frame, map);
    }

    /**
     * Get a marker at a specified frame
     * @param frame Frame to find the marker
     * @param color Marker color
     * @return Marker found, otherwise <code>null</code> not found
     */
    public Marker getMarker(int frame, Color color) {
        if (cache == null) {
            refresh();
        }
        cache = getCache();
        Map<Color, Marker> map = cache.get(frame);
        if (map == null) {
            return null;
        }
        return map.get(color);
    }

    /**
     * Put a new marker at specified frame.
     * Cannot already exists a marker with the same color
     * @param frame Frame to put new marler
     * @param m Marker to be inserted
     */
    public void putMarker(int frame, Marker m) {
        if (cache == null) {
            refresh();
        }
        Map<Color, Marker> map = cache.get(frame);
        if (map == null) {
            map = new HashMap<Color, Marker>();
            cache.put(frame, map);
        }
        assert m.getColor() != null;
        assert !map.containsKey(m.getColor());
        map.put(m.getColor(), m);
    }
}
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.