/*
 * Copyright 2013 Proofpoint, Inc.
 *
 * 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 com.proofpoint.reporting;

import com.google.common.base.Function;
import com.proofpoint.reporting.BucketIdProvider;

public abstract class Bucketed<T>
{
    private static final BucketIdProvider INITIAL_BUCKET_ID_PROVIDER = new BucketIdProvider()
    {
        @Override
        public int get()
        {
            return -5;
        }
    };
    private BucketIdProvider bucketIdProvider = INITIAL_BUCKET_ID_PROVIDER;
    private int currentBucketId = -10;
    private T previousBucket = null;
    private T currentBucket = null;

    protected abstract T createBucket();

    protected synchronized <R> R applyToCurrentBucket(Function<T, R> function)
    {
        rotateBucketIfNeeded();
        return function.apply(currentBucket);
    }

    @SuppressWarnings("UnusedDeclaration") // Called via reflection
    private synchronized T getPreviousBucket()
    {
        rotateBucketIfNeeded();
        return previousBucket;
    }

    public synchronized void setBucketIdProvider(BucketIdProvider bucketIdProvider)
    {
        this.bucketIdProvider = bucketIdProvider;
        int bucketId = bucketIdProvider.get();
        if (bucketId != currentBucketId) {
            currentBucketId = bucketId;
            currentBucket = createBucket();
            previousBucket = createBucket();
        }
    }

    private void rotateBucketIfNeeded()
    {
        int bucketId = bucketIdProvider.get();
        if (bucketId != currentBucketId) {
            if (currentBucketId + 1 == bucketId) {
                previousBucket = currentBucket;
            }
            else {
                previousBucket = createBucket();
            }
            currentBucketId = bucketId;
            currentBucket = createBucket();
        }
    }
}

@@ -628,16 +628,72 @@
 rting;%0A%0A
+import com.google.common.annotations.VisibleForTesting;%0A
 import c
@@ -1592,24 +1592,47 @@
 ket;%0A    %7D%0A%0A
+    @VisibleForTesting%0A
     public s
@@ -1759,37 +1759,40 @@
 ovider;%0A        
-int b
+currentB
 ucketId = bucket
@@ -1813,95 +1813,8 @@
 ();%0A
-        if (bucketId != currentBucketId) %7B%0A            currentBucketId = bucketId;%0A    
     
@@ -1841,36 +1841,32 @@
 createBucket();%0A
-    
         previous
@@ -1886,34 +1886,24 @@
 teBucket();%0A
-        %7D%0A
     %7D%0A%0A    p

Address review comments, mark @VisibleForTesting