/** Returns deep copy of this map instance. */
  public MediaSSRCGroupMap copy() {
    Map<String, List<SSRCGroup>> mapCopy = new HashMap<String, List<SSRCGroup>>();

    for (String media : groupMap.keySet()) {
      List<SSRCGroup> listToCopy = new ArrayList<SSRCGroup>(groupMap.get(media));
      List<SSRCGroup> listCopy = new ArrayList<SSRCGroup>(listToCopy.size());

      for (SSRCGroup group : listToCopy) {
        listCopy.add(group.copy());
      }

      mapCopy.put(media, listCopy);
    }

    return new MediaSSRCGroupMap(mapCopy);
  }
  /**
   * Extracts SSRC groups from Jingle content list.
   *
   * @param contents the list of <tt>ContentPacketExtension</tt> which will be examined for media
   *     SSRC groups.
   * @return <tt>MediaSSRCGroupMap</tt> that reflects SSRC groups of media described by given
   *     content list.
   */
  public static MediaSSRCGroupMap getSSRCGroupsForContents(List<ContentPacketExtension> contents) {
    MediaSSRCGroupMap mediaSSRCGroupMap = new MediaSSRCGroupMap();

    for (ContentPacketExtension content : contents) {
      List<SSRCGroup> mediaGroups = mediaSSRCGroupMap.getSSRCGroupsForMedia(content.getName());

      // FIXME: does not check for duplicates
      mediaGroups.addAll(SSRCGroup.getSSRCGroupsForContent(content));
    }

    return mediaSSRCGroupMap;
  }
  /**
   * Removes SSRC groups contained ing given <tt>MediaSSRCGroupMap</tt> from this map if they exist.
   *
   * @param mapToRemove the <tt>MediaSSRCGroupMap</tt> that contains SSRC groups mappings to be
   *     removed from this instance.
   * @return the <tt>MediaSSRCGroupMap</tt> that contains only these SSRC groups which were actually
   *     removed(existed in this map).
   */
  public MediaSSRCGroupMap remove(MediaSSRCGroupMap mapToRemove) {
    MediaSSRCGroupMap removedGroups = new MediaSSRCGroupMap();

    for (String media : mapToRemove.groupMap.keySet()) {
      List<SSRCGroup> groupList = getSSRCGroupsForMedia(media);

      List<SSRCGroup> toBeRemoved = new ArrayList<SSRCGroup>();

      for (SSRCGroup ssrcGroupToCheck : mapToRemove.groupMap.get(media)) {
        for (SSRCGroup ssrcGroup : groupList) {
          if (ssrcGroupToCheck.equals(ssrcGroup)) {
            toBeRemoved.add(ssrcGroup);
          }
        }
      }

      removedGroups.getSSRCGroupsForMedia(media).addAll(toBeRemoved);

      groupList.removeAll(toBeRemoved);
    }

    return removedGroups;
  }