/*
 *  Copyright (c) 2012 Malhar, Inc.
 *  All Rights Reserved.
 */
package com.malhartech.lib.algo;

import java.util.Comparator;


/**
 *
 * @author Chetan Narsude <chetan@malhar-inc.com>
 */
class WindowedHolder<T>
{
  T identifier;
  int totalCount;
  transient int position;
  final transient int windowedCount[];

  WindowedHolder()
  {
    this.totalCount = 0;
    this.position = 0;
    windowedCount = null;
  }

  public WindowedHolder(T identifier, int windowCount)
  {
    this.identifier = identifier;
    this.totalCount = 0;
    this.position = 0;
    windowedCount = new int[windowCount];
  }

  public void adjustCount(int i)
  {
    windowedCount[position] += i;
  }

  public void slide()
  {
    int currentCount = windowedCount[position];
    position = position == windowedCount.length - 1 ? 0 : position + 1;
    totalCount += currentCount - windowedCount[position];
    windowedCount[position] = 0;
  }

  @Override
  public String toString()
  {
    return identifier + " => " + totalCount;
  }

  @Override
  public WindowedHolder clone()
  {
    WindowedHolder clone;
    if (windowedCount == null) {
      clone = new WindowedHolder();
      clone.identifier = identifier;
    }
    else {
      clone = new WindowedHolder(identifier, windowedCount.length);
    }

    clone.totalCount = totalCount;
    clone.position = position;

    return clone;
  }
}

class TopSpotComparator implements Comparator<WindowedHolder>
{
  @Override
  public int compare(WindowedHolder o1, WindowedHolder o2)
  {
    if (o1.totalCount > o2.totalCount) {
      return 1;
    }
    else if (o1.totalCount < o2.totalCount) {
      return -1;
    }

    return 0;
  }
}

@@ -125,17 +125,16 @@
 rator;%0A%0A
-%0A
 /**%0A *%0A 
@@ -248,26 +248,16 @@
 ount;%0A  
-transient 
 int posi
@@ -268,24 +268,8 @@
 ;%0A  
-final transient 
 int 
@@ -313,82 +313,8 @@
   %7B%0A
-    this.totalCount = 0;%0A    this.position = 0;%0A    windowedCount = null;%0A
   %7D%0A
@@ -915,370 +915,8 @@
 %0A  %7D
-%0A%0A  @Override%0A  public WindowedHolder clone()%0A  %7B%0A    WindowedHolder clone;%0A    if (windowedCount == null) %7B%0A      clone = new WindowedHolder();%0A      clone.identifier = identifier;%0A    %7D%0A    else %7B%0A      clone = new WindowedHolder(identifier, windowedCount.length);%0A    %7D%0A%0A    clone.totalCount = totalCount;%0A    clone.position = position;%0A%0A    return clone;%0A  %7D
 %0A%7D%0A%0A
@@ -975,16 +975,19 @@
 edHolder
+%3C?%3E
 %3E%0A%7B%0A  @O
@@ -1029,16 +1029,19 @@
 edHolder
+%3C?%3E
  o1, Win
@@ -1051,16 +1051,19 @@
 edHolder
+%3C?%3E
  o2)%0A  %7B

remove the clone method and transient fields.