Fixes based on code review feedback
This commit is contained in:
@ -57,7 +57,7 @@ public class App {
|
||||
executorService.scheduleAtFixedRate(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
World world = World.getInstance();
|
||||
World world = new World();
|
||||
List<String> countries = world.fetch();
|
||||
System.out.println("Our world currently has the following countries:-");
|
||||
for (String country : countries) {
|
||||
|
@ -15,23 +15,11 @@ import java.util.List;
|
||||
*/
|
||||
public class DataFetcher {
|
||||
|
||||
private static DataFetcher df;
|
||||
private final String filename = "world.txt";
|
||||
private long lastFetched = -1;
|
||||
private long lastFetched;
|
||||
|
||||
private DataFetcher() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Init.
|
||||
*
|
||||
* @return DataFetcher instance
|
||||
*/
|
||||
public static DataFetcher getInstance() {
|
||||
if (df == null) {
|
||||
df = new DataFetcher();
|
||||
}
|
||||
return df;
|
||||
public DataFetcher() {
|
||||
this.lastFetched = -1;
|
||||
}
|
||||
|
||||
private boolean isDirty(long fileLastModified) {
|
||||
@ -66,6 +54,6 @@ public class DataFetcher {
|
||||
return data;
|
||||
}
|
||||
|
||||
return null;
|
||||
return new ArrayList<String>();
|
||||
}
|
||||
}
|
||||
|
@ -12,22 +12,12 @@ import java.util.List;
|
||||
*/
|
||||
public class World {
|
||||
|
||||
private static World world;
|
||||
private static List<String> countries = new ArrayList<String>();
|
||||
private List<String> countries;
|
||||
private DataFetcher df;
|
||||
|
||||
private World() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Init.
|
||||
*
|
||||
* @return World instance
|
||||
*/
|
||||
public static World getInstance() {
|
||||
if (world == null) {
|
||||
world = new World();
|
||||
}
|
||||
return world;
|
||||
public World() {
|
||||
this.countries = new ArrayList<String>();
|
||||
this.df = new DataFetcher();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -37,10 +27,9 @@ public class World {
|
||||
* @return List of strings
|
||||
*/
|
||||
public List<String> fetch() {
|
||||
DataFetcher df = DataFetcher.getInstance();
|
||||
List<String> data = df.fetch();
|
||||
|
||||
countries = data == null ? countries : data;
|
||||
countries = data.isEmpty() ? countries : data;
|
||||
|
||||
return countries;
|
||||
}
|
||||
|
@ -24,10 +24,8 @@ package org.dirty.flag;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.iluwatar.dirtyflag.DataFetcher;
|
||||
@ -39,23 +37,16 @@ import com.iluwatar.dirtyflag.DataFetcher;
|
||||
*/
|
||||
public class DirtyFlagTest {
|
||||
|
||||
@BeforeEach
|
||||
public void reset() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
|
||||
Field instance = DataFetcher.class.getDeclaredField("df");
|
||||
instance.setAccessible(true);
|
||||
instance.set(null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDirty() {
|
||||
DataFetcher df = DataFetcher.getInstance();
|
||||
DataFetcher df = new DataFetcher();
|
||||
List<String> countries = df.fetch();
|
||||
assertTrue(!countries.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsNotDirty() {
|
||||
DataFetcher df = DataFetcher.getInstance();
|
||||
DataFetcher df = new DataFetcher();
|
||||
df.fetch();
|
||||
List<String> countries = df.fetch();
|
||||
assertTrue(countries == null);
|
||||
|
Reference in New Issue
Block a user