public void AddDocument(Document d)
    {
     // Contract.Requires<ArgumentNullException>(d != null, "argument d must not be null");
      //  if (d == null) throw new ArgumentNullException("d");

      AddDocumentToPriorityNode(d, d.Priority);
    }
    private void AddDocumentToPriorityNode(Document doc, int priority)
    {
    //   Contract.Requires<ArgumentException>(priority >= 0 && priority < 10, "priority value must be between 0 and 9");
      //if (priority > 9 || priority < 0)
      //    throw new ArgumentException("Priority must be between 0 and 9");

      if (_priorityNodes[priority].Value == null)
      {
        --priority;
        if (priority >= 0)
        {
          // check for the next lower priority
          AddDocumentToPriorityNode(doc, priority);
        }
        else // now no priority node exists with the same priority or lower
        // add the new document to the end
        {
          _documentList.AddLast(doc);
          _priorityNodes[doc.Priority] = _documentList.Last;
        }
        return;
      }
      else // a priority node exists
      {
        LinkedListNode<Document> prioNode = _priorityNodes[priority];
        if (priority == doc.Priority)
        // priority node with the same priority exists
        {
          _documentList.AddAfter(prioNode, doc);

          // set the priority node to the last document with the same priority
          _priorityNodes[doc.Priority] = prioNode.Next;
        }
        else // only priority node with a lower priority exists
        {
          // get the first node of the lower priority
          LinkedListNode<Document> firstPrioNode = prioNode;

          while (firstPrioNode.Previous != null &&
             firstPrioNode.Previous.Value.Priority == prioNode.Value.Priority)
          {
            firstPrioNode = prioNode.Previous;
            prioNode = firstPrioNode;
          }

          _documentList.AddBefore(firstPrioNode, doc);

          // set the priority node to the new value
          _priorityNodes[doc.Priority] = firstPrioNode.Previous;
        }
      }
    }