Jonathan Hart

Pulled internal event classes out of EventuallyConsistentMap class

for clarity.

Also switched to using Pair instead of my own Entry class for returning
entrySet().

Change-Id: I78d84f9fe931257d4ffe1d48c9d0de25f18c638f
...@@ -15,9 +15,8 @@ ...@@ -15,9 +15,8 @@
15 */ 15 */
16 package org.onosproject.store.impl; 16 package org.onosproject.store.impl;
17 17
18 -import com.google.common.base.MoreObjects;
19 -import com.google.common.collect.ImmutableList;
20 import org.apache.commons.lang3.RandomUtils; 18 import org.apache.commons.lang3.RandomUtils;
19 +import org.apache.commons.lang3.tuple.Pair;
21 import org.onlab.util.KryoNamespace; 20 import org.onlab.util.KryoNamespace;
22 import org.onosproject.cluster.ClusterService; 21 import org.onosproject.cluster.ClusterService;
23 import org.onosproject.cluster.ControllerNode; 22 import org.onosproject.cluster.ControllerNode;
...@@ -38,7 +37,6 @@ import java.util.HashMap; ...@@ -38,7 +37,6 @@ import java.util.HashMap;
38 import java.util.LinkedList; 37 import java.util.LinkedList;
39 import java.util.List; 38 import java.util.List;
40 import java.util.Map; 39 import java.util.Map;
41 -import java.util.Objects;
42 import java.util.Set; 40 import java.util.Set;
43 import java.util.concurrent.ConcurrentHashMap; 41 import java.util.concurrent.ConcurrentHashMap;
44 import java.util.concurrent.CopyOnWriteArraySet; 42 import java.util.concurrent.CopyOnWriteArraySet;
...@@ -351,7 +349,7 @@ public class EventuallyConsistentMapImpl<K, V> ...@@ -351,7 +349,7 @@ public class EventuallyConsistentMapImpl<K, V>
351 checkState(!destroyed, mapName + ERROR_DESTROYED); 349 checkState(!destroyed, mapName + ERROR_DESTROYED);
352 350
353 return items.entrySet().stream() 351 return items.entrySet().stream()
354 - .map(e -> new Entry(e.getKey(), e.getValue().value())) 352 + .map(e -> Pair.of(e.getKey(), e.getValue().value()))
355 .collect(Collectors.toSet()); 353 .collect(Collectors.toSet());
356 } 354 }
357 355
...@@ -415,49 +413,6 @@ public class EventuallyConsistentMapImpl<K, V> ...@@ -415,49 +413,6 @@ public class EventuallyConsistentMapImpl<K, V>
415 clusterCommunicator.unicast(message, peer); 413 clusterCommunicator.unicast(message, peer);
416 } 414 }
417 415
418 - private final class Entry implements Map.Entry<K, V> {
419 -
420 - private final K key;
421 - private final V value;
422 -
423 - public Entry(K key, V value) {
424 - this.key = key;
425 - this.value = value;
426 - }
427 -
428 - @Override
429 - public K getKey() {
430 - return key;
431 - }
432 -
433 - @Override
434 - public V getValue() {
435 - return value;
436 - }
437 -
438 - @Override
439 - public V setValue(V value) {
440 - throw new UnsupportedOperationException();
441 - }
442 -
443 - @Override
444 - public boolean equals(Object o) {
445 - if (!(o instanceof Map.Entry)) {
446 - return false;
447 - }
448 -
449 - Map.Entry that = (Map.Entry) o;
450 -
451 - return Objects.equals(this.key, that.getKey()) &&
452 - Objects.equals(this.value, that.getValue());
453 - }
454 -
455 - @Override
456 - public int hashCode() {
457 - return Objects.hash(key, value);
458 - }
459 - }
460 -
461 private final class SendAdvertisementTask implements Runnable { 416 private final class SendAdvertisementTask implements Runnable {
462 @Override 417 @Override
463 public void run() { 418 public void run() {
...@@ -754,112 +709,4 @@ public class EventuallyConsistentMapImpl<K, V> ...@@ -754,112 +709,4 @@ public class EventuallyConsistentMapImpl<K, V>
754 } 709 }
755 } 710 }
756 711
757 - static final class InternalPutEvent<K, V> {
758 - private final List<PutEntry<K, V>> entries;
759 -
760 - public InternalPutEvent(K key, V value, Timestamp timestamp) {
761 - entries = ImmutableList.of(new PutEntry<>(key, value, timestamp));
762 - }
763 -
764 - public InternalPutEvent(List<PutEntry<K, V>> entries) {
765 - this.entries = checkNotNull(entries);
766 - }
767 -
768 - // Needed for serialization.
769 - @SuppressWarnings("unused")
770 - private InternalPutEvent() {
771 - entries = null;
772 - }
773 -
774 - public List<PutEntry<K, V>> entries() {
775 - return entries;
776 - }
777 - }
778 -
779 - static final class PutEntry<K, V> {
780 - private final K key;
781 - private final V value;
782 - private final Timestamp timestamp;
783 -
784 - public PutEntry(K key, V value, Timestamp timestamp) {
785 - this.key = checkNotNull(key);
786 - this.value = checkNotNull(value);
787 - this.timestamp = checkNotNull(timestamp);
788 - }
789 -
790 - // Needed for serialization.
791 - @SuppressWarnings("unused")
792 - private PutEntry() {
793 - this.key = null;
794 - this.value = null;
795 - this.timestamp = null;
796 - }
797 -
798 - public K key() {
799 - return key;
800 - }
801 -
802 - public V value() {
803 - return value;
804 - }
805 -
806 - public Timestamp timestamp() {
807 - return timestamp;
808 - }
809 -
810 - public String toString() {
811 - return MoreObjects.toStringHelper(getClass())
812 - .add("key", key)
813 - .add("value", value)
814 - .add("timestamp", timestamp)
815 - .toString();
816 - }
817 - }
818 -
819 - static final class InternalRemoveEvent<K> {
820 - private final List<RemoveEntry<K>> entries;
821 -
822 - public InternalRemoveEvent(K key, Timestamp timestamp) {
823 - entries = ImmutableList.of(new RemoveEntry<>(key, timestamp));
824 - }
825 -
826 - public InternalRemoveEvent(List<RemoveEntry<K>> entries) {
827 - this.entries = checkNotNull(entries);
828 - }
829 -
830 - // Needed for serialization.
831 - @SuppressWarnings("unused")
832 - private InternalRemoveEvent() {
833 - entries = null;
834 - }
835 -
836 - public List<RemoveEntry<K>> entries() {
837 - return entries;
838 - }
839 - }
840 -
841 - static final class RemoveEntry<K> {
842 - private final K key;
843 - private final Timestamp timestamp;
844 -
845 - public RemoveEntry(K key, Timestamp timestamp) {
846 - this.key = checkNotNull(key);
847 - this.timestamp = checkNotNull(timestamp);
848 - }
849 -
850 - // Needed for serialization.
851 - @SuppressWarnings("unused")
852 - private RemoveEntry() {
853 - this.key = null;
854 - this.timestamp = null;
855 - }
856 -
857 - public K key() {
858 - return key;
859 - }
860 -
861 - public Timestamp timestamp() {
862 - return timestamp;
863 - }
864 - }
865 } 712 }
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.store.impl;
17 +
18 +import com.google.common.collect.ImmutableList;
19 +import org.onosproject.store.Timestamp;
20 +
21 +import java.util.List;
22 +
23 +import static com.google.common.base.Preconditions.checkNotNull;
24 +
25 +/**
26 + * Internal inter-instance event used by EventuallyConsistentMap for PUT events.
27 + */
28 +final class InternalPutEvent<K, V> {
29 + private final List<PutEntry<K, V>> entries;
30 +
31 + /**
32 + * Creates a put event for a single key.
33 + *
34 + * @param key key the event concerns
35 + * @param value value of the key
36 + * @param timestamp timestamp of the event
37 + */
38 + public InternalPutEvent(K key, V value, Timestamp timestamp) {
39 + entries = ImmutableList.of(new PutEntry<>(key, value, timestamp));
40 + }
41 +
42 + /**
43 + * Creates a put event for multiple keys.
44 + *
45 + * @param entries list of put entries to send an event for
46 + */
47 + public InternalPutEvent(List<PutEntry<K, V>> entries) {
48 + this.entries = checkNotNull(entries);
49 + }
50 +
51 + // Needed for serialization.
52 + @SuppressWarnings("unused")
53 + private InternalPutEvent() {
54 + entries = null;
55 + }
56 +
57 + /**
58 + * Returns the list of put entries this event concerns.
59 + *
60 + * @return list of put entries
61 + */
62 + public List<PutEntry<K, V>> entries() {
63 + return entries;
64 + }
65 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.store.impl;
17 +
18 +import com.google.common.collect.ImmutableList;
19 +import org.onosproject.store.Timestamp;
20 +
21 +import java.util.List;
22 +
23 +import static com.google.common.base.Preconditions.checkNotNull;
24 +
25 +/**
26 + * Internal inter-instance event used by EventuallyConsistentMap for REMOVE
27 + * events.
28 + */
29 +final class InternalRemoveEvent<K> {
30 + private final List<RemoveEntry<K>> entries;
31 +
32 + /**
33 + * Creates a remove event for a single key.
34 + *
35 + * @param key key the event concerns
36 + * @param timestamp timestamp of the event
37 + */
38 + public InternalRemoveEvent(K key, Timestamp timestamp) {
39 + entries = ImmutableList.of(new RemoveEntry<>(key, timestamp));
40 + }
41 +
42 + /**
43 + * Creates a remove event for multiple keys.
44 + *
45 + * @param entries list of remove entries to send an event for
46 + */
47 + public InternalRemoveEvent(List<RemoveEntry<K>> entries) {
48 + this.entries = checkNotNull(entries);
49 + }
50 +
51 + // Needed for serialization.
52 + @SuppressWarnings("unused")
53 + private InternalRemoveEvent() {
54 + entries = null;
55 + }
56 +
57 + /**
58 + * Returns the list of remove entries this event concerns.
59 + *
60 + * @return list of remove entries
61 + */
62 + public List<RemoveEntry<K>> entries() {
63 + return entries;
64 + }
65 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.store.impl;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import org.onosproject.store.Timestamp;
20 +
21 +import static com.google.common.base.Preconditions.checkNotNull;
22 +
23 +/**
24 + * Describes a single put event in an EventuallyConsistentMap.
25 + */
26 +final class PutEntry<K, V> {
27 + private final K key;
28 + private final V value;
29 + private final Timestamp timestamp;
30 +
31 + /**
32 + * Creates a new put entry.
33 + *
34 + * @param key key of the entry
35 + * @param value value of the entry
36 + * @param timestamp timestamp of the put event
37 + */
38 + public PutEntry(K key, V value, Timestamp timestamp) {
39 + this.key = checkNotNull(key);
40 + this.value = checkNotNull(value);
41 + this.timestamp = checkNotNull(timestamp);
42 + }
43 +
44 + // Needed for serialization.
45 + @SuppressWarnings("unused")
46 + private PutEntry() {
47 + this.key = null;
48 + this.value = null;
49 + this.timestamp = null;
50 + }
51 +
52 + /**
53 + * Returns the key of the entry.
54 + *
55 + * @return the key
56 + */
57 + public K key() {
58 + return key;
59 + }
60 +
61 + /**
62 + * Returns the value of the entry.
63 + *
64 + * @return the value
65 + */
66 + public V value() {
67 + return value;
68 + }
69 +
70 + /**
71 + * Returns the timestamp of the event.
72 + *
73 + * @return the timestamp
74 + */
75 + public Timestamp timestamp() {
76 + return timestamp;
77 + }
78 +
79 + @Override
80 + public String toString() {
81 + return MoreObjects.toStringHelper(getClass())
82 + .add("key", key)
83 + .add("value", value)
84 + .add("timestamp", timestamp)
85 + .toString();
86 + }
87 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.store.impl;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import org.onosproject.store.Timestamp;
20 +
21 +import static com.google.common.base.Preconditions.checkNotNull;
22 +
23 +/**
24 + * Describes a single remove event in an EventuallyConsistentMap.
25 + */
26 +final class RemoveEntry<K> {
27 + private final K key;
28 + private final Timestamp timestamp;
29 +
30 + /**
31 + * Creates a new remove entry.
32 + *
33 + * @param key key of the entry
34 + * @param timestamp timestamp of the remove event
35 + */
36 + public RemoveEntry(K key, Timestamp timestamp) {
37 + this.key = checkNotNull(key);
38 + this.timestamp = checkNotNull(timestamp);
39 + }
40 +
41 + // Needed for serialization.
42 + @SuppressWarnings("unused")
43 + private RemoveEntry() {
44 + this.key = null;
45 + this.timestamp = null;
46 + }
47 +
48 + /**
49 + * Returns the key of the entry.
50 + *
51 + * @return the key
52 + */
53 + public K key() {
54 + return key;
55 + }
56 +
57 + /**
58 + * Returns the timestamp of the event.
59 + *
60 + * @return the timestamp
61 + */
62 + public Timestamp timestamp() {
63 + return timestamp;
64 + }
65 +
66 + @Override
67 + public String toString() {
68 + return MoreObjects.toStringHelper(getClass())
69 + .add("key", key)
70 + .add("timestamp", timestamp)
71 + .toString();
72 + }
73 +}
...@@ -100,11 +100,11 @@ public class EventuallyConsistentMapImplTest { ...@@ -100,11 +100,11 @@ public class EventuallyConsistentMapImplTest {
100 .register(TestTimestamp.class) 100 .register(TestTimestamp.class)
101 // Below is the classes that the map internally registers 101 // Below is the classes that the map internally registers
102 .register(WallClockTimestamp.class) 102 .register(WallClockTimestamp.class)
103 - .register(EventuallyConsistentMapImpl.PutEntry.class) 103 + .register(PutEntry.class)
104 - .register(EventuallyConsistentMapImpl.RemoveEntry.class) 104 + .register(RemoveEntry.class)
105 .register(ArrayList.class) 105 .register(ArrayList.class)
106 - .register(EventuallyConsistentMapImpl.InternalPutEvent.class) 106 + .register(InternalPutEvent.class)
107 - .register(EventuallyConsistentMapImpl.InternalRemoveEvent.class) 107 + .register(InternalRemoveEvent.class)
108 .register(AntiEntropyAdvertisement.class) 108 .register(AntiEntropyAdvertisement.class)
109 .register(HashMap.class) 109 .register(HashMap.class)
110 .build(); 110 .build();
...@@ -586,9 +586,8 @@ public class EventuallyConsistentMapImplTest { ...@@ -586,9 +586,8 @@ public class EventuallyConsistentMapImplTest {
586 } 586 }
587 587
588 private ClusterMessage generatePutMessage(String key, String value, Timestamp timestamp) { 588 private ClusterMessage generatePutMessage(String key, String value, Timestamp timestamp) {
589 - EventuallyConsistentMapImpl.InternalPutEvent<String, String> event = 589 + InternalPutEvent<String, String> event =
590 - new EventuallyConsistentMapImpl.InternalPutEvent<>( 590 + new InternalPutEvent<>(key, value, timestamp);
591 - key, value, timestamp);
592 591
593 return new ClusterMessage( 592 return new ClusterMessage(
594 clusterService.getLocalNode().id(), PUT_MESSAGE_SUBJECT, 593 clusterService.getLocalNode().id(), PUT_MESSAGE_SUBJECT,
...@@ -596,21 +595,18 @@ public class EventuallyConsistentMapImplTest { ...@@ -596,21 +595,18 @@ public class EventuallyConsistentMapImplTest {
596 } 595 }
597 596
598 private ClusterMessage generatePutMessage(String key1, String value1, String key2, String value2) { 597 private ClusterMessage generatePutMessage(String key1, String value1, String key2, String value2) {
599 - ArrayList<EventuallyConsistentMapImpl.PutEntry<String, String>> list = new ArrayList<>(); 598 + ArrayList<PutEntry<String, String>> list = new ArrayList<>();
600 599
601 Timestamp timestamp1 = clockService.peek(1); 600 Timestamp timestamp1 = clockService.peek(1);
602 Timestamp timestamp2 = clockService.peek(2); 601 Timestamp timestamp2 = clockService.peek(2);
603 602
604 - EventuallyConsistentMapImpl.PutEntry<String, String> pe1 603 + PutEntry<String, String> pe1 = new PutEntry<>(key1, value1, timestamp1);
605 - = new EventuallyConsistentMapImpl.PutEntry<>(key1, value1, timestamp1); 604 + PutEntry<String, String> pe2 = new PutEntry<>(key2, value2, timestamp2);
606 - EventuallyConsistentMapImpl.PutEntry<String, String> pe2
607 - = new EventuallyConsistentMapImpl.PutEntry<>(key2, value2, timestamp2);
608 605
609 list.add(pe1); 606 list.add(pe1);
610 list.add(pe2); 607 list.add(pe2);
611 608
612 - EventuallyConsistentMapImpl.InternalPutEvent<String, String> event 609 + InternalPutEvent<String, String> event = new InternalPutEvent<>(list);
613 - = new EventuallyConsistentMapImpl.InternalPutEvent<>(list);
614 610
615 return new ClusterMessage( 611 return new ClusterMessage(
616 clusterService.getLocalNode().id(), PUT_MESSAGE_SUBJECT, 612 clusterService.getLocalNode().id(), PUT_MESSAGE_SUBJECT,
...@@ -618,9 +614,7 @@ public class EventuallyConsistentMapImplTest { ...@@ -618,9 +614,7 @@ public class EventuallyConsistentMapImplTest {
618 } 614 }
619 615
620 private ClusterMessage generateRemoveMessage(String key, Timestamp timestamp) { 616 private ClusterMessage generateRemoveMessage(String key, Timestamp timestamp) {
621 - EventuallyConsistentMapImpl.InternalRemoveEvent<String> event = 617 + InternalRemoveEvent<String> event = new InternalRemoveEvent<>(key, timestamp);
622 - new EventuallyConsistentMapImpl.InternalRemoveEvent<>(
623 - key, timestamp);
624 618
625 return new ClusterMessage( 619 return new ClusterMessage(
626 clusterService.getLocalNode().id(), REMOVE_MESSAGE_SUBJECT, 620 clusterService.getLocalNode().id(), REMOVE_MESSAGE_SUBJECT,
...@@ -628,21 +622,18 @@ public class EventuallyConsistentMapImplTest { ...@@ -628,21 +622,18 @@ public class EventuallyConsistentMapImplTest {
628 } 622 }
629 623
630 private ClusterMessage generateRemoveMessage(String key1, String key2) { 624 private ClusterMessage generateRemoveMessage(String key1, String key2) {
631 - ArrayList<EventuallyConsistentMapImpl.RemoveEntry<String>> list = new ArrayList<>(); 625 + ArrayList<RemoveEntry<String>> list = new ArrayList<>();
632 626
633 Timestamp timestamp1 = clockService.peek(1); 627 Timestamp timestamp1 = clockService.peek(1);
634 Timestamp timestamp2 = clockService.peek(2); 628 Timestamp timestamp2 = clockService.peek(2);
635 629
636 - EventuallyConsistentMapImpl.RemoveEntry<String> re1 630 + RemoveEntry<String> re1 = new RemoveEntry<>(key1, timestamp1);
637 - = new EventuallyConsistentMapImpl.RemoveEntry<>(key1, timestamp1); 631 + RemoveEntry<String> re2 = new RemoveEntry<>(key2, timestamp2);
638 - EventuallyConsistentMapImpl.RemoveEntry<String> re2
639 - = new EventuallyConsistentMapImpl.RemoveEntry<>(key2, timestamp2);
640 632
641 list.add(re1); 633 list.add(re1);
642 list.add(re2); 634 list.add(re2);
643 635
644 - EventuallyConsistentMapImpl.InternalRemoveEvent<String> event 636 + InternalRemoveEvent<String> event = new InternalRemoveEvent<>(list);
645 - = new EventuallyConsistentMapImpl.InternalRemoveEvent<>(list);
646 637
647 return new ClusterMessage( 638 return new ClusterMessage(
648 clusterService.getLocalNode().id(), REMOVE_MESSAGE_SUBJECT, 639 clusterService.getLocalNode().id(), REMOVE_MESSAGE_SUBJECT,
......