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

de.flapdoodle.tests.Variations Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2011
 *   Michael Mosmann 
 *   Jan Bernitt 
 *
 * with contributions from
 * 	nobody yet
 *
 * 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 de.flapdoodle.tests;

import java.util.Iterator;

import de.flapdoodle.tests.sample.IJoinedSample;
import de.flapdoodle.tests.sample.ISample;
import de.flapdoodle.tests.sample.Sample;
import de.flapdoodle.tests.sample.Simple;
import de.flapdoodle.tests.sampler.IVariation;


public class Variations {

	private Variations() {
		// no instance
	}
		
	public static  IGenerator> of(IVariation variation) {
		return new First(variation);
	}
	
	public static ,N extends IGenerator> IGenerator> of(IVariation variation,N next) {
		return new Join(variation,next);
	}
	
	public static class Join,N extends IGenerator> implements IGenerator> {

		private final IVariation _variation;
		private final N _next;
		private Iterator _iterator;
		private S _nextValue;

		public Join(IVariation variation, N next) {
			_variation = variation;
			_next = next;
			
			_iterator = _variation.iterator();
		}
		
		@Override
		public boolean hasNext() {
			return _iterator.hasNext() || _next.hasNext();
		}
		
		public IJoinedSample get() {
			if (_nextValue==null) _nextValue=_next.get();
			IJoinedSample ret = Sample.of(_iterator.next(), _nextValue);
			if (!_iterator.hasNext()) {
				if (_next.hasNext()) {
					_iterator=_variation.iterator();
					_nextValue=null;
				}
			}
			return ret;
		}
	}
	
	static class First implements IGenerator> {

		private Iterator _iterator;

		public First(IVariation variation) {
			_iterator = variation.iterator();
		}
		
		@Override
		public boolean hasNext() {
			return _iterator.hasNext();
		}
		
		@Override
		public ISample get() {
			return Simple.of(_iterator.next());
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy