private <T extends SingularityMachineAbstraction<T>> CheckResult check(
      T object, AbstractMachineManager<T> manager) {
    Optional<T> existingObject = manager.getObject(object.getId());

    if (!existingObject.isPresent()) {
      manager.saveObject(object);

      return CheckResult.NEW;
    }

    MachineState currentState = existingObject.get().getCurrentState().getState();

    switch (currentState) {
      case ACTIVE:
      case FROZEN:
        return CheckResult.ALREADY_ACTIVE;
      case DEAD:
      case MISSING_ON_STARTUP:
        manager.changeState(object.getId(), MachineState.ACTIVE, Optional.<String>absent());
        return CheckResult.NEW;
      case DECOMMISSIONED:
      case DECOMMISSIONING:
      case STARTING_DECOMMISSION:
        return CheckResult.DECOMMISSIONING;
    }

    throw new IllegalStateException(
        String.format("Invalid state %s for %s", currentState, object.getId()));
  }
 private <T extends SingularityMachineAbstraction<T>> void changeState(
     Map<T, MachineState> map, AbstractMachineManager<T> manager) {
   for (Entry<T, MachineState> entry : map.entrySet()) {
     manager.changeState(
         entry.getKey().getId(), entry.getValue(), entry.getKey().getCurrentState().getUser());
   }
 }