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

com.github.bogdanovmn.projecteuler.framework.ProblemDescription Maven / Gradle / Ivy

Go to download

This is a lite framework for solving of math problems of "Project Euler" (https://projecteuler.net)

There is a newer version: 0.5.2
Show newest version
package com.github.bogdanovmn.projecteuler.framework;

import org.reflections.Reflections;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Set;

public class ProblemDescription {
	private final String[] inputArgs;

	public ProblemDescription(String... inputArgs) {
		if (inputArgs.length < 1) {
			throw new IllegalArgumentException("At least one parameter must be specified: the problem number");
		}
		this.inputArgs = inputArgs;
	}

	private int number() {
		return Integer.parseInt(inputArgs[0]);
	}

	private String[] parameters() {
		return Arrays.copyOfRange(inputArgs, 1, inputArgs.length);
	}

	public Problem problem() {
		Set> annotated = new Reflections("").getTypesAnnotatedWith(ProjectEulerProblem.class);
		Class aClass = annotated.stream()
			.filter(cl ->
				cl.getAnnotation(ProjectEulerProblem.class).number() == number()
			)
			.filter(
				cl -> cl.getSuperclass().equals(Problem.class)
			)
			.findFirst().orElseThrow(
				() -> new IllegalArgumentException(
					String.format("Can't find any class for the problem #%d", number())
				)
			);
		try {
			return (Problem) aClass.getConstructor(ProblemParameters.class)
				.newInstance(
					new ProblemParameters(parameters())
				);
		}
		catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
			e.printStackTrace();
			throw new IllegalStateException(
				String.format(
					"Can't invoke a constructor with '%s' arg",
					ProblemParameters.class.getName()
				)
			);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy