Skip to content

Instantly share code, notes, and snippets.

View drawers's full-sized avatar
🟩
In greenish twilight at the bottom of the Rhine

David Rawson drawers

🟩
In greenish twilight at the bottom of the Rhine
View GitHub Profile
@drawers
drawers / ModularExponent.java
Last active October 6, 2015 02:11
An implementation of a divide-and conquer method of calculating modular exponents
public class ModularExponent {
//ModularExponent
//An implementation of divide-and-conquer method of calculating modular exponents
//See Introduction to Algorithms, Corment et al.
//tested correct to (a,b,n) = (256,256,256)
public static int modularExponent(int a, int b, int n) {
if (a < 0 || b < 0 || n < 0) {
throw new IllegalArgumentException("Arguments must be positive integers");
}
class MainPresenter implements MainContract.Presenter {
private final PlayerRepository playerRepository;
private final GamesRepository gamesRepository;
private final MainContract.View view;
@Inject
MainPresenter(PlayerRepository playerRepository, GamesRepository gamesRepository, MainContract.View view) {
this.playerRepository = playerRepository;
this.gamesRepository = gamesRepository;
ListenableFuture<Games> retrieveGames(String playerName) {
return FluentFuture.from(playerRepository.retrieve(playerName))
.transformAsync( (player) -> gamesRepository.retrieve(player.id()), directExecutor())
}
Observable<Games> retrieveGames(String playerName) {
return playerRepository.retrieve(playerName)
.flatMap( (player) -> gamesRepository.retrieve(player.id()));
}
class MainRequestHelper {
private final PlayerRepository playerRepository;
private final GamesRepository gamesRepository;
@Inject
public MainRequestHelper(PlayerRepository playerRepository, GamesRepository gamesRepository, StatisticsProcessor statisticsProcessor) {
this.playerRepository = playerRepository;
this.gamesRepository = gamesRepository;
}
class MainPresenter implements MainContract.Presenter {
private final MainRequestHelper mainRequestHelper;
private final MainContract.View view;
@Inject
MainPresenter(MainRequestHelper mainRequestHelper, MainContract.View view) {
this.mainRequestHelper = mainRequestHelper;
this.view = view;
}
@ProducerModule(includes = { PlayerRepositoryModule.class, GamesRepositoryModule.class, PlayerNameModule.class } )
final class MainRequestModule {
@Produces
static ListenableFuture<Player> player(PlayerRepository playerRepository, String playerName) {
return playerRepository.retrieve(playerName);
}
@Produces
static ListenableFuture<Games> games(GamesRepository gamesRepository, Player player) {
@ProductionComponent(modules = { MainRequestModule.class, ExecutorModule.class })
interface MainRequestComponent {
ListenableFuture<Games> games();
}
void showGames(String playerName) {
MainRequestComponent component = DaggerMainRequestComponent.builder()
.playerNameModule(new PlayerNameModule(playerName))
.build();
ListenableFuture<Games> futureGames = component.games();
futureGames.addCallback(...) //todo: show games using view
}
public MainRequestHelper(PlayerRepository playerRepository, GamesRepository gamesRepository, StatisticsProcessor statisticsProcessor) {
this.playerRepository = playerRepository;
this.gamesRepository = gamesRepository;
this.statisticsProcessor = statisticsProcessor;
}
ListenableFuture<Statistics> getStatistics(String username) {
return FluentFuture.from(playerRepository.retrieve(username))
.transformAsync( (player) -> gamesRepository.retrieve(player.id()), directExecutor())
.transform( (games) -> statisticsProcessor.process(games), directExecutor());