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

org.ggp.base.util.game.GameRepository Maven / Gradle / Ivy

The newest version!
package org.ggp.base.util.game;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Game repositories contain games, and provide two main services: you can
 * query a repository to get a list of available games (by key), and given
 * a key, you can look up the associated Game object.
 *
 * All queries to a game repository are cached, and the caching is handled
 * in this abstract base class. Concrete subclasses will implement the actual
 * behavior required for fetching games from the underlying repositories.
 *
 * @author Sam
 */
public abstract class GameRepository {
    private static final String STANFORD_REPO_URL = "games.ggp.org/stanford";
    private static final String DRESDEN_REPO_URL = "games.ggp.org/dresden";
    private static final String DEFAULT_REPO_URL = "games.ggp.org/base";

    public static GameRepository getDefaultRepository() {
        return new CloudGameRepository(DEFAULT_REPO_URL);
    }

    public static GameRepository getDresdenRepository() {
        return new CloudGameRepository(DRESDEN_REPO_URL);
    }

    public static GameRepository getStanfordRepository() {
        return new CloudGameRepository(STANFORD_REPO_URL);
    }

    public Game getGame(String theKey) {
        if (!theGames.containsKey(theKey)) {
            Game theGame = getUncachedGame(theKey);
            if (theGame != null) {
                theGames.put(theKey, theGame);
            }
        }
        return theGames.get(theKey);
    }

    public Set getGameKeys() {
        if (theGameKeys == null) {
            theGameKeys = getUncachedGameKeys();
        }
        return theGameKeys;
    }

    // Abstract methods, for implementation classes.
    protected abstract Game getUncachedGame(String theKey);
    protected abstract Set getUncachedGameKeys();

    // Cached values, lazily filled.
    private Set theGameKeys;
    private Map theGames = new HashMap();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy