Skip to content

Commit

Permalink
Clean up code in directory inbox class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Børlum committed Nov 20, 2011
1 parent b3848df commit a28a318
Showing 1 changed file with 13 additions and 12 deletions.
Expand Up @@ -39,6 +39,7 @@
import java.util.concurrent.TimeUnit;

import static com.trifork.stamdata.Preconditions.checkArgument;
import static com.trifork.stamdata.Preconditions.checkState;

/**
* Uses a file system directory as inbox.
Expand All @@ -63,7 +64,7 @@
*/
public class DirectoryInbox implements Inbox
{
private static final ConcurrentMap<String, DirectoryState> sizeHistory = new MapMaker().expireAfterAccess(10, TimeUnit.MINUTES).makeMap();
private static final ConcurrentMap<String, InboxState> sizeHistory = new MapMaker().expireAfterAccess(10, TimeUnit.MINUTES).makeMap();

private final File inboxDirectory;
private final File lockFile;
Expand Down Expand Up @@ -101,18 +102,18 @@ public class DirectoryInbox implements Inbox
@Override
public File top()
{
Preconditions.checkState(!isLocked(), "The inbox is locked.");
checkState(!isLocked(), "The inbox is locked.");

return dataSets.peek();
}

@Override
public void advance() throws IOException
{
Preconditions.checkState(!isLocked(), "The inbox is locked.");
checkState(!isLocked(), "The inbox is locked.");

File element = top();
Preconditions.checkState(element != null, "You must not call advance when no data sets are present.");
checkState(element != null, "You must not call advance when no data sets are present.");

// First we have to successfully delete the
// directory physically.
Expand All @@ -136,15 +137,15 @@ public void advance() throws IOException
@Override
public int readyCount()
{
Preconditions.checkState(!isLocked(), "The inbox is locked.");
checkState(!isLocked(), "The inbox is locked.");

return dataSets.size();
}

@Override
public void update() throws IOException
{
Preconditions.checkState(!isLocked(), "The inbox is locked.");
checkState(!isLocked(), "The inbox is locked.");

final Ordering FILENAME_ORDERING = Ordering.usingToString();

Expand All @@ -167,8 +168,8 @@ public void update() throws IOException
//
if (element.isFile()) continue;

DirectoryState previousState = sizeHistory.get(element.getPath());
DirectoryState currentState = createState(element);
InboxState previousState = sizeHistory.get(element.getPath());
InboxState currentState = createState(element);

if (previousState == null || currentState.size != previousState.size)
{
Expand Down Expand Up @@ -215,18 +216,18 @@ public boolean isLocked()
return lockFile.exists();
}

private static DirectoryState createState(File directory)
private static InboxState createState(File directory)
{
checkArgument(directory.isDirectory(), "Cannot create a snapshot state of anything but a directory.");
checkArgument(directory.isDirectory(), "Cannot create a snapshot state of anything but a directory. file=%s", directory.getAbsolutePath());

DirectoryState state = new DirectoryState();
InboxState state = new InboxState();
state.size = FileUtils.sizeOfDirectory(directory);
state.timestamp = Instant.now();

return state;
}

private static class DirectoryState
private static class InboxState
{
Long size = null;
Instant timestamp = null;
Expand Down

0 comments on commit a28a318

Please sign in to comment.