001: /*
002: * $Id: LuaStateFactory.java,v 1.4 2006/12/22 14:06:40 thiago Exp $
003: * Copyright (C) 2003-2007 Kepler Project.
004: *
005: * Permission is hereby granted, free of charge, to any person obtaining
006: * a copy of this software and associated documentation files (the
007: * "Software"), to deal in the Software without restriction, including
008: * without limitation the rights to use, copy, modify, merge, publish,
009: * distribute, sublicense, and/or sell copies of the Software, and to
010: * permit persons to whom the Software is furnished to do so, subject to
011: * the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be
014: * included in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
017: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
021: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
022: * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024:
025: package org.keplerproject.luajava;
026:
027: import java.util.ArrayList;
028: import java.util.List;
029:
030: /**
031: * This class is responsible for instantiating new LuaStates.
032: * When a new LuaState is instantiated it is put into a List
033: * and an index is returned. This index is registred in Lua
034: * and it is used to find the right LuaState when lua calls
035: * a Java Function.
036: *
037: * @author Thiago Ponte
038: */
039: public final class LuaStateFactory {
040: /**
041: * Array with all luaState's instances
042: */
043: private static final List states = new ArrayList();
044:
045: /**
046: * Non-public constructor.
047: */
048: private LuaStateFactory() {
049: }
050:
051: /**
052: * Method that creates a new instance of LuaState
053: * @return LuaState
054: */
055: public synchronized static LuaState newLuaState() {
056: int i = getNextStateIndex();
057: LuaState L = new LuaState(i);
058:
059: states.add(i, L);
060:
061: return L;
062: }
063:
064: /**
065: * Returns a existing instance of LuaState
066: * @param index
067: * @return LuaState
068: */
069: public synchronized static LuaState getExistingState(int index) {
070: return (LuaState) states.get(index);
071: }
072:
073: /**
074: * Receives a existing LuaState and checks if it exists in the states list.
075: * If it doesn't exist adds it to the list.
076: * @param L
077: * @return int
078: */
079: public synchronized static int insertLuaState(LuaState L) {
080: int i;
081: for (i = 0; i < states.size(); i++) {
082: LuaState state = (LuaState) states.get(i);
083:
084: if (state != null) {
085: if (state.getCPtrPeer() == L.getCPtrPeer())
086: return i;
087: }
088: }
089:
090: i = getNextStateIndex();
091:
092: states.set(i, L);
093:
094: return i;
095: }
096:
097: /**
098: * removes the luaState from the states list
099: * @param idx
100: */
101: public synchronized static void removeLuaState(int idx) {
102: states.add(idx, null);
103: }
104:
105: /**
106: * Get next available index
107: * @return int
108: */
109: private synchronized static int getNextStateIndex() {
110: int i;
111: for (i = 0; i < states.size() && states.get(i) != null; i++)
112: ;
113:
114: return i;
115: }
116: }
|