@Override public void merge(Descriptor other){
  for (  DescriptorTag tag : descriptorTagFactory.getAllTags()) {
    Identifier identifier=tag.getIdentifier();
    if (identifier != null) {
      MergeStrategy strategy=getMergeStrategy(tag.getTagName());
      Descriptor left=baseDescriptor;
      Descriptor right=other;
      List<Element> itemsL=new ArrayList<Element>(left.getTags(tag));
      List<Element> itemsR=new ArrayList<Element>(right.getTags(tag));
      try {
        for (        Element itemL : itemsL) {
          DescriptorElement lElement=(DescriptorElement)itemL;
          DescriptorElement rElement=(DescriptorElement)right.getTagByIdentifier(tag.getTagName(),identifier.getIdentifier(lElement));
          if (rElement != null) {
            strategy.inBoth(left,lElement,rElement);
          }
 else {
            strategy.inLeft(left,lElement);
          }
        }
        for (        Element itemR : itemsR) {
          DescriptorElement rElement=(DescriptorElement)itemR;
          DescriptorElement lElement=(DescriptorElement)left.getTagByIdentifier(tag.getTagName(),identifier.getIdentifier(rElement));
          if (lElement == null) {
            strategy.inRight(left,rElement);
          }
        }
      }
 catch (      Exception ex) {
        throw new CargoException("Element Merging Exception",ex);
      }
    }
 else {
      Descriptor left=baseDescriptor;
      Descriptor right=other;
      List<Element> itemsL=left.getTags(tag);
      List<Element> itemsR=new ArrayList<Element>(right.getTags(tag));
      if (tag.isMultipleAllowed()) {
        for (        Element itemR : itemsR) {
          DescriptorElement rightElement=(DescriptorElement)itemR;
          left.addElement(tag,rightElement,left.getRootElement());
        }
      }
 else {
        MergeStrategy strategy=getMergeStrategy(tag.getTagName());
        DescriptorElement leftElement=itemsL.size() == 0 ? null : (DescriptorElement)itemsL.get(0);
        DescriptorElement rightElement=itemsR.size() == 0 ? null : (DescriptorElement)itemsR.get(0);
        try {
          if (leftElement != null && rightElement != null) {
            strategy.inBoth(left,leftElement,rightElement);
          }
 else           if (leftElement != null) {
            strategy.inLeft(left,leftElement);
          }
 else           if (rightElement != null) {
            strategy.inRight(left,rightElement);
          }
        }
 catch (        Exception ex) {
          throw new CargoException("Element Merging Exception",ex);
        }
      }
    }
  }
}

@ Override public void merge ( Descriptor other ) { for ( DescriptorTag tag : descriptorTagFactory . getAllTags ( ) ) { Identifier identifier = tag . getIdentifier ( ) ; if ( identifier != null ) { MergeStrategy strategy = getMergeStrategy ( tag . getTagName ( ) ) ; Descriptor left = baseDescriptor ; Descriptor right = other ; List < Element > itemsL = new ArrayList < Element > ( left . getTags ( tag ) ) ; List < Element > itemsR = new ArrayList < Element > ( right . getTags ( tag ) ) ; try { for ( Element itemL : itemsL ) { DescriptorElement lElement = ( DescriptorElement ) itemL ; DescriptorElement rElement = ( DescriptorElement ) right . getTagByIdentifier ( tag . getTagName ( ) , identifier . getIdentifier ( lElement ) ) ; if ( rElement != null ) { strategy . inBoth ( left , lElement , rElement ) ; } else { strategy . inLeft ( left , lElement ) ; } } for ( Element itemR : itemsR ) { DescriptorElement rElement = ( DescriptorElement ) itemR ; DescriptorElement lElement = ( DescriptorElement ) left . getTagByIdentifier ( tag . getTagName ( ) , identifier . getIdentifier ( rElement ) ) ; if ( lElement == null ) { strategy . inRight ( left , rElement ) ; } } } catch ( Exception ex ) { throw new CargoException ( "Element Merging Exception" , ex ) ; } } else { Descriptor left = baseDescriptor ; Descriptor right = other ; List < Element > itemsL = left . getTags ( tag ) ; List < Element > itemsR = new ArrayList < Element > ( right . getTags ( tag ) ) ; if ( tag . isMultipleAllowed ( ) ) { for ( Element itemR : itemsR ) { DescriptorElement rightElement = ( DescriptorElement ) itemR ; left . addElement ( tag , rightElement , left . getRootElement ( ) ) ; } } else { MergeStrategy strategy = getMergeStrategy ( tag . getTagName ( ) ) ; DescriptorElement leftElement = itemsL . size ( ) == 0 ? null : ( DescriptorElement ) itemsL . get ( 0 ) ; DescriptorElement rightElement = itemsR . size ( ) == 0 ? null : ( DescriptorElement ) itemsR . get ( 0 ) ; try { if ( leftElement != null && rightElement != null ) { strategy . inBoth ( left , leftElement , rightElement ) ; } else if ( leftElement != null ) { strategy . inLeft ( left , leftElement ) ; } else if ( rightElement != null ) { strategy . inRight ( left , rightElement ) ; } } catch ( Exception ex ) { throw new CargoException ( "Element Merging Exception" , ex ) ; } } } } }
merge this descriptor onto another .
