Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: marauroa/game/RPActionFactory.java


1   /* $Id: RPActionFactory.java,v 1.10 2003/12/12 21:41:50 root777 Exp $ */
2   /***************************************************************************
3    *                      (C) Copyright 2003 - Marauroa                      *
4    ***************************************************************************
5    ***************************************************************************
6    *                                                                         *
7    *   This program is free software; you can redistribute it and/or modify  *
8    *   it under the terms of the GNU General Public License as published by  *
9    *   the Free Software Foundation; either version 2 of the License, or     *
10   *   (at your option) any later version.                                   *
11   *                                                                         *
12   ***************************************************************************/
13  package marauroa.game;
14  
15  import java.io.IOException;
16  import java.util.HashMap;
17  import java.util.Map;
18  import marauroa.marauroad;
19  import marauroa.net.InputSerializer;
20  import marauroa.net.OutputSerializer;
21  
22  /** This class is used to creating a RPAction object from a stream or to serialize
23   *  a RPAction object to a stream. */
24  public class RPActionFactory
25  {
26    private static Map factoryArray;
27    private static RPActionFactory rpActionFactory;
28    
29    private RPActionFactory()
30    {
31      factoryArray= new HashMap();
32    }
33    
34    /** This method returns an instance of RPObjectFactory
35     *  @return A shared instance of RPObjectFactory */
36    public static RPActionFactory getFactory()
37    {
38      if(rpActionFactory==null)
39      {
40        rpActionFactory=new RPActionFactory();
41        rpActionFactory.register(0,RPAction.class);
42      }
43      
44      return rpActionFactory;
45    }
46    
47    /** registers a RPAction class
48     @param index the RPAction type
49     @param rpActionClass the RPAction class
50     **/
51    public void register(int index,Class rpActionClass)
52    {
53      factoryArray.put(new Integer(index),rpActionClass);
54    }
55    
56    /** Returns a object of the right class from a stream of serialized data.
57     @param is InputSerializer
58     @throws IOException in case of problems with the RPAction */
59    public RPAction getRPAction(InputSerializer is) throws IOException
60    {
61      marauroad.trace("RPActionFactory::getRPAction",">");
62      try
63      {
64        int index = is.readInt();
65        Class rpActionType=(Class) factoryArray.get(new Integer(index));
66  //      marauroad.trace("RPActionFactory::getRPAction","D","index is " +index + ", class is "+rpActionType);
67        if(rpActionType!=null)
68        {
69          RPAction tmp=(RPAction) rpActionType.newInstance();
70          tmp.readObject(is);
71          return tmp;
72        }
73        else
74        {
75          marauroad.trace("RPActionFactory::getRPAction","X","RPAction type ["+index+"] is not registered.");
76          throw new IOException("RPAction type ["+index+"] is not registered.");
77        }
78      }
79      catch(Exception e)
80      {
81        marauroad.trace("RPActionFactory::getRPAction","X",e.getMessage());
82        throw new IOException(e.getMessage());
83      }
84      finally
85      {
86        marauroad.trace("RPActionFactory::getRPAction","<");
87      }
88    }
89    
90    /**
91     * adds the given RPAction into serializer
92     */
93    public void addRPAction(OutputSerializer os,RPAction rp_action) throws IOException
94    {
95      marauroad.trace("RPActionFactory::addRPAction",">");
96      try
97      {
98        int index = rp_action.actionType;
99  //      marauroad.trace("RPActionFactory::addRPAction","D","index is " +index + ", class is "+rp_action.getClass().getName());
100       os.write(index);
101       rp_action.writeObject(os);
102     }
103     catch(Exception e)
104     {
105       marauroad.trace("RPActionFactory::addRPAction","X",e.getMessage());
106       throw new IOException(e.getMessage());
107     }
108     finally
109     {
110       marauroad.trace("RPActionFactory::addRPAction","<");
111     }
112   }
113 }
114