/**
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.activemq;

import java.io.Serializable;
import java.util.Random;

/**
 * Configuration options used to control how messages are re-delivered when they
 * are rolled back.
 * 
 * @org.apache.xbean.XBean element="redeliveryPolicy"
 * 
 * @version $Revision: 1.11 $
 */
public class RedeliveryPolicy implements Cloneable, Serializable {

	public static final int NO_MAXIMUM_REDELIVERIES = -1;
	
    // +/-15% for a 30% spread -cgs
    protected double collisionAvoidanceFactor = 0.15d;
    protected int maximumRedeliveries = 6;
    protected long initialRedeliveryDelay = 1000L;
    protected static Random randomNumberGenerator;
    protected boolean useCollisionAvoidance = false;
    protected boolean useExponentialBackOff = false;
    protected short backOffMultiplier = 5;

    public RedeliveryPolicy() {
    }

    public RedeliveryPolicy copy() {
        try {
            return (RedeliveryPolicy) clone();
        }
        catch (CloneNotSupportedException e) {
            throw new RuntimeException("Could not clone: " + e, e);
        }
    }

    public short getBackOffMultiplier() {
        return backOffMultiplier;
    }

    public void setBackOffMultiplier(short backOffMultiplier) {
        this.backOffMultiplier = backOffMultiplier;
    }

    public short getCollisionAvoidancePercent() {
        return (short) Math.round(collisionAvoidanceFactor * 100);
    }

    public void setCollisionAvoidancePercent(short collisionAvoidancePercent) {
        this.collisionAvoidanceFactor = collisionAvoidancePercent * 0.01d;
    }

    public long getInitialRedeliveryDelay() {
        return initialRedeliveryDelay;
    }

    public void setInitialRedeliveryDelay(long initialRedeliveryDelay) {
        this.initialRedeliveryDelay = initialRedeliveryDelay;
    }

    public int getMaximumRedeliveries() {
        return maximumRedeliveries;
    }

    public void setMaximumRedeliveries(int maximumRedeliveries) {
        this.maximumRedeliveries = maximumRedeliveries;
    }

    public long getRedeliveryDelay(long previousDelay) {
        long redeliveryDelay;

        if (previousDelay == 0) {
            redeliveryDelay = initialRedeliveryDelay;
        } else if (useExponentialBackOff && backOffMultiplier > 1) {
            redeliveryDelay = previousDelay * backOffMultiplier;
        } else {
            redeliveryDelay = previousDelay;
        }

        if (useCollisionAvoidance) {
            /*
             * First random determines +/-, second random determines how far to
             * go in that direction. -cgs
             */
            Random random = getRandomNumberGenerator();
            double variance = (random.nextBoolean() ? collisionAvoidanceFactor : -collisionAvoidanceFactor) * random.nextDouble();
            redeliveryDelay += redeliveryDelay * variance;
        }

        return redeliveryDelay;
    }

    public boolean isUseCollisionAvoidance() {
        return useCollisionAvoidance;
    }

    public void setUseCollisionAvoidance(boolean useCollisionAvoidance) {
        this.useCollisionAvoidance = useCollisionAvoidance;
    }

    public boolean isUseExponentialBackOff() {
        return useExponentialBackOff;
    }

    public void setUseExponentialBackOff(boolean useExponentialBackOff) {
        this.useExponentialBackOff = useExponentialBackOff;
    }

    protected static synchronized Random getRandomNumberGenerator() {
        if (randomNumberGenerator == null) {
            randomNumberGenerator = new Random();
        }
        return randomNumberGenerator;
    }

}

@@ -1248,23 +1248,21 @@
 s%0A    pr
-otec
+iva
 te
-d
  double 
@@ -1301,23 +1301,21 @@
 ;%0A    pr
-otec
+iva
 te
-d
  int max
@@ -1342,23 +1342,21 @@
 ;%0A    pr
-otec
+iva
 te
-d
  long in
@@ -1387,31 +1387,29 @@
 000L;%0A    pr
-otec
+iva
 te
-d
  static Rand
@@ -1432,39 +1432,37 @@
 enerator;%0A    pr
-otec
+iva
 te
-d
  boolean useColl
@@ -1491,23 +1491,21 @@
 ;%0A    pr
-otec
+iva
 te
-d
  boolean
@@ -1542,23 +1542,21 @@
 ;%0A    pr
-otec
+iva
 te
-d
  short b

change scope of variables from protected to private