Skip to content

Commit

Permalink
Minor code refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
sangupta committed Feb 23, 2016
1 parent da5e973 commit 69d9fc1
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -901,15 +901,15 @@ private final class EntryIterator implements Iterator<Entry<K, V>> {

private final Iterator<Entry<K, EvictibleEntry<K, V>>> iterator = delegate.entrySet().iterator();

private volatile Entry<K, V> ce = null;
private volatile Entry<K, V> currentEntry = null;

public EntryIterator() {
}

@Override
public Entry<K, V> next() {
this.ce = this.iterator.next().getValue();
return this.ce;
this.currentEntry = this.iterator.next().getValue();
return this.currentEntry;
}

@Override
Expand All @@ -919,12 +919,12 @@ public boolean hasNext() {

@Override
public synchronized void remove() {
if (this.ce == null) {
throw new IllegalStateException();
if (this.currentEntry == null) {
throw new IllegalStateException("There is no entry that can be removed");
}

ConcurrentMapWithTimedEvictionDecorator.this.remove(ce.getKey(), ce.getValue());
this.ce = null;
ConcurrentMapWithTimedEvictionDecorator.this.remove(currentEntry.getKey(), currentEntry.getValue());
this.currentEntry = null;
}
}

Expand Down
18 changes: 12 additions & 6 deletions src/main/java/com/stoyanr/evictor/map/EvictibleEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ public class EvictibleEntry<K, V> implements Entry<K, V> {
* if evictMs is negative
*/
EvictibleEntry(ConcurrentMapWithTimedEvictionDecorator<K, V> map, K key, V value, long evictMs) {
if (value == null)
throw new NullPointerException();
if (evictMs < 0)
throw new IllegalArgumentException();
if (value == null) {
throw new NullPointerException("Value cannot be null");
}

if (evictMs < 0) {
throw new IllegalArgumentException("Eviction time cannot be less than zero");
}

this.map = map;
this.key = key;
this.value = value;
Expand Down Expand Up @@ -124,8 +128,10 @@ public V getValue() {
*/
@Override
public synchronized V setValue(V value) {
if (value == null)
throw new NullPointerException();
if (value == null) {
throw new NullPointerException("value cannot be null");
}

V oldValue = this.value;
this.value = value;
return oldValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ public NavigableMapEvictionQueue() {
* if the map is <code>null</code>
*/
public NavigableMapEvictionQueue(ConcurrentNavigableMap<Long, EvictibleEntry<K, V>> map) {
if (map == null)
throw new NullPointerException();
if (map == null) {
throw new NullPointerException("Map instnace cannot be null");
}

this.map = map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ public PriorityEvictionQueue(int initialCapacity) {
* if the queue is <code>null</code>
*/
public PriorityEvictionQueue(Queue<EvictibleEntry<K, V>> queue) {
if (queue == null)
throw new NullPointerException();
if (queue == null) {
throw new NullPointerException("Queue instance cannot be null");
}

this.queue = queue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,20 @@ public DelayedTaskEvictionScheduler(EvictionQueue<K, V> queue) {
* @param queue
* the queue to be used
*
* @param ses
* @param executorService
* the scheduled executor service to be used
*
* @throws NullPointerException
* if either the queue or the scheduled executor service is
* <code>null</code>
*/
public DelayedTaskEvictionScheduler(EvictionQueue<K, V> queue, ScheduledExecutorService ses) {
public DelayedTaskEvictionScheduler(EvictionQueue<K, V> queue, ScheduledExecutorService executorService) {
super(queue);
if (ses == null) {
if (executorService == null) {
throw new NullPointerException("ScheduledExecutorService instance cannot be null");
}

this.ses = ses;
this.ses = executorService;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ public ExecutorServiceEvictionScheduler() {
* Creates an eviction scheduler with the specified scheduled executor
* service.
*
* @param ses
* @param executorService
* the scheduled executor service to be used
*
* @throws NullPointerException
* if the scheduled executor service is <code>null</code>
*/
public ExecutorServiceEvictionScheduler(ScheduledExecutorService ses) {
public ExecutorServiceEvictionScheduler(ScheduledExecutorService executorService) {
super();
if (ses == null) {
if (executorService == null) {
throw new NullPointerException("ScheduledExecutorService instance cannot be null");
}

this.executorService = ses;
this.executorService = executorService;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public RegularTaskEvictionScheduler(long delay, TimeUnit timeUnit) {
* implementation (see {@link AbstractQueueEvictionScheduler}) and the
* specified scheduled executor service and delay.
*
* @param ses
* @param executorService
* the scheduled executor service to be used
*
* @param delay
Expand All @@ -113,17 +113,17 @@ public RegularTaskEvictionScheduler(long delay, TimeUnit timeUnit) {
* @throws IllegalArgumentException
* if delay is less than or equal to zero
*/
public RegularTaskEvictionScheduler(ScheduledExecutorService ses, long delay, TimeUnit timeUnit) {
public RegularTaskEvictionScheduler(ScheduledExecutorService executorService, long delay, TimeUnit timeUnit) {
super();
if (ses == null) {
if (executorService == null) {
throw new NullPointerException("ScheduledExecutorService instance cannot be null");
}

if (delay <= 0) {
throw new IllegalArgumentException("Delay cannot be less than or equal to zero");
}

this.executorService = ses;
this.executorService = executorService;
this.delay = delay;
this.timeUnit = timeUnit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public class SingleThreadEvictionScheduler<K, V> extends AbstractQueueEvictionSc

private volatile long next = 0;

private final Thread t = new Thread(new EvictionThread());
private final Thread evictionThread = new Thread(new EvictionThread());

private final Object m = new Object();
private final Object mutex = new Object();

/**
* Creates a single thread eviction scheduler with the default queue
Expand All @@ -60,7 +60,7 @@ public class SingleThreadEvictionScheduler<K, V> extends AbstractQueueEvictionSc
*/
public SingleThreadEvictionScheduler() {
super();
t.start();
evictionThread.start();
}

/**
Expand All @@ -76,7 +76,7 @@ public SingleThreadEvictionScheduler() {
*/
public SingleThreadEvictionScheduler(EvictionQueue<K, V> queue) {
super(queue);
t.start();
evictionThread.start();
}

/**
Expand All @@ -89,9 +89,9 @@ public SingleThreadEvictionScheduler(EvictionQueue<K, V> queue) {
@Override
public void shutdown() {
finished = true;
t.interrupt();
evictionThread.interrupt();
try {
t.join();
evictionThread.join();
} catch (InterruptedException e) {
// TODO: add this
}
Expand All @@ -109,9 +109,9 @@ public void shutdown() {
@Override
protected void onScheduleEviction(EvictibleEntry<K, V> e) {
if (getNextEvictionTime() != next) {
synchronized (m) {
synchronized (mutex) {
notified = true;
m.notifyAll();
mutex.notifyAll();
}
}
}
Expand All @@ -128,9 +128,9 @@ protected void onScheduleEviction(EvictibleEntry<K, V> e) {
@Override
protected void onCancelEviction(EvictibleEntry<K, V> e) {
if (getNextEvictionTime() != next) {
synchronized (m) {
synchronized (mutex) {
notified = true;
m.notifyAll();
mutex.notifyAll();
}
}
}
Expand Down Expand Up @@ -199,9 +199,9 @@ private long calcTimeout(long time) {
private boolean waitFor(long timeout) {
boolean result = true;
try {
synchronized (m) {
synchronized (mutex) {
notified = false;
m.wait(timeout / 1000000, (int) (timeout % 1000000));
mutex.wait(timeout / 1000000, (int) (timeout % 1000000));
result = !notified;
}
} catch (InterruptedException e) {
Expand Down

0 comments on commit 69d9fc1

Please sign in to comment.