package bencode.type;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * <p>B编码的列表类型。
 * 
 * <p>格式如下：
 * <ul>
 *  <li>列表的基本格式为"l${contents}e"</li>
 *  <li>列表的内容可以是B编码所支持的任一中数据类型</li>
 *  <li>注意，列表中的各个元素之间是没有特定分隔符的</li>
 * </ul>
 *
 * @author caoxudong
 * @since 0.1.0
 */
public class BList implements BType<LinkedList<BType<?>>> {

  public static final char PREFIX = 'l';
  public static final char SUFFIX = 'e';
  
  private LinkedList<BType<?>> content = new LinkedList<>();
  private int contentLength = 2;
  
  @Override
  public LinkedList<BType<?>> getContent() {
    return this.content;
  }
  
  @Override
  public void setContent(LinkedList<BType<?>> value) {
    int newValueLength = 0;
    for (BType<?> e: value) {
      newValueLength += e.getContentLength();
    }
    this.content = value;
    this.contentLength = newValueLength + 2;
  }
  
  @Override
  public int getContentLength() {
    return contentLength;
  }
  
  public void add(BType<?> bElement) {
    this.content.add(bElement);
    this.contentLength += bElement.getContentLength();
  }
  
  public void addAll(Collection<? extends BType<?>> list) {
    this.content.addAll(list);
    for (BType<?> e: content) {
      this.contentLength += e.getContentLength();
    }
  }
  
  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if (!(obj instanceof BList)) {
      return false;
    }

    Iterator<BType<?>> selfIterator = content.iterator();
    Iterator<BType<?>> targetIterator = ((BList)obj).content.iterator();
    while ((selfIterator.hasNext()) && (targetIterator.hasNext())) {
      BType<?> selfElemenet = selfIterator.next();
      BType<?> targetElement = targetIterator.next();
      boolean isEqualTo = selfElemenet == null 
          ? targetElement == null
          : selfElemenet.equals(targetElement);
      if (!isEqualTo) {
        return false;
      }
    }
    return !(selfIterator.hasNext() || targetIterator.hasNext());
  }
}

@@ -52,36 +52,8 @@
 n;%0D%0A
-import java.util.Iterator;%0D%0A
 impo
@@ -1495,580 +1495,59 @@
  %7D%0D%0A
-%0D%0A
     
-Iterator%3CBType%3C?%3E%3E selfIterator = content.iterator();%0D%0A    Iterator%3CBType%3C?%3E%3E targetIterator = ((BList)obj).content.iterator();%0D%0A    while ((selfIterator.hasNext()) && (targetIterator.hasNext())) %7B%0D%0A      BType%3C?%3E selfElemenet = selfIterator.next();%0D%0A      BType%3C?%3E targetElement = targetIterator.next();%0D%0A      boolean isEqualTo = selfElemenet == null %0D%0A          ? targetElement == null%0D%0A          : selfElemenet.equals(targetElement);%0D%0A      if (!isEqualTo) %7B%0D%0A        return false;%0D%0A      %7D%0D%0A    %7D%0D%0A    return !(selfIterator.hasNext() %7C%7C targetIterator.hasNext()
+return this.content.equals(((BList)obj).content
 );%0D%0A

update BList#equals method