All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anrisoftware.globalpom.distribution.linear.LinearDistributionRange Maven / Gradle / Ivy

There is a newer version: 4.7.0
Show newest version
/*
 * Copyright 2016 Erwin Müller 
 *
 * 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.anrisoftware.globalpom.distribution.linear;

import static org.apache.commons.math3.util.FastMath.abs;

import java.util.Iterator;

import javax.inject.Inject;

import com.anrisoftware.globalpom.distribution.range.DefaultRange;
import com.anrisoftware.globalpom.distribution.range.DistributionRange;
import com.anrisoftware.globalpom.distribution.range.Range;
import com.anrisoftware.globalpom.distribution.range.RangeFactory;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;

/**
 * Linear distribution range.
 *
 * @author Erwin Mueller, [email protected]
 * @since 2.14
 */
class LinearDistributionRange extends DefaultRange implements DistributionRange {

    @Inject
    private RangeFactory rangeFactory;

    @Inject
    private LinearDistributionRangeFactory linearRangeFactory;

    /**
     * @see LinearDistributionRangeFactory#create(DistributionRange)
     */
    @AssistedInject
    LinearDistributionRange(@Assisted Range range) {
        super(range);
    }

    @Override
    public Iterable iterator(final int size) {
        return new Iterable() {

            @Override
            public Iterator iterator() {
                return new It(size);
            }
        };
    }

    private class It implements Iterator {

        private final int size;

        private int index;

        It(int size) {
            this.size = size;
            this.index = 0;
        }

        @Override
        public boolean hasNext() {
            return index < size;
        }

        @Override
        public DistributionRange next() {
            return nextRange(index++, size, getMax());
        }

        private DistributionRange nextRange(int index, int size, double max) {
            double min = getMin();
            double step = abs((min - max) / size);
            double rmin = min + step * index;
            double rmax = min + step * (index + 1);
            return linearRangeFactory.create(rangeFactory.create(rmin, rmax));
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy