/*
 * Copyright 1998-2012 Linux.org.ru
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package ru.org.linux.comment;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ru.org.linux.user.ProfileProperties;

import javax.annotation.Nonnull;
import java.io.Serializable;
import java.util.*;

public class CommentList implements Serializable {
  private static final Log logger = LogFactory.getLog(CommentList.class);

  private final List<Comment> comments = new ArrayList<Comment>(CommentFilter.COMMENTS_INITIAL_BUFSIZE);
  private final CommentNode root = new CommentNode();
  private final Map<Integer, CommentNode> treeHash = new HashMap<Integer, CommentNode>(CommentFilter.COMMENTS_INITIAL_BUFSIZE);

  private final long lastmod;

  public CommentList(List<Comment> comments, long lastmod) {
    this.lastmod = lastmod;
    this.comments.addAll(comments);
    logger.debug("Read list size = " +comments.size());
    buildTree();
  }

  @Nonnull
  public List<Comment> getList() {
    return Collections.unmodifiableList(comments);
  }

  private void buildTree() {
    /* build tree */
    for (Comment comment : comments) {
      CommentNode node = new CommentNode(comment);

      treeHash.put(comment.getId(), node);

      if (comment.getReplyTo()==0) {
        root.addChild(node);
      } else {
        CommentNode parentNode = treeHash.get(comment.getReplyTo());
        if (parentNode!=null) {
          parentNode.addChild(node);
        } else {
          root.addChild(node);
        }
      }
    }
  }

  public CommentNode getRoot() {
    return root;
  }

  public CommentNode getNode(int msgid) {
    return treeHash.get(msgid);
  }

  private int getCommentPage(@Nonnull Comment comment, int messages, boolean reverse) {
    int index = comments.indexOf(comment);

    if (reverse) {
      return (comments.size()-index)/messages;
    } else {
      return index/messages;
    }
  }

  public int getCommentPage(@Nonnull Comment comment, @Nonnull ProfileProperties profile) {
    int messages = profile.getMessages();
    boolean reverse = profile.isShowNewFirst();

    return getCommentPage(comment, messages, reverse);
  }

  public long getLastmod() {
    return lastmod;
  }
}

@@ -661,84 +661,47 @@
 ort 
-org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory
+com.google.common.collect.ImmutableList
 ;%0Aim
@@ -822,17 +822,68 @@
 va.util.
-*
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Apubli
@@ -942,176 +942,45 @@
 ate 
-static final Log logger = LogFactory.getLog(CommentList.class);%0A%0A  private final List%3CComment%3E comments = new ArrayList%3CComment%3E(CommentFilter.COMMENTS_INITIAL_BUFSIZE)
+final ImmutableList%3CComment%3E comments
 ;%0A  
@@ -1280,16 +1280,17 @@
 astmod;%0A
+%0A
     this
@@ -1302,73 +1302,40 @@
 ents
-.addAll(comments);%0A    logger.debug(%22Read list size = %22 +
+ = ImmutableList.copyOf(
 comments
 .siz
@@ -1330,23 +1330,16 @@
 comments
-.size()
 );%0A    b
@@ -1375,16 +1375,25 @@
  public 
+Immutable
 List%3CCom
@@ -1425,45 +1425,16 @@
 urn 
-Collections.unmodifiableList(
 comments
 );%0A 
@@ -1425,25 +1425,24 @@
 urn comments
-)
 ;%0A  %7D%0A%0A  pri

use ImmutableList