@Before
  public void setup() throws NoSuchFieldException, IllegalAccessException {
    initMocks(this);

    // injecting Population mock
    Field inj = GAPipeline.class.getDeclaredField("population");
    inj.setAccessible(true);
    inj.set(gaPipeline, population);
    inj.setAccessible(false);

    gaPipeline.setChoosePairsStrategy(choosePairsStrategy);
    gaPipeline.setCrossoverStrategy(crossoverStrategy);
    gaPipeline.setMutationStrategy(mutationStrategy);
    gaPipeline.setSelectionStrategy(selectionStrategy);

    // We add 2 elements to makePairs() return,
    // so crossoverStrategy.crossover() should be invoked 2 times
    Set<Couple> crossoverStub = new HashSet<Couple>();
    crossoverStub.add(Couple.ofCouple(1, 2));
    crossoverStub.add(Couple.ofCouple(1, 3));

    // Init strategies
    when(choosePairsStrategy.choosePairs(Matchers.<Population>any())).thenReturn(crossoverStub);
    when(crossoverStrategy.crossover(Matchers.<net.wintermuse.util.Pair>any()))
        .thenReturn(mock(Pair.class));
    when(mutationStrategy.mutate(any())).thenReturn(mock(Object.class));
    when(selectionStrategy.select(Matchers.<Population>any())).thenReturn(mock(Population.class));
  }
    public synchronized void removeItem(int guid, int qua, int pguid) {
      int i = 0;
      if (perso1.get_GUID() == pguid) i = 1;
      else if (perso2.get_GUID() == pguid) i = 2;
      ok1 = false;
      ok2 = false;

      SocketManager.GAME_SEND_EXCHANGE_OK(
          perso1.get_compte().getGameThread().get_out(), ok1, perso1.get_GUID());
      SocketManager.GAME_SEND_EXCHANGE_OK(
          perso2.get_compte().getGameThread().get_out(), ok1, perso1.get_GUID());
      SocketManager.GAME_SEND_EXCHANGE_OK(
          perso1.get_compte().getGameThread().get_out(), ok2, perso2.get_GUID());
      SocketManager.GAME_SEND_EXCHANGE_OK(
          perso2.get_compte().getGameThread().get_out(), ok2, perso2.get_GUID());

      Objet obj = World.getObjet(guid);
      if (obj == null) return;
      String add = "|" + obj.getTemplate().getID() + "|" + obj.parseStatsString();
      if (i == 1) {
        Couple<Integer, Integer> couple = getCoupleInList(items1, guid);
        int newQua = couple.second - qua;
        if (newQua < 1) // Si il n'y a pu d'item
        {
          items1.remove(couple);
          SocketManager.GAME_SEND_EXCHANGE_MOVE_OK(perso1, 'O', "-", "" + guid);
          SocketManager.GAME_SEND_EXCHANGE_OTHER_MOVE_OK(
              perso2.get_compte().getGameThread().get_out(), 'O', "-", "" + guid);
        } else {
          couple.second = newQua;
          SocketManager.GAME_SEND_EXCHANGE_MOVE_OK(perso1, 'O', "+", "" + guid + "|" + newQua);
          SocketManager.GAME_SEND_EXCHANGE_OTHER_MOVE_OK(
              perso2.get_compte().getGameThread().get_out(),
              'O',
              "+",
              "" + guid + "|" + newQua + add);
        }
      } else if (i == 2) {
        Couple<Integer, Integer> couple = getCoupleInList(items2, guid);
        int newQua = couple.second - qua;

        if (newQua < 1) // Si il n'y a pu d'item
        {
          items2.remove(couple);
          SocketManager.GAME_SEND_EXCHANGE_OTHER_MOVE_OK(
              perso1.get_compte().getGameThread().get_out(), 'O', "-", "" + guid);
          SocketManager.GAME_SEND_EXCHANGE_MOVE_OK(perso2, 'O', "-", "" + guid);
        } else {
          couple.second = newQua;
          SocketManager.GAME_SEND_EXCHANGE_OTHER_MOVE_OK(
              perso1.get_compte().getGameThread().get_out(),
              'O',
              "+",
              "" + guid + "|" + newQua + add);
          SocketManager.GAME_SEND_EXCHANGE_MOVE_OK(perso2, 'O', "+", "" + guid + "|" + newQua);
        }
      }
    }
    public synchronized void addItem(int guid, int qua, int pguid) {
      ok1 = false;
      ok2 = false;

      Objet obj = World.getObjet(guid);
      int i = 0;

      if (perso1.get_GUID() == pguid) i = 1;
      if (perso2.get_GUID() == pguid) i = 2;

      if (qua == 1) qua = 1;
      String str = guid + "|" + qua;
      if (obj == null) return;
      String add = "|" + obj.getTemplate().getID() + "|" + obj.parseStatsString();
      SocketManager.GAME_SEND_EXCHANGE_OK(
          perso1.get_compte().getGameThread().get_out(), ok1, perso1.get_GUID());
      SocketManager.GAME_SEND_EXCHANGE_OK(
          perso2.get_compte().getGameThread().get_out(), ok1, perso1.get_GUID());
      SocketManager.GAME_SEND_EXCHANGE_OK(
          perso1.get_compte().getGameThread().get_out(), ok2, perso2.get_GUID());
      SocketManager.GAME_SEND_EXCHANGE_OK(
          perso2.get_compte().getGameThread().get_out(), ok2, perso2.get_GUID());
      if (i == 1) {
        Couple<Integer, Integer> couple = getCoupleInList(items1, guid);
        if (couple != null) {
          couple.second += qua;
          SocketManager.GAME_SEND_EXCHANGE_MOVE_OK(
              perso1, 'O', "+", "" + guid + "|" + couple.second);
          SocketManager.GAME_SEND_EXCHANGE_OTHER_MOVE_OK(
              perso2.get_compte().getGameThread().get_out(),
              'O',
              "+",
              "" + guid + "|" + couple.second + add);
          return;
        }
        SocketManager.GAME_SEND_EXCHANGE_MOVE_OK(perso1, 'O', "+", str);
        SocketManager.GAME_SEND_EXCHANGE_OTHER_MOVE_OK(
            perso2.get_compte().getGameThread().get_out(), 'O', "+", str + add);
        items1.add(new Couple<Integer, Integer>(guid, qua));
      } else if (i == 2) {
        Couple<Integer, Integer> couple = getCoupleInList(items2, guid);
        if (couple != null) {
          couple.second += qua;
          SocketManager.GAME_SEND_EXCHANGE_MOVE_OK(
              perso2, 'O', "+", "" + guid + "|" + couple.second);
          SocketManager.GAME_SEND_EXCHANGE_OTHER_MOVE_OK(
              perso1.get_compte().getGameThread().get_out(),
              'O',
              "+",
              "" + guid + "|" + couple.second + add);
          return;
        }
        SocketManager.GAME_SEND_EXCHANGE_MOVE_OK(perso2, 'O', "+", str);
        SocketManager.GAME_SEND_EXCHANGE_OTHER_MOVE_OK(
            perso1.get_compte().getGameThread().get_out(), 'O', "+", str + add);
        items2.add(new Couple<Integer, Integer>(guid, qua));
      }
    }
  public Couple<String> setText(@Nullable final String text, @Nullable final String requestor) {
    if (StringUtil.isEmpty(text)
        && !Comparing.equal(requestor, myCurrentRequestor)
        && !EventLog.LOG_REQUESTOR.equals(requestor)) {
      return Couple.of(myInfoPanel.getText(), myCurrentRequestor);
    }

    boolean logMode = myInfoPanel.updateText(EventLog.LOG_REQUESTOR.equals(requestor) ? "" : text);
    myCurrentRequestor = logMode ? EventLog.LOG_REQUESTOR : requestor;
    return Couple.of(text, requestor);
  }
 /**
  * This is the preferred way for creating a Representation via a customized Resource serialization
  *
  * @param rootName
  * @param serialization
  */
 public JdomRepresentation(String rootName, CoupleList<String, Object> serialization) {
   Element root = new Element(rootName);
   for (Couple<String, Object> couple : serialization) {
     Element elt = new Element(couple.getLeft());
     if (couple.getRight() == null) {
       elt.setText(this.getEmptyValue());
     } else {
       elt.setText(couple.getRight().toString());
     }
     root.addContent(elt);
   }
   this.document = new Document(root);
 }
  @NotNull
  @Override
  public Couple<? extends MiniSet<E>> splitAt(int position) {
    final int n = size();
    if (position == 0) return Couple.of(MiniEmpty.one(), this);
    if (position == n) return Couple.of(this, MiniEmpty.one());
    if (position < 0 || position > n)
      throw new IndexOutOfBoundsException(
          "Attempted to split a list of " + n + " elements at position " + position);

    final Object[] array = array();
    MiniSet<E> a = setOf(array, 0, position, false, false);
    MiniSet<E> b = setOf(array, position, n, false, false);
    return Couple.of(a, b);
  }
  private static Couple<Integer> composeText(
      StringBuilder sb,
      List<Pair<String, HighlightInfo>> list,
      int index,
      String text,
      int endPos,
      int startPos) {
    int i = index;
    while (i < list.size()) {
      Pair<String, HighlightInfo> pair = list.get(i);
      HighlightInfo info = pair.second;
      if (info.endOffset <= startPos) {
        break;
      }

      String severity = pair.first;
      HighlightInfo prev = i < list.size() - 1 ? list.get(i + 1).second : null;

      sb.insert(0, text.substring(info.endOffset, endPos));
      sb.insert(0, "</" + severity + ">");
      endPos = info.endOffset;
      if (prev != null && prev.endOffset > info.startOffset) {
        Couple<Integer> result = composeText(sb, list, i + 1, text, endPos, info.startOffset);
        i = result.first - 1;
        endPos = result.second;
      }
      sb.insert(0, text.substring(info.startOffset, endPos));
      sb.insert(0, "<" + severity + " descr=\"" + info.getDescription() + "\">");

      endPos = info.startOffset;
      i++;
    }

    return Couple.newOne(i, endPos);
  }
  private static Couple<Integer> composeText(
      StringBuilder sb,
      List<Pair<String, HighlightInfo>> list,
      int index,
      String text,
      int endPos,
      int startPos,
      boolean showAttributesKeys) {
    int i = index;
    while (i < list.size()) {
      Pair<String, HighlightInfo> pair = list.get(i);
      HighlightInfo info = pair.second;
      if (info.endOffset <= startPos) {
        break;
      }

      String severity = pair.first;
      HighlightInfo prev = i < list.size() - 1 ? list.get(i + 1).second : null;

      sb.insert(0, text.substring(info.endOffset, endPos));
      sb.insert(0, "</" + severity + ">");
      endPos = info.endOffset;
      if (prev != null && prev.endOffset > info.startOffset) {
        Couple<Integer> result =
            composeText(sb, list, i + 1, text, endPos, info.startOffset, showAttributesKeys);
        i = result.first - 1;
        endPos = result.second;
      }
      sb.insert(0, text.substring(info.startOffset, endPos));

      String str =
          "<"
              + severity
              + " descr=\""
              + StringUtil.escapeQuotes(String.valueOf(info.getDescription()))
              + "\"";
      if (showAttributesKeys) {
        str += " textAttributesKey=\"" + info.forcedTextAttributesKey + "\"";
      }
      str += ">";
      sb.insert(0, str);

      endPos = info.startOffset;
      i++;
    }

    return Couple.of(i, endPos);
  }
 public synchronized void apply() {
   // Gestion des Kamas
   perso1.addKamas((-kamas1 + kamas2));
   perso2.addKamas((-kamas2 + kamas1));
   for (Couple<Integer, Integer> couple : items1) {
     if (couple.second == 0) continue;
     if (!perso1.hasItemGuid(
         couple.first)) // Si le perso n'a pas l'item (Ne devrait pas arriver)
     {
       couple.second = 0; // On met la quantité a 0 pour éviter les problemes
       continue;
     }
     Objet obj = World.getObjet(couple.first);
     if ((obj.getQuantity() - couple.second) < 1) // S'il ne reste plus d'item apres l'échange
     {
       perso1.removeItem(couple.first);
       couple.second = obj.getQuantity();
       SocketManager.GAME_SEND_REMOVE_ITEM_PACKET(perso1, couple.first);
       if (!perso2.addObjet(obj, true)) // Si le joueur avait un item similaire
       World.removeItem(couple.first); // On supprime l'item inutile
     } else {
       obj.setQuantity(obj.getQuantity() - couple.second);
       SocketManager.GAME_SEND_OBJECT_QUANTITY_PACKET(perso1, obj);
       Objet newObj = Objet.getCloneObjet(obj, couple.second);
       if (perso2.addObjet(newObj, true)) // Si le joueur n'avait pas d'item similaire
       World.addObjet(newObj, true); // On ajoute l'item au World
     }
   }
   for (Couple<Integer, Integer> couple : items2) {
     if (couple.second == 0) continue;
     if (!perso2.hasItemGuid(
         couple.first)) // Si le perso n'a pas l'item (Ne devrait pas arriver)
     {
       couple.second = 0; // On met la quantité a 0 pour éviter les problemes
       continue;
     }
     Objet obj = World.getObjet(couple.first);
     if ((obj.getQuantity() - couple.second) < 1) // S'il ne reste plus d'item apres l'échange
     {
       perso2.removeItem(couple.first);
       couple.second = obj.getQuantity();
       SocketManager.GAME_SEND_REMOVE_ITEM_PACKET(perso2, couple.first);
       if (!perso1.addObjet(obj, true)) // Si le joueur avait un item similaire
       World.removeItem(couple.first); // On supprime l'item inutile
     } else {
       obj.setQuantity(obj.getQuantity() - couple.second);
       SocketManager.GAME_SEND_OBJECT_QUANTITY_PACKET(perso2, obj);
       Objet newObj = Objet.getCloneObjet(obj, couple.second);
       if (perso1.addObjet(newObj, true)) // Si le joueur n'avait pas d'item similaire
       World.addObjet(newObj, true); // On ajoute l'item au World
     }
   }
   // Fin
   perso1.set_isTradingWith(0);
   perso2.set_isTradingWith(0);
   perso1.setCurExchange(null);
   perso2.setCurExchange(null);
   SocketManager.GAME_SEND_Ow_PACKET(perso1);
   SocketManager.GAME_SEND_Ow_PACKET(perso2);
   SocketManager.GAME_SEND_STATS_PACKET(perso1);
   SocketManager.GAME_SEND_STATS_PACKET(perso2);
   SocketManager.GAME_SEND_EXCHANGE_VALID(perso1.get_compte().getGameThread().get_out(), 'a');
   SocketManager.GAME_SEND_EXCHANGE_VALID(perso2.get_compte().getGameThread().get_out(), 'a');
   SQLManager.SAVE_PERSONNAGE(perso1, true);
   SQLManager.SAVE_PERSONNAGE(perso2, true);
 }