public QueryTemplatePacket and(UnionTemplatePacket template){
  LinkedList<QueryTemplatePacket> optimizedPackets=new LinkedList<QueryTemplatePacket>();
  for (  QueryTemplatePacket packet : template.getPackets()) {
    QueryTemplatePacket result=new QueryTemplatePacket(this);
    result.intersectRanges(packet);
    result.intersectUids(packet);
    if (!result.isAlwaysEmpty())     optimizedPackets.add(result);
  }
  if (optimizedPackets.isEmpty())   template.setAlwaysEmpty(true);
  template.setPackets(optimizedPackets);
  return template;
}

public QueryTemplatePacket and ( UnionTemplatePacket template ) { LinkedList < QueryTemplatePacket > optimizedPackets = new LinkedList < QueryTemplatePacket > ( ) ; for ( QueryTemplatePacket packet : template . getPackets ( ) ) { QueryTemplatePacket result = new QueryTemplatePacket ( this ) ; result . intersectRanges ( packet ) ; result . intersectUids ( packet ) ; if ( ! result . isAlwaysEmpty ( ) ) optimizedPackets . add ( result ) ; } if ( optimizedPackets . isEmpty ( ) ) template . setAlwaysEmpty ( true ) ; template . setPackets ( optimizedPackets ) ; return template ; }
merge given templates into one template . for example : x = 3 and ( y = 5 or z = 8 ) = = > ( x = 3 and y = 5 ) or ( x = 3 and z = 8 )
