Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Rules repository implementation to read data from JSON file
package com.karollotkowski.cleancode.repositories;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.karollotkowski.cleancode.core.Rule;
import io.dropwizard.jackson.Jackson;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class JsonFileRulesRepository implements RulesRepository {
private static final String RULES_FILE = "rules.json";
private static final ObjectMapper MAPPER = Jackson.newObjectMapper();
private final List<Rule> rules;
public JsonFileRulesRepository() {
rules = fetchRules();
System.out.println("Number of defined rules: " + rules.size());
public Optional<Rule> random() {
return Optional.ofNullable(rules.iterator().next());
private List<Rule> fetchRules() {
final URL rulesResource = getClass().getClassLoader().getResource(RULES_FILE);
try {
return MAPPER.readValue(rulesResource, new TypeReference<ArrayList<Rule>>() {});
} catch (IOException e) {
return Collections.emptyList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment