HaniiPuppy

Exception problem solution

Mar 25th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.27 KB | None | 0 0
  1.     public static class MovePlayersToGameException extends Exception
  2.     {
  3.         public MovePlayersToGameException(Collection<? extends PlayerAlreadyInGameException> alreadyInGame,
  4.                                           Collection<? extends PlayerNotOnlineException> notOnline)
  5.         {
  6.             super();
  7.             alreadyInGameExceptions = (Collection<PlayerAlreadyInGameException>)alreadyInGame;
  8.             notOnlineExceptions = (Collection<PlayerNotOnlineException>)notOnline;
  9.         }
  10.  
  11.         public MovePlayersToGameException(Collection<? extends PlayerAlreadyInGameException> alreadyInGame,
  12.                                           Collection<? extends PlayerNotOnlineException> notOnline,
  13.                                           String message)
  14.         {
  15.             super(message);
  16.             alreadyInGameExceptions = (Collection<PlayerAlreadyInGameException>)alreadyInGame;
  17.             notOnlineExceptions = (Collection<PlayerNotOnlineException>)notOnline;
  18.         }
  19.  
  20.         public MovePlayersToGameException(Collection<? extends PlayerAlreadyInGameException> alreadyInGame,
  21.                                           Collection<? extends PlayerNotOnlineException> notOnline,
  22.                                           String message,
  23.                                           Throwable cause)
  24.         {
  25.             super(message, cause);
  26.             alreadyInGameExceptions = (Collection<PlayerAlreadyInGameException>)alreadyInGame;
  27.             notOnlineExceptions = (Collection<PlayerNotOnlineException>)notOnline;
  28.         }
  29.        
  30.         public MovePlayersToGameException(Collection<? extends PlayerAlreadyInGameException> alreadyInGame,
  31.                                           Collection<? extends PlayerNotOnlineException> notOnline,
  32.                                           Throwable cause)
  33.         {
  34.             super(cause);
  35.             alreadyInGameExceptions = (Collection<PlayerAlreadyInGameException>)alreadyInGame;
  36.             notOnlineExceptions = (Collection<PlayerNotOnlineException>)notOnline;
  37.         }
  38.        
  39.         Collection<PlayerAlreadyInGameException> alreadyInGameExceptions;
  40.         Collection<PlayerNotOnlineException> notOnlineExceptions;
  41.        
  42.         public Collection<PlayerAlreadyInGameException> getPlayerAlreadyInGameExceptions()
  43.         { return new ArrayList<PlayerAlreadyInGameException>(alreadyInGameExceptions); }
  44.        
  45.         public Collection<PlayerNotOnlineException> getPlayerNotOnlineExceptions()
  46.         { return new ArrayList<PlayerNotOnlineException>(notOnlineExceptions); }
  47.     }
  48.  
  49. ---------- Later down in the class -------------
  50.  
  51.     public boolean movePlayerToGame(Player player) throws PlayerNotOnlineException, PlayerAlreadyInGameException, GameNotInProgressException
  52.     {
  53.         if(!this.isInGame())
  54.             throw new GameNotInProgressException();
  55.        
  56.         if(!player.isOnline())
  57.             throw new PlayerNotOnlineException(player, "Cannot move player to game because player is not online.");
  58.        
  59.         synchronized(busyPlayersInGame)
  60.         {
  61.             if(this.playersInGame.contains(player))
  62.                 throw new PlayerAlreadyInGameException(player, "Player is already in game.");
  63.            
  64.             this.playersInGame.add(player);
  65.         }
  66.        
  67.         synchronized(busyPlayersInLobby)
  68.         { this.playersInLobby.remove(player); }
  69.        
  70.         this.currentStage.tpPlayersToStartingPositions(player);
  71.         return true;
  72.     }
  73.    
  74.     public Collection<Player> movePlayersToGame(Collection<? extends Player> players) throws GameNotInProgressException, MovePlayersToGameException
  75.     {
  76.         if(!this.isInGame())
  77.             throw new GameNotInProgressException();
  78.        
  79.         Collection<Player> returnVals = new ArrayList<Player>();
  80.         Collection<PlayerNotOnlineException> notOnlineExceptions = new ArrayList<PlayerNotOnlineException>();
  81.         Collection<PlayerAlreadyInGameException> alreadyInGameExceptions = new ArrayList<PlayerAlreadyInGameException>();
  82.        
  83.         for(Player player : players)
  84.         {
  85.             try
  86.             {
  87.                 if(!player.isOnline())
  88.                     throw new PlayerNotOnlineException(player, "Cannot move player to game because player is not online.");
  89.                
  90.                 synchronized(busyPlayersInGame)
  91.                 {
  92.                     if(this.playersInGame.contains(player))
  93.                         throw new PlayerAlreadyInGameException(player, "Player is already in game.");
  94.  
  95.                     this.playersInGame.add(player);
  96.                 }
  97.                
  98.                 synchronized(busyPlayersInLobby)
  99.                 { this.playersInLobby.remove(player); }
  100.                
  101.                 returnVals.add(player);
  102.             }
  103.             catch(PlayerNotOnlineException e)
  104.             { notOnlineExceptions.add(e); }
  105.             catch(PlayerAlreadyInGameException e)
  106.             { alreadyInGameExceptions.add(e); }
  107.         }
  108.        
  109.         this.currentStage.tpPlayersToStartingPositions(returnVals);
  110.        
  111.         if((!notOnlineExceptions.isEmpty()) || (!alreadyInGameExceptions.isEmpty()))
  112.             throw new MovePlayersToGameException(alreadyInGameExceptions, notOnlineExceptions);
  113.        
  114.         return returnVals;
  115.     }
Advertisement
Add Comment
Please, Sign In to add comment