Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

[Final] Fixing null zinc repo index bug. #20

Merged
merged 7 commits into from Nov 15, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java
Expand Up @@ -6,6 +6,8 @@

import java.io.*;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* User: NachoSoto
* Date: 9/3/13
Expand Down Expand Up @@ -45,16 +47,26 @@ public ZincRepoIndex getIndex() {

private ZincRepoIndex initializeIndex() {
try {
return mGson.fromJson(new FileReader(mIndexFile), ZincRepoIndex.class);
return checkNotNull(readRepoIndexFile());
} catch (FileNotFoundException fnfe) {
try {
mIndexFile.getParentFile().mkdirs();
mIndexFile.createNewFile();

return new ZincRepoIndex();
} catch (IOException ioe) {
throw new ZincRuntimeException("Error creating index file", ioe);
}
return createNewIndexFile();
} catch (NullPointerException npe) {
return createNewIndexFile();
}
}

private ZincRepoIndex readRepoIndexFile() throws FileNotFoundException {
return mGson.fromJson(new FileReader(mIndexFile), ZincRepoIndex.class);
}

private ZincRepoIndex createNewIndexFile() {
try {
mIndexFile.getParentFile().mkdirs();
mIndexFile.createNewFile();

return new ZincRepoIndex();
} catch (IOException ioe) {
throw new ZincRuntimeException("Error creating index file", ioe);
}
}

Expand Down