<commit_before>/*********************************************************************
 Flapi, the fluent API builder for Java.
 Visit the project page at https://github.com/UnquietCode/Flapi

 Flapi is free and open software provided without a license.
 ********************************************************************/

package unquietcode.tools.flapi;

import unquietcode.tools.flapi.outline.BlockOutline;
import unquietcode.tools.flapi.outline.BlockReference;
import unquietcode.tools.flapi.outline.DescriptorOutline;
import unquietcode.tools.flapi.outline.MethodOutline;

import javax.lang.model.SourceVersion;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author <NAME>
 * @version 08-25-2012
 *
 * Validates a descriptor to ensure that minimum requirements are met.
 * The "pre" validator works on the {@link DescriptorOutline} model.
 */
public class DescriptorPreValidator {
	private final DescriptorOutline outline;

	public DescriptorPreValidator(DescriptorOutline outline) {
		this.outline = outline;
	}

	public void validate() {
		checkThatDescriptorMethodNameIsValid();
		checkForInvalidMethodSignatures();
		checkForNameCollisions();
		checkForUnmatchedGroups();
	}

	private void checkThatDescriptorMethodNameIsValid() {
		String name = outline.getCreateMethod();

		if (!SourceVersion.isName(name)) {
			throw new DescriptorBuilderException("Invalid method name for create method: '"+name+"'.");
		}
	}

	private void checkForNameCollisions() {
		_checkForNameCollisions(outline, new HashSet<String>());
	}

	private void _checkForNameCollisions(BlockOutline block, Set<String> names) {
		if (block instanceof BlockReference) {
			return;
		}

		// block name collisions
		String name = block.getName();
		if (names.contains(name)) {
			throw new DescriptorBuilderException("Duplicate block name encountered: "+ name);
		} else {
			names.add(name);
		}

		// check method name collisions
		for (MethodOutline method : block.getAllMethods()) {
			String currentSignature = method.getMethodSignature();
			MethodParser currentParsed = new MethodParser(currentSignature);

			for (MethodOutline otherMethod : block.getAllMethods()) {
				if (method == otherMethod) { continue; }
				MethodParser otherParsed = new MethodParser(otherMethod.getMethodSignature());

				if (currentParsed.compilerEquivalent(otherParsed)) {
					throw new DescriptorBuilderException("Two methods with the same signature: " + currentSignature);
				}
			}
		}<commit_msg>reject block names which are not valid Java names<commit_after>/*********************************************************************
 Flapi, the fluent API builder for Java.
 Visit the project page at https://github.com/UnquietCode/Flapi

 Flapi is free and open software provided without a license.
 ********************************************************************/

package unquietcode.tools.flapi;

import unquietcode.tools.flapi.outline.BlockOutline;
import unquietcode.tools.flapi.outline.BlockReference;
import unquietcode.tools.flapi.outline.DescriptorOutline;
import unquietcode.tools.flapi.outline.MethodOutline;

import javax.lang.model.SourceVersion;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author <NAME>
 * @version 08-25-2012
 *
 * Validates a descriptor to ensure that minimum requirements are met.
 * The "pre" validator works on the {@link DescriptorOutline} model.
 */
public class DescriptorPreValidator {
	private final DescriptorOutline outline;

	public DescriptorPreValidator(DescriptorOutline outline) {
		this.outline = outline;
	}

	public void validate() {
		checkThatDescriptorMethodNameIsValid();
		checkForInvalidMethodSignatures();
		checkForNameCollisions();
		checkForUnmatchedGroups();
	}

	private void checkThatDescriptorMethodNameIsValid() {
		String name = outline.getCreateMethod();

		if (!SourceVersion.isName(name)) {
			throw new DescriptorBuilderException("Invalid method name for create method: '"+name+"'.");
		}
	}

	private void checkForNameCollisions() {
		_checkForNameCollisions(outline, new HashSet<String>());
	}

	private void _checkForNameCollisions(BlockOutline block, Set<String> names) {
		if (block instanceof BlockReference) {
			return;
		}

		// block name collisions
		String name = block.getName();
		if (names.contains(name)) {
			throw new DescriptorBuilderException("Duplicate block name encountered: "+ name);
		} else {
			names.add(name);
		}

		// check name validity
		if (!SourceVersion.isName(name)) {
			throw new DescriptorBuilderException("Invalid block name '"+name+"'.");
		}

		// check method name collisions
		for (MethodOutline method : block.getAllMethods()) {
			String currentSignature = method.getMethodSignature();
			MethodParser currentParsed = new MethodParser(currentSignature);

			for (MethodOutline otherMethod : block.getAllMethods()) {
				if (method == otherMethod) { continue; }
				MethodParser otherParsed = new MethodParser(otherMethod.getMethodSignature());

				if (currentParsed.compilerEquivalent(otherParsed)) {
					throw new DescriptorBuilderException("Two methods with the same signature: " + currentSignature);
				}
			}
		}
{'fraction_non_alphanumeric': 0.14591147786946737, 'fraction_numerical': 0.003000750187546887, 'mean_word_length': 5.28150765606596, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5107233', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1431, 'n_words': 373}