| Start/ | End/ | |||
| True | False | - | Line | Source |
| 1 | /* | |||
| 2 | * [ JIGA ] | |||
| 3 | * | |||
| 4 | * Copyright (c) 2003 Glenn Sanson <glenn.sanson at free.fr> | |||
| 5 | * | |||
| 6 | * This code is distributed under the GNU Library General Public License | |||
| 7 | * | |||
| 8 | * This library is free software; you can redistribute it and/or | |||
| 9 | * modify it under the terms of the GNU Library General Public License | |||
| 10 | * as published by the Free Software Foundation; either version 2 of the | |||
| 11 | * License, or (at your option) any later version. | |||
| 12 | * | |||
| 13 | * This library is distributed in the hope that it will be useful, but | |||
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
| 16 | * Library General Public License for more details. | |||
| 17 | * | |||
| 18 | * You should have received a copy of the GNU Library General Public License | |||
| 19 | * along with this program; if not, write to the Free Software Foundation, Inc., | |||
| 20 | * 675 Mass Ave, Cambridge, MA 02139, USA. | |||
| 21 | * | |||
| 22 | * | |||
| 23 | * [http://glenn.sanson.free.fr/jiga/] | |||
| 24 | */ | |||
| 25 | ||||
| 26 | package net.library.jiga; | |||
| 27 | ||||
| 28 | import java.util.Vector; | |||
| 29 | ||||
| 30 | /** | |||
| 31 | * A convenient way to maintain all objects required by the game, which allows | |||
| 32 | * data exchange between GameScreens | |||
| 33 | * <p>FAQ : Why do I use <code>Vector</code> to simulate a <code>HashMap</code>? | |||
| 34 | * <p>A : Because <code>HashMap</code> was implemented in Java release 1.2 and JIGA is 1.1 compatible | |||
| 35 | * <p>Q : Why didn't I use <code>Hashtable</code> instead? | |||
| 36 | * <p>A : Because of a bug in the implementation of <code>Hashtable</code> on some old VMs | |||
| 37 | * (N.B.: <code>GameContext</code> acts like a <code>HashMap</code> : | |||
| 38 | * <ul> | |||
| 39 | * <li>this is not a synchronized object -<code>Vector</code> is synchronized, but not <code>GameContext</code>- | |||
| 40 | * <li><code>null</code> values are allowed | |||
| 41 | * </ul> | |||
| 42 | * @author Glenn Sanson | |||
| 43 | */ | |||
| 44 | public class GameContext { | |||
| 45 | private Vector dataList; | |||
| 46 | ||||
| 47 | private Vector keyList; | |||
| 48 | ||||
| 49 | /** | |||
| 50 | * Initialisation of the pseudo HashMap | |||
| 51 | * | |||
| 52 | */ | |||
| 95 | 0 | 53 | public GameContext() { | |
| 54 | dataList = new Vector(); | |||
| 55 | keyList = new Vector(); | |||
| 56 | } | |||
| 57 | ||||
| 58 | /** | |||
| 59 | * Adds an objet | |||
| 60 | * @param key The key associated with the object to add | |||
| 61 | * @param object The object to add | |||
| 62 | */ | |||
| 4750 | 0 | 63 | public final void addObject(String key, Object object) { | |
| 64 | int index = keyList.indexOf(key); | |||
| 65 | ||||
| 4750 | 0 | - | 66 | if (index == -1) { |
| 67 | keyList.addElement(key); | |||
| 68 | dataList.addElement(object); | |||
| 69 | } else { | |||
| 70 | dataList.setElementAt(object, index); | |||
| 71 | } | |||
| 72 | } | |||
| 73 | ||||
| 74 | /** | |||
| 75 | * Removes an objet | |||
| 76 | * @param key The key associated with the object to remove | |||
| 77 | */ | |||
| 0 | 0 | - | 78 | public final void removeObject(String key) { |
| 79 | int index = keyList.indexOf(key); | |||
| 80 | ||||
| 0 | 0 | - | 81 | if (index != -1) { |
| 82 | keyList.removeElementAt(index); | |||
| 83 | dataList.removeElementAt(index); | |||
| 84 | } | |||
| 85 | } | |||
| 86 | ||||
| 87 | /** | |||
| 88 | * Gets an objet | |||
| 89 | * @param key The key associated with the object to get | |||
| 90 | * @return The searched object, or <code>null</code> if the key was not found | |||
| 91 | */ | |||
| 21007 | 0 | 92 | public final Object getObject(String key) { | |
| 93 | int index = keyList.indexOf(key); | |||
| 94 | ||||
| 16732 | 4275 | 95 | if (index != -1) { | |
| 16732 | 96 | return dataList.elementAt(index); | ||
| 97 | } | |||
| 98 | ||||
| 4275 | 99 | return null; | ||
| 100 | } | |||
| 101 | } | |||
| ***TER 67% (8/12) of SOURCE FILE GameContext.java | ||||