code: @Override
      public void computeEfficientMargins()
      {
          //minimal margins
          emargin.top = margin.top;
          emargin.bottom = margin.bottom;
          //check if something inside can be collapsed and it is larger
          if (containsBlocks() && containsFlow())
          {
              BlockBox firstseparated = null;
              int mbottom = 0;
              
              for (int i = 0; i < getSubBoxNumber(); i++)
              {
                  BlockBox subbox = (BlockBox) getSubBox(i);
                  if (subbox.isDisplayed() && subbox.isInFlow())
                  {
                      subbox.computeEfficientMargins();
                      boolean boxempty = subbox.marginsAdjoin();
                      
                      //if no separated box yet, update the top margin
                      if (firstseparated == null && !separatedFromTop(this)) 
                      {
                      	if (subbox.emargin.top > emargin.top)
                      		emargin.top = subbox.emargin.top;
                      }
                      
                      //update the bottom margin
                      if (boxempty)
                      {
                          int m = Math.max(subbox.emargin.top, subbox.emargin.bottom); //margins adjoin: we may use both top or bottom
                      	if (m > mbottom)
                      		mbottom = m;
                      }
                      else
                      	mbottom = subbox.emargin.bottom;
                      
                      if (!boxempty && firstseparated == null)
                          firstseparated = subbox;
                  }
              }
  
              //collapse bottom margins
              if (mbottom > emargin.bottom && !separatedFromBottom(this))
              	emargin.bottom = mbottom;
          }
          
          //if the box is empty, collapse it to a single margin
          if (marginsAdjoin())
          {
          	emargin.top = Math.max(emargin.top, emargin.bottom);
          	emargin.bottom = 0;
          }
          
      }
docs: Computes the efficient sizes of in-flow margins for collapsing