Skip to content

Instantly share code, notes, and snippets.

Avatar

David Rawson drawers

  • ClearPoint
  • Auckland, New Zealand
View GitHub Profile
@drawers
drawers / ModularExponent.java
Last active Oct 6, 2015
An implementation of a divide-and conquer method of calculating modular exponents
View ModularExponent.java
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");
}
View RetrieveGamesRx.java
Observable<Games> retrieveGames(String playerName) {
return playerRepository.retrieve(playerName)
.flatMap( (player) -> gamesRepository.retrieve(player.id()));
}
View RetrieveGamesGuava.java
ListenableFuture<Games> retrieveGames(String playerName) {
return FluentFuture.from(playerRepository.retrieve(playerName))
.transformAsync( (player) -> gamesRepository.retrieve(player.id()), directExecutor())
}
View MainPresenter1.java
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;
View MainRequestHelper1.java
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;
}
View MainPresenter2.java
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;
}
View ProducerModule1.java
@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) {
View MainRequestComponent1.java
@ProductionComponent(modules = { MainRequestModule.class, ExecutorModule.class })
interface MainRequestComponent {
ListenableFuture<Games> games();
}
View ShowGames1.java
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
}
View MainRequestHelper2.java
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());