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

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 = "UniqueNumber")
public class UniqueNumber extends ParameterBarn {
	private Integer currentNumber;
	private String format;

	@Constructor
	public UniqueNumber(
			@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.format = format;
		try {
			currentNumber = Integer.parseInt(begin);
		} catch (Exception e) {
			currentNumber = 0;
		}
	}

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

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