Fix Checkstyle and PMD warnings for HazelcastEventChannel
Change-Id: I107b5083dc9d59aba1a5a7eae663e21a468cbc3b
Remove bug fix for direct pointer comparison, will fix
it later as a separate checkin.
Change-Id: I107b5083dc9d59aba1a5a7eae663e21a468cbc3b
diff --git a/src/main/java/net/onrc/onos/datagrid/HazelcastEventChannel.java b/src/main/java/net/onrc/onos/datagrid/HazelcastEventChannel.java
index 7739e83..cace751 100644
--- a/src/main/java/net/onrc/onos/datagrid/HazelcastEventChannel.java
+++ b/src/main/java/net/onrc/onos/datagrid/HazelcastEventChannel.java
@@ -1,9 +1,7 @@
package net.onrc.onos.datagrid;
import java.util.Collection;
-import java.util.HashMap;
import java.util.LinkedList;
-import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -19,55 +17,59 @@
/**
* A datagrid event channel that uses Hazelcast as a datagrid.
+ *
+ * @param <K> The class type of the key.
+ * @param <V> The class type of the value.
*/
public class HazelcastEventChannel<K, V> implements IEventChannel<K, V> {
- private HazelcastInstance hazelcastInstance; // The Hazelcast instance
- private String channelName; // The event channel name
- private Class<?> typeK; // The class type of the key
- private Class<?> typeV; // The class type of the value
- private IMap<K, byte[]> channelMap = null; // The Hazelcast channel map
+ private final HazelcastInstance hazelcastInstance; // The Hazelcast instance
+ private final String channelName; // The event channel name
+ private final Class<?> typeK; // The class type of the key
+ private final Class<?> typeV; // The class type of the value
+ private IMap<K, byte[]> channelMap; // The Hazelcast channel map
// The channel listeners
- private CopyOnWriteArrayList<IEventChannelListener<K, V>> listeners =
- new CopyOnWriteArrayList<IEventChannelListener<K, V>>();
+ private final CopyOnWriteArrayList<IEventChannelListener<K, V>> listeners =
+ new CopyOnWriteArrayList<>();
// The map entry listener
- private MapEntryListener mapEntryListener = new MapEntryListener<K>();
- private String mapListenerId = null; // The map listener ID
+ private final MapEntryListener mapEntryListener = new MapEntryListener<K>();
+ private String mapListenerId; // The map listener ID
// TODO: We should use a single global KryoFactory instance
- private KryoFactory kryoFactory = new KryoFactory();
+ private final KryoFactory kryoFactory = new KryoFactory();
// Maximum serialized event size
- private final static int MAX_BUFFER_SIZE = 64*1024;
+ private static final int MAX_BUFFER_SIZE = 64 * 1024;
/**
* Constructor for a given event channel name.
*
- * @param hazelcastInstance the Hazelcast instance to use.
- * @param channelName the event channel name.
- * @param typeK the type of the Key in the Key-Value store.
- * @param typeV the type of the Value in the Key-Value store.
+ * @param newHazelcastInstance the Hazelcast instance to use.
+ * @param newChannelName the event channel name.
+ * @param newTypeK the type of the Key in the Key-Value store.
+ * @param newTypeV the type of the Value in the Key-Value store.
*/
- public HazelcastEventChannel(HazelcastInstance hazelcastInstance,
- String channelName, Class<K> typeK,
- Class<V> typeV) {
- this.hazelcastInstance = hazelcastInstance;
- this.channelName = channelName;
- this.typeK = typeK;
- this.typeV = typeV;
+ public HazelcastEventChannel(HazelcastInstance newHazelcastInstance,
+ String newChannelName, Class<K> newTypeK,
+ Class<V> newTypeV) {
+ hazelcastInstance = newHazelcastInstance;
+ channelName = newChannelName;
+ typeK = newTypeK;
+ typeV = newTypeV;
}
/**
* Verify the key and value types of a channel.
*
- * @param typeK the type of the key to verify.
- * @param typeV the type of the value to verify.
+ * @param typeKToVerify the type of the key to verify.
+ * @param typeVToVerify the type of the value to verify.
* @return true if the key and value types of the channel match,
* otherwise false.
*/
@Override
- public boolean verifyKeyValueTypes(Class typeK, Class typeV) {
- return (this.typeK == typeK) && (this.typeV == typeV);
+ public boolean verifyKeyValueTypes(Class typeKToVerify,
+ Class typeVToVerify) {
+ return (typeK == typeKToVerify) && (typeV == typeVToVerify);
}
/**
@@ -75,7 +77,7 @@
*/
@Override
protected void finalize() {
- shutdown();
+ shutdown();
}
/**
@@ -83,11 +85,11 @@
*/
@Override
public void startup() {
- if (channelMap == null) {
- channelMap = hazelcastInstance.getMap(channelName);
- mapListenerId = channelMap.addEntryListener(mapEntryListener,
- true);
- }
+ if (channelMap == null) {
+ channelMap = hazelcastInstance.getMap(channelName);
+ mapListenerId = channelMap.addEntryListener(mapEntryListener,
+ true);
+ }
}
/**
@@ -95,11 +97,11 @@
*/
@Override
public void shutdown() {
- if (channelMap != null) {
- channelMap.removeEntryListener(mapListenerId);
- channelMap = null;
- mapListenerId = null;
- }
+ if (channelMap != null) {
+ channelMap.removeEntryListener(mapListenerId);
+ channelMap = null;
+ mapListenerId = null;
+ }
}
/**
@@ -109,9 +111,10 @@
*/
@Override
public void addListener(IEventChannelListener<K, V> listener) {
- if (listeners.contains(listener))
- return; // Nothing to do: already a listener
- listeners.add(listener);
+ if (listeners.contains(listener)) {
+ return; // Nothing to do: already a listener
+ }
+ listeners.add(listener);
}
/**
@@ -121,33 +124,33 @@
*/
@Override
public void removeListener(IEventChannelListener<K, V> listener) {
- listeners.remove(listener);
+ listeners.remove(listener);
}
/**
* Add an entry to the channel.
*
- * @param key the key of the entry to add.
+ * @param key the key of the entry to add.
* @param value the value of the entry to add.
*/
@Override
public void addEntry(K key, V value) {
- //
- // Encode the value
- //
- byte[] buffer = new byte[MAX_BUFFER_SIZE];
- Kryo kryo = kryoFactory.newKryo();
- Output output = new Output(buffer, -1);
- kryo.writeObject(output, value);
- byte[] valueBytes = output.toBytes();
- kryoFactory.deleteKryo(kryo);
+ //
+ // Encode the value
+ //
+ byte[] buffer = new byte[MAX_BUFFER_SIZE];
+ Kryo kryo = kryoFactory.newKryo();
+ Output output = new Output(buffer, -1);
+ kryo.writeObject(output, value);
+ byte[] valueBytes = output.toBytes();
+ kryoFactory.deleteKryo(kryo);
- //
- // Put the entry in the map:
- // - Key : Type <K>
- // - Value : Serialized Value (byte[])
- //
- channelMap.putAsync(key, valueBytes);
+ //
+ // Put the entry in the map:
+ // - Key : Type <K>
+ // - Value : Serialized Value (byte[])
+ //
+ channelMap.putAsync(key, valueBytes);
}
/**
@@ -157,24 +160,24 @@
*/
@Override
public void removeEntry(K key) {
- //
- // Remove the entry:
- // - Key : Type <K>
- // - Value : Serialized Value (byte[])
- //
- channelMap.removeAsync(key);
+ //
+ // Remove the entry:
+ // - Key : Type <K>
+ // - Value : Serialized Value (byte[])
+ //
+ channelMap.removeAsync(key);
}
/**
* Update an entry in the channel.
*
- * @param key the key of the entry to update.
+ * @param key the key of the entry to update.
* @param value the value of the entry to update.
*/
@Override
public void updateEntry(K key, V value) {
- // NOTE: Adding an entry with an existing key automatically updates it
- addEntry(key, value);
+ // NOTE: Adding an entry with an existing key automatically updates it
+ addEntry(key, value);
}
/**
@@ -186,19 +189,20 @@
@Override
@Deprecated
public V getEntry(K key) {
- byte[] valueBytes = channelMap.get(key);
- if (valueBytes == null)
- return null;
+ byte[] valueBytes = channelMap.get(key);
+ if (valueBytes == null) {
+ return null;
+ }
- Kryo kryo = kryoFactory.newKryo();
- //
- // Decode the value
- //
- Input input = new Input(valueBytes);
- V value = (V)kryo.readObject(input, typeV);
- kryoFactory.deleteKryo(kryo);
+ Kryo kryo = kryoFactory.newKryo();
+ //
+ // Decode the value
+ //
+ Input input = new Input(valueBytes);
+ V value = (V) kryo.readObject(input, typeV);
+ kryoFactory.deleteKryo(kryo);
- return value;
+ return value;
}
/**
@@ -209,27 +213,28 @@
@Override
@Deprecated
public Collection<V> getAllEntries() {
- Collection<V> allEntries = new LinkedList<V>();
+ Collection<V> allEntries = new LinkedList<V>();
- if (channelMap == null)
- return allEntries; // Nothing found
+ if (channelMap == null) {
+ return allEntries; // Nothing found
+ }
- //
- // Get all entries
- //
- Collection<byte[]> values = channelMap.values();
- Kryo kryo = kryoFactory.newKryo();
- for (byte[] valueBytes : values) {
- //
- // Decode the value
- //
- Input input = new Input(valueBytes);
- V value = (V)kryo.readObject(input, typeV);
- allEntries.add(value);
- }
- kryoFactory.deleteKryo(kryo);
+ //
+ // Get all entries
+ //
+ Collection<byte[]> values = channelMap.values();
+ Kryo kryo = kryoFactory.newKryo();
+ for (byte[] valueBytes : values) {
+ //
+ // Decode the value
+ //
+ Input input = new Input(valueBytes);
+ V value = (V) kryo.readObject(input, typeV);
+ allEntries.add(value);
+ }
+ kryoFactory.deleteKryo(kryo);
- return allEntries;
+ return allEntries;
}
/**
@@ -238,125 +243,125 @@
@Override
@Deprecated
public void removeAllEntries() {
- //
- // Remove all entries
- //
- // NOTE: We remove the entries one-by-one so the per-entry
- // notifications will be delivered.
- //
- // channelMap.clear();
- Set<K> keySet = channelMap.keySet();
- for (K key : keySet) {
- channelMap.removeAsync(key);
- }
+ //
+ // Remove all entries
+ //
+ // NOTE: We remove the entries one-by-one so the per-entry
+ // notifications will be delivered.
+ //
+ // channelMap.clear();
+ Set<K> keySet = channelMap.keySet();
+ for (K key : keySet) {
+ channelMap.removeAsync(key);
+ }
}
/**
* Class for receiving event notifications for the channel.
- *
+ * <p/>
* The datagrid map is:
- * - Key: Type K
- * - Value: Serialized V (byte[])
+ * - Key: Type K
+ * - Value: Serialized V (byte[])
*/
private class MapEntryListener<K> implements EntryListener<K, byte[]> {
- /**
- * Receive a notification that an entry is added.
- *
- * @param event the notification event for the entry.
- */
- @Override
- public void entryAdded(EntryEvent<K, byte[]> event) {
- //
- // Decode the value
- //
- byte[] valueBytes = event.getValue();
- Kryo kryo = kryoFactory.newKryo();
- Input input = new Input(valueBytes);
- V value = (V)kryo.readObject(input, typeV);
+ /**
+ * Receive a notification that an entry is added.
+ *
+ * @param event the notification event for the entry.
+ */
+ @Override
+ public void entryAdded(EntryEvent<K, byte[]> event) {
+ //
+ // Decode the value
+ //
+ byte[] valueBytes = event.getValue();
+ Kryo kryo = kryoFactory.newKryo();
+ Input input = new Input(valueBytes);
+ V value = (V) kryo.readObject(input, typeV);
- //
- // Deliver the notification
- //
- int index = 0;
- for (IEventChannelListener listener : listeners) {
- V copyValue = value;
- if (index++ > 0) {
- // Each listener should get a deep copy of the value
- copyValue = kryo.copy(value);
- }
- listener.entryAdded(copyValue);
- }
- kryoFactory.deleteKryo(kryo);
- }
+ //
+ // Deliver the notification
+ //
+ int index = 0;
+ for (IEventChannelListener listener : listeners) {
+ V copyValue = value;
+ if (index++ > 0) {
+ // Each listener should get a deep copy of the value
+ copyValue = kryo.copy(value);
+ }
+ listener.entryAdded(copyValue);
+ }
+ kryoFactory.deleteKryo(kryo);
+ }
- /**
- * Receive a notification that an entry is removed.
- *
- * @param event the notification event for the entry.
- */
- @Override
- public void entryRemoved(EntryEvent<K, byte[]> event) {
- //
- // Decode the value
- //
- byte[] valueBytes = event.getValue();
- Kryo kryo = kryoFactory.newKryo();
- Input input = new Input(valueBytes);
- V value = (V)kryo.readObject(input, typeV);
+ /**
+ * Receive a notification that an entry is removed.
+ *
+ * @param event the notification event for the entry.
+ */
+ @Override
+ public void entryRemoved(EntryEvent<K, byte[]> event) {
+ //
+ // Decode the value
+ //
+ byte[] valueBytes = event.getValue();
+ Kryo kryo = kryoFactory.newKryo();
+ Input input = new Input(valueBytes);
+ V value = (V) kryo.readObject(input, typeV);
- //
- // Deliver the notification
- //
- int index = 0;
- for (IEventChannelListener listener : listeners) {
- V copyValue = value;
- if (index++ > 0) {
- // Each listener should get a deep copy of the value
- copyValue = kryo.copy(value);
- }
- listener.entryRemoved(copyValue);
- }
- kryoFactory.deleteKryo(kryo);
- }
+ //
+ // Deliver the notification
+ //
+ int index = 0;
+ for (IEventChannelListener listener : listeners) {
+ V copyValue = value;
+ if (index++ > 0) {
+ // Each listener should get a deep copy of the value
+ copyValue = kryo.copy(value);
+ }
+ listener.entryRemoved(copyValue);
+ }
+ kryoFactory.deleteKryo(kryo);
+ }
- /**
- * Receive a notification that an entry is updated.
- *
- * @param event the notification event for the entry.
- */
- @Override
- public void entryUpdated(EntryEvent<K, byte[]> event) {
- //
- // Decode the value
- //
- byte[] valueBytes = event.getValue();
- Kryo kryo = kryoFactory.newKryo();
- Input input = new Input(valueBytes);
- V value = (V)kryo.readObject(input, typeV);
+ /**
+ * Receive a notification that an entry is updated.
+ *
+ * @param event the notification event for the entry.
+ */
+ @Override
+ public void entryUpdated(EntryEvent<K, byte[]> event) {
+ //
+ // Decode the value
+ //
+ byte[] valueBytes = event.getValue();
+ Kryo kryo = kryoFactory.newKryo();
+ Input input = new Input(valueBytes);
+ V value = (V) kryo.readObject(input, typeV);
- //
- // Deliver the notification
- //
- int index = 0;
- for (IEventChannelListener listener : listeners) {
- V copyValue = value;
- if (index++ > 0) {
- // Each listener should get a deep copy of the value
- copyValue = kryo.copy(value);
- }
- listener.entryUpdated(copyValue);
- }
- kryoFactory.deleteKryo(kryo);
- }
+ //
+ // Deliver the notification
+ //
+ int index = 0;
+ for (IEventChannelListener listener : listeners) {
+ V copyValue = value;
+ if (index++ > 0) {
+ // Each listener should get a deep copy of the value
+ copyValue = kryo.copy(value);
+ }
+ listener.entryUpdated(copyValue);
+ }
+ kryoFactory.deleteKryo(kryo);
+ }
- /**
- * Receive a notification that an entry is evicted.
- *
- * @param event the notification event for the entry.
- */
- @Override
- public void entryEvicted(EntryEvent<K, byte[]> event) {
- // NOTE: We don't use eviction for this map
- }
+ /**
+ * Receive a notification that an entry is evicted.
+ *
+ * @param event the notification event for the entry.
+ */
+ @Override
+ public void entryEvicted(EntryEvent<K, byte[]> event) {
+ // NOTE: We don't use eviction for this map
+ }
}
}