Posts

Showing posts from August 16, 2018

Processor for handling generic commands

Image
Clash Royale CLAN TAG #URR8PPP .everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0; up vote 2 down vote favorite I'm trying to implement business logic layer based on concepts of commands and command handlers. A command is a thing that contains input parameters for executing some action, and it knows what kind of output that action should produce. A command handler contains logic for actually executing an action: it accepts a command as a param, handles it in some way, and (if successful) produces an output object. public interface ICommand<TResultData> public interface ICommandHandler<TCommand, TResultData> where TCommand : ICommand<TResultData> CommandResult<TResultData> Handle(TCommand command); public static class CommandProcessor public static CommandResult<TResultData> Process<TCommand, TResultData>(TCommand command) where TCommand : ICommand<TResultData> var handler = Servi

Computing an approximate value of Pi via Monte Carlo method in Java with streams

Image
Clash Royale CLAN TAG #URR8PPP .everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0; up vote 4 down vote favorite I have this short program that attempts to compute an approximate value of $pi$: package net.coderodde.fun; import java.awt.geom.Point2D; import java.util.Arrays; import java.util.Objects; import java.util.Random; import java.util.stream.Collectors; import java.util.stream.IntStream; /** * This class computes an approximate value of Pi. * * @author Rodion "rodde" Efremov * @version 1.6 (Feb 23, 2018) */ public class MonteCarloPiComputer /** * The default radius of the simulated circle. */ private static final double DEFAULT_RADIUS = 0.5; /** * The random number generator. */ private final Random random; public MonteCarloPiComputer(Random random) this.random = Objects.requireNonNull( random, "The input random number generator is null."); public MonteCarloPiComputer() this(new Random(