Skip to content

Instantly share code, notes, and snippets.

View algorythmist's full-sized avatar

Dimitri Papaioannou algorythmist

View GitHub Profile
/**
* Provides common functionality to all Rotten Tomato clients
*/
public abstract class AbstractTomatoService {
protected static final String ROTTENTOMATOES_API = "http://api.rottentomatoes.com/api/public/v1.0/";
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
{
"id": 770672122,
"title": "Toy Story 3",
"year": 2010,
"genres": ["Animation", "Kids & Family", "Science Fiction & Fantasy", "Comedy"],
"mpaa_rating": "G",
"runtime": 103,
"critics_consensus": "",
"release_dates": {
"theater": "2010-06-18",
public class Movie {
@Key
private int id;
@Key
private String title;
@Key
private List<String> genres;
public interface MovieService {
List<Movie> getBoxOfficeMovies(Locale country, int limit)
throws IOException;
List<Movie> getOpeningMovies(Locale country, int limit) throws IOException;
List<Movie> getInThreatersMovies(Locale country, int limit, int page)
public class HttpMovieService extends AbstractTomatoService implements
MovieService {
@Override
public List<Movie> getBoxOfficeMovies(Locale country, int limit)
throws IOException {
String url = ROTTENTOMATOES_API + BOX_OFFICE_MOVIES;
GenericUrl request = getMovieRequest(country, limit, url);
HttpResponse response = executeRequest(request);
MovieFeed feed = response.parseAs(MovieFeed.class);
public class MovieFeed {
@Key("movies")
public ArrayList<Movie> movies;
}
/**
* Abstraction to provide a Rotten Tomatoes API key at runtime
*
*/
public interface KeyProvider {
String getApiKey();
}
public class MovieServiceTest {
private MovieService movieService = null;
@Before
public void setUp() throws IOException {
if (movieService == null) {
KeyProvider keyProvider = new FilesystemKeyProvider(
"/Users/dimitri/dev/tomatos.properties", "key");
movieService = new HttpMovieService(keyProvider);
@algorythmist
algorythmist / EquivalenceClass.java
Last active February 5, 2020 17:35
Java equivalence class
class EquivalenceClass {
String title;
long size;
public EquivalenceClass(String title, long size) {
this.title = title;
this.size = size;
}
@Override
public boolean equals(Object o) {
@algorythmist
algorythmist / DuplicateFinder.java
Created February 5, 2020 17:36
Java Duplicate Finder
public class DuplicateFinder {
public Map<EquivalenceClass, List<Song>> findDuplicates(List<Song> songs) {
Map<EquivalenceClass, List<Song>> equivalenceClasses = new HashMap<>();
for (Song song : songs) {
EquivalenceClass equivalenceClass = new EquivalenceClass(song.getTitle(), song.getMediaInfo().getSize());
List<Song> songsInClass = equivalenceClasses.get(equivalenceClass);
if (songsInClass == null) {
songsInClass = new ArrayList<>();
equivalenceClasses.put(equivalenceClass, songsInClass);