/** @see java.util.AbstractList#add(java.lang.Object) */
  @Override
  public boolean add(CmsDocument element) {

    CmsDocument doc = convertDocument(element);
    if (doc.isNullDocument() || checkContainer(doc)) {
      return true;
    }

    ensureCapacity(m_size + 1);
    m_documents[m_size++] = doc;
    return true;
  }
  /** @see java.util.AbstractList#add(int, java.lang.Object) */
  @Override
  public void add(int index, CmsDocument element) {

    rangeCheck(index);
    CmsDocument doc = convertDocument(element);
    if (doc.isNullDocument() || checkContainer(doc)) {
      return;
    }

    ensureCapacity(m_size + 1); // Increments modCount!!
    System.arraycopy(m_documents, index, m_documents, index + 1, m_size - index);
    m_documents[index] = doc;
    m_size++;
  }
  /**
   * Converts an object to a document.
   *
   * <p>
   *
   * @param element the object to convert
   * @return the document object representation
   */
  private CmsDocument convertDocument(Object element) {

    try {
      return (CmsDocument) element;
    } catch (Exception e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("Tried adding wrong object to document list " + e);
      }
    }
    return CmsDocument.getNullDocument();
  }
  /**
   * Checks if the document is added as a subdocument or attachment.
   *
   * <p>If the document was not merged, it is a new document that has to be added to the list
   * depending on the only versions flag.
   *
   * <p>
   *
   * @param doc the document to check
   * @return true if the document was added as subdocument, attachment or if the document should not
   *     be added, otherwise false
   */
  private boolean checkContainer(CmsDocument doc) {

    boolean docnameWithoutPostfixEquality;
    for (int i = 0; i < m_size; i++) {
      docnameWithoutPostfixEquality =
          doc.getDocumentNameFullWithoutPostfix()
              .equals(m_documents[i].getDocumentNameFullWithoutPostfix());
      if (m_documents[i].isVersionOf(doc) || (m_useTypes && docnameWithoutPostfixEquality)) {
        if (m_useTypes) {

          if ((m_defaultType != null) && (m_defaultType.equals(doc.getPostfix()))) {
            if (m_documents[i].getTypes().size() > 0) {
              doc.setTypes(m_documents[i].getTypes());
            }
            doc.addType(m_documents[i]);
            m_documents[i] = doc;
          } else {
            m_documents[i].addType(doc);
          }
          return true;
        } else if ((m_useAttachments) && (doc.isAttachment()) && (!m_documents[i].isAttachment())) {
          // store attachment in temporary list to assign later
          m_attachments.add(doc);
          return true;
        } else if ((m_useAttachments) && (m_documents[i].isAttachment()) && (!doc.isAttachment())) {
          if (isOnlyVersions()) {
            return true;
          } else {
            m_attachments.add(m_documents[i]);
            m_documents[i] = doc;
            return true;
          }
        } else if (m_useLanguages) {
          // merge the document if languages are used
          m_documents[i] = m_documents[i].mergeDocuments(doc);
          return true;
        }
      } else if (isOnlyVersions()) {
        // this is no version of a present document, do not add it at all
        return true;
      }
    }
    return false;
  }