/*
 * Copyright 2016 Ignacio del Valle Alles idelvall@brutusin.org.
 *
 * 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 org.brutusin.wava.core.plug.impl;

import org.brutusin.wava.core.plug.NicenessHandler;

/**
 * Chooses the most favorable niceness value from those values with the minimum
 * multiplicity (collisions due to the pigeonhole principle)
 *
 * @author Ignacio del Valle Alles idelvall@brutusin.org
 */
public final class HomogeneusSpreadNicenessHandler extends NicenessHandler {

    @Override
    public int getNiceness(int i, int total, int minNiceness, int maxNiceness) {
        int s = maxNiceness - minNiceness + 1;
        int r = (total - 1) / s + 1;
        int c = total - (s * (r - 1));
        int l = c * r;
        if (i + 1 <= l) {
            return minNiceness + i / r;
        } else {
            return minNiceness + c + (i - l) / (r - 1);
        }
    }
}

@@ -751,146 +751,382 @@
 %0A * 
-Chooses the most favorable niceness value from those values with the minimum%0D%0A * multiplicity (collisions due to the pigeonhole principle)
+Distributes niceness among processes in such a way that:%0D%0A * %3Cpre%3E%0D%0A * 1) %7B@code niceness(i+1)-1 %3C= niceness(i) %3C= niceness(i+1)%7D%0D%0A * 2) %7B@code multiplicity(n) = m%7D for all niceness values %7B@code n%7D, or exists a niceness value %7B@code n0%7D such as: %0D%0A *    %7B@code multiplicity(n) = m%7D for %7B@code n %3C= n0%7D and %0D%0A *    %7B@code multiplicity(n) = m+1%7D for %7B@code n %3E n0%7D%0D%0A * %3C/pre%3E
 %0D%0A *

improve documentation