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

tools.codebuilder.EnumClass Maven / Gradle / Ivy

The newest version!
package tools.codebuilder;

import java.util.HashSet;
import java.util.Set;

public class EnumClass {
	private String name;
	private Set children;
	private Set values;
	private Set openValues; // Add open values field
	private boolean root;

	public EnumClass(String name, boolean root) {
		this.root = root;
		this.name = name;
		this.children = new HashSet<>();
		this.values = new HashSet<>();
		this.openValues = new HashSet<>(); // Initialize open values
	}

	public EnumClass(String name) {
		this.root = false;
		this.name = name;
		this.children = new HashSet<>();
		this.values = new HashSet<>();
		this.openValues = new HashSet<>(); // Initialize open values
	}

	public void addChild(EnumClass child) {
		this.children.add(child);
	}

	public void addValue(String value) {
		this.values.add(value);
	}

	public void addOpenValue(String value) {
		this.openValues.add(value);
	}

	// Method to add nested enumeration along the path
	public void addNestedEnum(String key, Set values) {
		String[] parts = key.split("\\.");
		EnumClass current = this;

		for (String part : parts) {
			boolean found = false;
			for (EnumClass child : current.children) {
				if (child.name.equals(part)) {
					current = child;
					found = true;
					break;
				}
			}

			if (!found) {
				EnumClass newChild = new EnumClass(part);
				current.addChild(newChild);
				current = newChild;
			}
		}

		current.values.addAll(values);
	}

	// Method to add open value
	public void addOpenValueToNestedEnum(String key) {
		String[] parts = key.split("\\.");
		EnumClass current = this;

		for (int i = 0; i < parts.length - 1; i++) {
			boolean found = false;
			for (EnumClass child : current.children) {
				if (child.name.equals(parts[i])) {
					current = child;
					found = true;
					break;
				}
			}

			if (!found) {
				EnumClass newChild = new EnumClass(parts[i]);
				current.addChild(newChild);
				current = newChild;
			}
		}

		current.addOpenValue(parts[parts.length - 1]);
	}

	public String exportAsJavaEnum() {
		StringBuilder sb = new StringBuilder();
		sb.append("public enum ").append(name).append(" {\n");

		if (this.root) {
			sb.append("	static final String OPEN = \"open\";\n");
		}

		if (values.isEmpty() && children.isEmpty() && openValues.isEmpty()) {
			sb.append("    " + name + ";\n");
			sb.append("}\n");
			return sb.toString();
		}

		if (values.isEmpty() && openValues.isEmpty()) {
			sb.append("    " + name + ";\n");
		}

		// Handle case where there are values but no children
		if (!values.isEmpty()) {
			boolean first = true;
			for (String value : values) {
				if (!first) {
					sb.append(",\n");
				}
				sb.append("    ").append(value);
				first = false;
			}
			sb.append(";\n");
		}

		// Handle case where there are open values
		// Handle case where there are open values
		if (!openValues.isEmpty()) {
			boolean first = true;
			for (String value : openValues) {
				if (!first) {
					sb.append(",\n");
				}
				sb.append("    ").append(value.toUpperCase()).append("(OPEN)");
				first = false;
			}
			sb.append(";\n\n");
			sb.append("    private final String value;\n\n");
			sb.append("    ").append(name).append("(String value) {\n");
			sb.append("        this.value = value;\n");
			sb.append("    }\n\n");
			sb.append("    public String getValue() {\n");
			sb.append("        return value;\n");
			sb.append("    }\n\n");
		}

		for (EnumClass child : children) {
			String childEnum = child.exportAsJavaEnum().trim();
			sb.append(childEnum.replaceAll("(?m)^", "    ")); // Adjust indent for nested enums
			sb.append("\n"); // Ensure new line after each child enum
		}

		sb.append("}\n");
		return sb.toString();
	}

	public static void main(String[] args) {
		// Example usage

		// Case 1: No values and no children
		EnumClass emptyEnum = new EnumClass("EmptyRoot");
		System.out.println(emptyEnum.exportAsJavaEnum());

		// Case 2: Values but no children
		EnumClass singleEnum = new EnumClass("SingleValueEnum");
		singleEnum.addValue("VALUE1");
		singleEnum.addValue("VALUE2");
		System.out.println(singleEnum.exportAsJavaEnum());

		// Case 3: Values and children
		EnumClass root = new EnumClass("RootEnum");
		EnumClass child1 = new EnumClass("ChildEnum1");
		EnumClass child2 = new EnumClass("ChildEnum2");

		child1.addValue("VALUE1");
		child1.addValue("VALUE2");

		child2.addValue("VALUE3");

		root.addChild(child1);
		root.addChild(child2);

		System.out.println(root.exportAsJavaEnum());

		// Case 4: Nested enums with multiple layers
		EnumClass subSubChildEnum = new EnumClass("SubSubChildEnum1");
		subSubChildEnum.addValue("SUBSUBVALUE1");
		EnumClass subSubChildEnum2 = new EnumClass("SubSubChildEnum2");
		subSubChildEnum2.addValue("SUBSUBVALUE2");

		EnumClass subChildEnum = new EnumClass("SubChildEnum1");
		subChildEnum.addValue("SUBVALUE1");
		subChildEnum.addChild(subSubChildEnum);
		subChildEnum.addChild(subSubChildEnum2);

		EnumClass childEnum = new EnumClass("ChildEnum1");
		childEnum.addValue("VALUE1");
		childEnum.addChild(subChildEnum);

		EnumClass rootEnum = new EnumClass("RootEnum");
		rootEnum.addValue("ROOTVALUE1");
		rootEnum.addChild(childEnum);

		// Output the Java enum representation
		System.out.println(rootEnum.exportAsJavaEnum());

		// Example usage of addNestedEnum method
		EnumClass enumClass = new EnumClass("MyEnum");
		Set values = new HashSet<>();
		values.add("VALUE1");
		values.add("VALUE2");
		enumClass.addNestedEnum("A.B.C", values);
		enumClass.addNestedEnum("A.", values);
		System.out.println(enumClass.exportAsJavaEnum());

		// Example usage of addOpenValueToNestedEnum method
		EnumClass enumClassWithOpenValue = new EnumClass("MyEnumWithOpenValue",true);
		enumClassWithOpenValue.addNestedEnum("A.B.C.D", values);
		enumClassWithOpenValue.addOpenValueToNestedEnum("A.B.C.OPENVALUE");
		enumClassWithOpenValue.addOpenValueToNestedEnum("A.B.C.OPENVALUE");
		enumClassWithOpenValue.addOpenValueToNestedEnum("A.B.C.OPENVALUE2");
		System.out.println(enumClassWithOpenValue.exportAsJavaEnum());
	}

	public Set getChildren() {
		return this.children;
	}

	public Object getName() {
		return this.name;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy