class IdentityUsernameGenerator extends AbstractIdentityValueGenerator {

	@Override
	public IdmIdentityDto generate(IdmIdentityDto dto, IdmGenerateValueDto valueGenerator) {
		Assert.notNull(dto, "DTO is required.");
		Assert.notNull(valueGenerator, "Value generator is required.");
		//
		// if exists username and configuration doesn't allow regenerate return dto
		if (!valueGenerator.isRegenerateValue() && StringUtils.isNotEmpty(dto.getUsername())) {
			return dto;
		}
		//
		String resultUsername = generateUsername(dto, valueGenerator);
		if (StringUtils.isEmpty(resultUsername)) {
			return dto;
		}
		//
		if (isSearchUniqueUsername(valueGenerator)) {
			dto.setUsername(getUniqueUsername(resultUsername));
		} else {
			dto.setUsername(resultUsername);
		}
		//
		return dto;
	}

}