Program: 32030
Project: 32030
Buggy source:
/** 
 * Toggles the direct chat status of a room.<br> Create a new direct chat room in the account data section if the room does not exist, otherwise the room is removed from the account data section. Direct chat room user ID choice algorithm:<br> 1- oldest joined room member 2- oldest invited room member 3- the user himself
 * @param roomId the room roomId
 * @param callback the asynchronous callback
 */
public void toggleDirectChatRoom(String roomId,String aParticipantUserId,ApiCallback<Void> callback){
  IMXStore store=getDataHandler().getStore();
  Room room=store.getRoom(roomId);
  if (null != room) {
    HashMap<String,List<String>> params;
    if (null != store.getDirectChatRoomsDict()) {
      params=new HashMap<>(store.getDirectChatRoomsDict());
    }
 else {
      params=new HashMap<>();
    }
    if (getDirectChatRoomIdsList().indexOf(roomId) < 0) {
      ArrayList<String> roomIdsList=new ArrayList<>();
      RoomMember directChatMember=null;
      String chosenUserId;
      if (null == aParticipantUserId) {
        ArrayList<RoomMember> members=new ArrayList<>(room.getActiveMembers());
        if (members.size() > 1) {
          Collections.sort(members,new Comparator<RoomMember>(){
            @Override public int compare(            RoomMember r1,            RoomMember r2){
              int res;
              long diff;
              if (RoomMember.MEMBERSHIP_JOIN.equals(r2.membership) && RoomMember.MEMBERSHIP_INVITE.equals(r1.membership)) {
                res=1;
              }
 else               if (r2.membership.equals(r1.membership)) {
                diff=r1.getOriginServerTs() - r2.getOriginServerTs();
                res=(0 == diff) ? 0 : ((diff > 0) ? 1 : -1);
              }
 else {
                res=-1;
              }
              return res;
            }
          }
);
          int nextIndexSearch=0;
          if (!TextUtils.equals(members.get(0).getUserId(),getMyUserId())) {
            if (RoomMember.MEMBERSHIP_JOIN.equals(members.get(0).membership)) {
              directChatMember=members.get(0);
            }
          }
 else {
            nextIndexSearch=1;
            if (RoomMember.MEMBERSHIP_JOIN.equals(members.get(1).membership)) {
              directChatMember=members.get(1);
            }
          }
          if (null == directChatMember) {
            if (RoomMember.MEMBERSHIP_INVITE.equals(members.get(nextIndexSearch).membership)) {
              directChatMember=members.get(nextIndexSearch);
            }
          }
        }
        if (null == directChatMember) {
          directChatMember=members.get(0);
        }
        chosenUserId=directChatMember.getUserId();
      }
 else {
        chosenUserId=aParticipantUserId;
      }
      if (params.containsKey(chosenUserId)) {
        roomIdsList=new ArrayList<>(params.get(chosenUserId));
      }
      roomIdsList.add(roomId);
      params.put(chosenUserId,roomIdsList);
    }
 else {
      if (null != store.getDirectChatRoomsDict()) {
        Collection<List<String>> listOfList=store.getDirectChatRoomsDict().values();
        for (        List<String> list : listOfList) {
          if (list.contains(roomId)) {
            list.remove(roomId);
          }
        }
      }
 else {
        Log.e(LOG_TAG,"String_Node_Str");
        return;
      }
    }
    HashMap<String,Object> requestParams=new HashMap<>();
    Collection<String> userIds=params.keySet();
    for (    String userId : userIds) {
      requestParams.put(userId,params.get(userId));
    }
    mAccountDataRestClient.setAccountData(getMyUserId(),AccountDataRestClient.ACCOUNT_DATA_TYPE_DIRECT_MESSAGES,requestParams,callback);
  }
}

Fixed version (ground truth):
/** 
 * Toggles the direct chat status of a room.<br> Create a new direct chat room in the account data section if the room does not exist, otherwise the room is removed from the account data section. Direct chat room user ID choice algorithm:<br> 1- oldest joined room member 2- oldest invited room member 3- the user himself
 * @param roomId the room roomId
 * @param callback the asynchronous callback
 */
public void toggleDirectChatRoom(String roomId,String aParticipantUserId,ApiCallback<Void> callback){
  IMXStore store=getDataHandler().getStore();
  Room room=store.getRoom(roomId);
  if (null != room) {
    HashMap<String,List<String>> params;
    if (null != store.getDirectChatRoomsDict()) {
      params=new HashMap<>(store.getDirectChatRoomsDict());
    }
 else {
      params=new HashMap<>();
    }
    if (getDirectChatRoomIdsList().indexOf(roomId) < 0) {
      ArrayList<String> roomIdsList=new ArrayList<>();
      RoomMember directChatMember=null;
      String chosenUserId;
      if (null == aParticipantUserId) {
        ArrayList<RoomMember> members=new ArrayList<>(room.getActiveMembers());
        if (0 == members.size()) {
          return;
        }
        if (members.size() > 1) {
          Collections.sort(members,new Comparator<RoomMember>(){
            @Override public int compare(            RoomMember r1,            RoomMember r2){
              int res;
              long diff;
              if (RoomMember.MEMBERSHIP_JOIN.equals(r2.membership) && RoomMember.MEMBERSHIP_INVITE.equals(r1.membership)) {
                res=1;
              }
 else               if (r2.membership.equals(r1.membership)) {
                diff=r1.getOriginServerTs() - r2.getOriginServerTs();
                res=(0 == diff) ? 0 : ((diff > 0) ? 1 : -1);
              }
 else {
                res=-1;
              }
              return res;
            }
          }
);
          int nextIndexSearch=0;
          if (!TextUtils.equals(members.get(0).getUserId(),getMyUserId())) {
            if (RoomMember.MEMBERSHIP_JOIN.equals(members.get(0).membership)) {
              directChatMember=members.get(0);
            }
          }
 else {
            nextIndexSearch=1;
            if (RoomMember.MEMBERSHIP_JOIN.equals(members.get(1).membership)) {
              directChatMember=members.get(1);
            }
          }
          if (null == directChatMember) {
            if (RoomMember.MEMBERSHIP_INVITE.equals(members.get(nextIndexSearch).membership)) {
              directChatMember=members.get(nextIndexSearch);
            }
          }
        }
        if (null == directChatMember) {
          directChatMember=members.get(0);
        }
        chosenUserId=directChatMember.getUserId();
      }
 else {
        chosenUserId=aParticipantUserId;
      }
      if (params.containsKey(chosenUserId)) {
        roomIdsList=new ArrayList<>(params.get(chosenUserId));
      }
      roomIdsList.add(roomId);
      params.put(chosenUserId,roomIdsList);
    }
 else {
      if (null != store.getDirectChatRoomsDict()) {
        Collection<List<String>> listOfList=store.getDirectChatRoomsDict().values();
        for (        List<String> list : listOfList) {
          if (list.contains(roomId)) {
            list.remove(roomId);
          }
        }
      }
 else {
        Log.e(LOG_TAG,"String_Node_Str");
        return;
      }
    }
    HashMap<String,Object> requestParams=new HashMap<>();
    Collection<String> userIds=params.keySet();
    for (    String userId : userIds) {
      requestParams.put(userId,params.get(userId));
    }
    mAccountDataRestClient.setAccountData(getMyUserId(),AccountDataRestClient.ACCOUNT_DATA_TYPE_DIRECT_MESSAGES,requestParams,callback);
  }
}