Advertisement
Guest User

CommandInterpreter

a guest
Mar 29th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. package pr0304Barracks.core.factories;
  2.  
  3. import pr0304Barracks.contracts.Executable;
  4. import pr0304Barracks.contracts.Repository;
  5. import pr0304Barracks.contracts.UnitFactory;
  6. import pr0304Barracks.core.commands.Command;
  7.  
  8. import java.lang.reflect.Constructor;
  9. import java.lang.reflect.InvocationTargetException;
  10.  
  11. /**
  12.  * Created by Venelin on 29.3.2017 г..
  13.  */
  14. public class CommandInterpreterImpl implements pr0304Barracks.contracts.CommandInterpreter {
  15.  
  16.     private static final String COMMANDS_PACKAGE_NAME =
  17.             "pr0304Barracks.core.commands.";
  18.     private static final String COMMAND_SUFFIX = "Command";
  19.  
  20.     private Repository repository;
  21.     private UnitFactory unitFactory;
  22.  
  23.     public CommandInterpreterImpl(Repository repository, UnitFactory unitFactory) {
  24.         this.repository = repository;
  25.         this.unitFactory = unitFactory;
  26.     }
  27.  
  28.     @Override
  29.     public Executable interpretCommand(String[] data, String commandName) throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException {
  30.         String commandType = commandName.toUpperCase().charAt(0) + commandName.substring(1) + COMMAND_SUFFIX;
  31.         Class commandClass = Class.forName(COMMANDS_PACKAGE_NAME + commandType);
  32.         Command command = null;
  33.         Constructor[] constructors = commandClass.getDeclaredConstructors();
  34.         for (Constructor constructor : constructors) {
  35.             if (constructor.getParameterCount() == 3) {
  36.                 constructor.setAccessible(true);
  37.                 command = (Command) constructor.newInstance(data, this.repository, this.unitFactory);
  38.                 break;
  39.             }
  40.         }
  41.         return command;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement