Fix warnings in sal-akka-raft test classes
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / utils / InMemoryJournal.java
index 790952883d1b83ea02e2335d6f029454c86f063a..f2a216c821d43098ad3c43454a1111439c818756 100644 (file)
@@ -21,7 +21,6 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
-import java.util.concurrent.Callable;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
@@ -42,7 +41,7 @@ public class InMemoryJournal extends AsyncWriteJournal {
         final CountDownLatch latch;
         final Class<?> ofType;
 
-        public WriteMessagesComplete(int count, Class<?> ofType) {
+        WriteMessagesComplete(int count, Class<?> ofType) {
             this.latch = new CountDownLatch(count);
             this.ofType = ofType;
         }
@@ -50,47 +49,47 @@ public class InMemoryJournal extends AsyncWriteJournal {
 
     static final Logger LOG = LoggerFactory.getLogger(InMemoryJournal.class);
 
-    private static final Map<String, Map<Long, Object>> journals = new ConcurrentHashMap<>();
+    private static final Map<String, Map<Long, Object>> JOURNALS = new ConcurrentHashMap<>();
 
-    private static final Map<String, CountDownLatch> deleteMessagesCompleteLatches = new ConcurrentHashMap<>();
+    private static final Map<String, CountDownLatch> DELETE_MESSAGES_COMPLETE_LATCHES = new ConcurrentHashMap<>();
 
-    private static final Map<String, WriteMessagesComplete> writeMessagesComplete = new ConcurrentHashMap<>();
+    private static final Map<String, WriteMessagesComplete> WRITE_MESSAGES_COMPLETE = new ConcurrentHashMap<>();
 
-    private static final Map<String, CountDownLatch> blockReadMessagesLatches = new ConcurrentHashMap<>();
+    private static final Map<String, CountDownLatch> BLOCK_READ_MESSAGES_LATCHES = new ConcurrentHashMap<>();
 
     private static Object deserialize(Object data) {
         return data instanceof byte[] ? SerializationUtils.deserialize((byte[])data) : data;
     }
 
     public static void addEntry(String persistenceId, long sequenceNr, Object data) {
-        Map<Long, Object> journal = journals.get(persistenceId);
-        if(journal == null) {
+        Map<Long, Object> journal = JOURNALS.get(persistenceId);
+        if (journal == null) {
             journal = Maps.newLinkedHashMap();
-            journals.put(persistenceId, journal);
+            JOURNALS.put(persistenceId, journal);
         }
 
         synchronized (journal) {
-            journal.put(sequenceNr, data instanceof Serializable ?
-                    SerializationUtils.serialize((Serializable) data) : data);
+            journal.put(sequenceNr, data instanceof Serializable
+                    SerializationUtils.serialize((Serializable) data) : data);
         }
     }
 
     public static void clear() {
-        journals.clear();
+        JOURNALS.clear();
     }
 
     @SuppressWarnings("unchecked")
     public static <T> List<T> get(String persistenceId, Class<T> type) {
-        Map<Long, Object> journalMap = journals.get(persistenceId);
-        if(journalMap == null) {
+        Map<Long, Object> journalMap = JOURNALS.get(persistenceId);
+        if (journalMap == null) {
             return Collections.<T>emptyList();
         }
 
         synchronized (journalMap) {
             List<T> journal = new ArrayList<>(journalMap.size());
-            for(Object entry: journalMap.values()) {
+            for (Object entry: journalMap.values()) {
                 Object data = deserialize(entry);
-                if(type.isInstance(data)) {
+                if (type.isInstance(data)) {
                     journal.add((T) data);
                 }
             }
@@ -100,16 +99,16 @@ public class InMemoryJournal extends AsyncWriteJournal {
     }
 
     public static Map<Long, Object> get(String persistenceId) {
-        Map<Long, Object> journalMap = journals.get(persistenceId);
+        Map<Long, Object> journalMap = JOURNALS.get(persistenceId);
         return journalMap != null ? journalMap : Collections.<Long, Object>emptyMap();
     }
 
     public static void dumpJournal(String persistenceId) {
         StringBuilder builder = new StringBuilder(String.format("Journal log for %s:", persistenceId));
-        Map<Long, Object> journalMap = journals.get(persistenceId);
-        if(journalMap != null) {
+        Map<Long, Object> journalMap = JOURNALS.get(persistenceId);
+        if (journalMap != null) {
             synchronized (journalMap) {
-                for(Map.Entry<Long, Object> e: journalMap.entrySet()) {
+                for (Map.Entry<Long, Object> e: journalMap.entrySet()) {
                     builder.append("\n    ").append(e.getKey()).append(" = ").append(e.getValue());
                 }
             }
@@ -119,31 +118,33 @@ public class InMemoryJournal extends AsyncWriteJournal {
     }
 
     public static void waitForDeleteMessagesComplete(String persistenceId) {
-        if(!Uninterruptibles.awaitUninterruptibly(deleteMessagesCompleteLatches.get(persistenceId), 5, TimeUnit.SECONDS)) {
+        if (!Uninterruptibles.awaitUninterruptibly(DELETE_MESSAGES_COMPLETE_LATCHES.get(persistenceId),
+                5, TimeUnit.SECONDS)) {
             throw new AssertionError("Delete messages did not complete");
         }
     }
 
     public static void waitForWriteMessagesComplete(String persistenceId) {
-        if(!Uninterruptibles.awaitUninterruptibly(writeMessagesComplete.get(persistenceId).latch, 5, TimeUnit.SECONDS)) {
+        if (!Uninterruptibles.awaitUninterruptibly(WRITE_MESSAGES_COMPLETE.get(persistenceId).latch,
+                5, TimeUnit.SECONDS)) {
             throw new AssertionError("Journal write messages did not complete");
         }
     }
 
     public static void addDeleteMessagesCompleteLatch(String persistenceId) {
-        deleteMessagesCompleteLatches.put(persistenceId, new CountDownLatch(1));
+        DELETE_MESSAGES_COMPLETE_LATCHES.put(persistenceId, new CountDownLatch(1));
     }
 
     public static void addWriteMessagesCompleteLatch(String persistenceId, int count) {
-        writeMessagesComplete.put(persistenceId, new WriteMessagesComplete(count, null));
+        WRITE_MESSAGES_COMPLETE.put(persistenceId, new WriteMessagesComplete(count, null));
     }
 
     public static void addWriteMessagesCompleteLatch(String persistenceId, int count, Class<?> ofType) {
-        writeMessagesComplete.put(persistenceId, new WriteMessagesComplete(count, ofType));
+        WRITE_MESSAGES_COMPLETE.put(persistenceId, new WriteMessagesComplete(count, ofType));
     }
 
     public static void addBlockReadMessagesLatch(String persistenceId, CountDownLatch latch) {
-        blockReadMessagesLatches.put(persistenceId, latch);
+        BLOCK_READ_MESSAGES_LATCHES.put(persistenceId, latch);
     }
 
     @Override
@@ -151,33 +152,30 @@ public class InMemoryJournal extends AsyncWriteJournal {
             final long toSequenceNr, final long max, final Consumer<PersistentRepr> replayCallback) {
         LOG.trace("doAsyncReplayMessages for {}: fromSequenceNr: {}, toSequenceNr: {}", persistenceId,
                 fromSequenceNr,toSequenceNr);
-        return Futures.future(new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                CountDownLatch blockLatch = blockReadMessagesLatches.remove(persistenceId);
-                if(blockLatch != null) {
-                    Uninterruptibles.awaitUninterruptibly(blockLatch);
-                }
+        return Futures.future(() -> {
+            CountDownLatch blockLatch = BLOCK_READ_MESSAGES_LATCHES.remove(persistenceId);
+            if (blockLatch != null) {
+                Uninterruptibles.awaitUninterruptibly(blockLatch);
+            }
 
-                Map<Long, Object> journal = journals.get(persistenceId);
-                if (journal == null) {
-                    return null;
-                }
+            Map<Long, Object> journal = JOURNALS.get(persistenceId);
+            if (journal == null) {
+                return null;
+            }
 
-                synchronized (journal) {
-                    int count = 0;
-                    for (Map.Entry<Long,Object> entry : journal.entrySet()) {
-                        if (++count <= max && entry.getKey() >= fromSequenceNr && entry.getKey() <= toSequenceNr) {
-                            PersistentRepr persistentMessage =
-                                    new PersistentImpl(deserialize(entry.getValue()), entry.getKey(), persistenceId,
-                                            null, false, null, null);
-                            replayCallback.accept(persistentMessage);
-                        }
+            synchronized (journal) {
+                int count = 0;
+                for (Map.Entry<Long,Object> entry : journal.entrySet()) {
+                    if (++count <= max && entry.getKey() >= fromSequenceNr && entry.getKey() <= toSequenceNr) {
+                        PersistentRepr persistentMessage =
+                                new PersistentImpl(deserialize(entry.getValue()), entry.getKey(), persistenceId,
+                                        null, false, null, null);
+                        replayCallback.accept(persistentMessage);
                     }
                 }
-
-                return null;
             }
+
+            return null;
         }, context().dispatcher());
     }
 
@@ -186,15 +184,15 @@ public class InMemoryJournal extends AsyncWriteJournal {
         LOG.trace("doAsyncReadHighestSequenceNr for {}: fromSequenceNr: {}", persistenceId, fromSequenceNr);
 
         // Akka calls this during recovery.
-        Map<Long, Object> journal = journals.get(persistenceId);
-        if(journal == null) {
+        Map<Long, Object> journal = JOURNALS.get(persistenceId);
+        if (journal == null) {
             return Futures.successful(fromSequenceNr);
         }
 
         synchronized (journal) {
             long highest = -1;
             for (Long seqNr : journal.keySet()) {
-                if(seqNr.longValue() >= fromSequenceNr && seqNr.longValue() > highest) {
+                if (seqNr.longValue() >= fromSequenceNr && seqNr.longValue() > highest) {
                     highest = seqNr.longValue();
                 }
             }
@@ -205,51 +203,48 @@ public class InMemoryJournal extends AsyncWriteJournal {
 
     @Override
     public Future<Iterable<Optional<Exception>>> doAsyncWriteMessages(final Iterable<AtomicWrite> messages) {
-        return Futures.future(new Callable<Iterable<Optional<Exception>>>() {
-            @Override
-            public Iterable<Optional<Exception>> call() throws Exception {
-                for (AtomicWrite write : messages) {
-                    // Copy to array - workaround for eclipse "ambiguous method" errors for toIterator, toIterable etc
-                    PersistentRepr[] array = new PersistentRepr[write.payload().size()];
-                    write.payload().copyToArray(array);
-                    for(PersistentRepr repr: array) {
-                        LOG.trace("doAsyncWriteMessages: id: {}: seqNr: {}, payload: {}", repr.persistenceId(),
-                            repr.sequenceNr(), repr.payload());
-
-                        addEntry(repr.persistenceId(), repr.sequenceNr(), repr.payload());
-
-                        WriteMessagesComplete complete = writeMessagesComplete.get(repr.persistenceId());
-                        if(complete != null) {
-                            if(complete.ofType == null || complete.ofType.equals(repr.payload().getClass())) {
-                                complete.latch.countDown();
-                            }
+        return Futures.future(() -> {
+            for (AtomicWrite write : messages) {
+                // Copy to array - workaround for eclipse "ambiguous method" errors for toIterator, toIterable etc
+                PersistentRepr[] array = new PersistentRepr[write.payload().size()];
+                write.payload().copyToArray(array);
+                for (PersistentRepr repr: array) {
+                    LOG.trace("doAsyncWriteMessages: id: {}: seqNr: {}, payload: {}", repr.persistenceId(),
+                        repr.sequenceNr(), repr.payload());
+
+                    addEntry(repr.persistenceId(), repr.sequenceNr(), repr.payload());
+
+                    WriteMessagesComplete complete = WRITE_MESSAGES_COMPLETE.get(repr.persistenceId());
+                    if (complete != null) {
+                        if (complete.ofType == null || complete.ofType.equals(repr.payload().getClass())) {
+                            complete.latch.countDown();
                         }
                     }
                 }
-
-                return Collections.emptyList();
             }
+
+            return Collections.emptyList();
         }, context().dispatcher());
     }
 
     @Override
     public Future<Void> doAsyncDeleteMessagesTo(String persistenceId, long toSequenceNr) {
         LOG.trace("doAsyncDeleteMessagesTo: {}", toSequenceNr);
-        Map<Long, Object> journal = journals.get(persistenceId);
-        if(journal != null) {
+        Map<Long, Object> journal = JOURNALS.get(persistenceId);
+        if (journal != null) {
             synchronized (journal) {
                 Iterator<Long> iter = journal.keySet().iterator();
-                while(iter.hasNext()) {
-                    Long n = iter.next();
-                    if(n <= toSequenceNr) {
+                while (iter.hasNext()) {
+                    Long num = iter.next();
+                    if (num <= toSequenceNr) {
                         iter.remove();
                     }
                 }
             }
         }
 
-        CountDownLatch latch = deleteMessagesCompleteLatches.get(persistenceId);
-        if(latch != null) {
+        CountDownLatch latch = DELETE_MESSAGES_COMPLETE_LATCHES.get(persistenceId);
+        if (latch != null) {
             latch.countDown();
         }