package org.bench4q.agent.plugin.basic.iterator;

import org.bench4q.agent.plugin.ParameterBarn;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;

@Plugin(value = "IteratorNumber")
public class IteratorNumber extends ParameterBarn {
	private Long iteratorNum;
	private String format;

	@Constructor
	public IteratorNumber(
			@Parameter(type = SupportTypes.Field, value = "testId") String testId,
			@Parameter(type = SupportTypes.Field, value = "begin") String begin,
			@Parameter(type = SupportTypes.Field, value = "format") String format) {
		super(testId);
		this.iteratorNum = Long.parseLong(begin);
		this.format = format;
	}

	@Behavior(value = "next", type = BehaviorType.CONTROL_BEHAVIOR)
	public void next() {
		synchronized (iteratorNum) {
			iteratorNum++;
		}
	}

	@Override
	public String getValue(String var) {
		return (this.format == null || this.format.isEmpty()) ? String
				.valueOf(this.iteratorNum) : String.format(this.format,
				this.iteratorNum);
	}
}
