Madan Jampani
Committed by Gerrit Code Review

Added Change utility class for representing change events + Moved Match class to org.onlab.util

Change-Id: I08e8cd8dd92983bd2764e83016b1abc0bf29388f
1 +/*
2 + * Copyright 2016 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.event;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import com.google.common.base.Objects;
20 +
21 +/**
22 + * Generic representation of an update.
23 + *
24 + * @param <T> type of value that was updated
25 + */
26 +public class Change<T> {
27 +
28 + private final T oldValue;
29 + private final T newValue;
30 +
31 + public Change(T oldValue, T newValue) {
32 + this.oldValue = oldValue;
33 + this.newValue = newValue;
34 + }
35 +
36 + /**
37 + * Returns previous value.
38 + * @return previous value.
39 + */
40 + public T oldValue() {
41 + return oldValue;
42 + }
43 +
44 + /**
45 + * Returns new or current value.
46 + * @return new value.
47 + */
48 + public T newValue() {
49 + return newValue;
50 + }
51 +
52 + @Override
53 + public boolean equals(Object other) {
54 + if (other == null || !(other instanceof Change)) {
55 + return false;
56 + }
57 + Change<T> that = (Change<T>) other;
58 + return Objects.equal(this.oldValue, that.oldValue) &&
59 + Objects.equal(this.newValue, that.newValue);
60 + }
61 +
62 + @Override
63 + public int hashCode() {
64 + return Objects.hashCode(oldValue, newValue);
65 + }
66 +
67 + @Override
68 + public String toString() {
69 + return MoreObjects.toStringHelper(getClass())
70 + .add("oldValue", oldValue)
71 + .add("newValue", newValue)
72 + .toString();
73 + }
74 +}
1 +package org.onosproject.event;
2 +
3 +import org.junit.Test;
4 +
5 +import com.google.common.testing.EqualsTester;
6 +
7 +import static org.junit.Assert.*;
8 +
9 +/**
10 + * Unit tests for {@link Change}.
11 + */
12 +public class ChangeTest {
13 +
14 + @Test
15 + public void getters() {
16 + Change<String> change = new Change<>("a", "b");
17 + assertEquals("a", change.oldValue());
18 + assertEquals("b", change.newValue());
19 + }
20 +
21 + @Test
22 + public void equality() {
23 + new EqualsTester()
24 + .addEqualityGroup(new Change<>("foo", "bar"),
25 + new Change<>("foo", "bar"))
26 + .addEqualityGroup(new Change<>("bar", "car"))
27 + .testEquals();
28 + }
29 +}
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 16
17 package org.onosproject.store.consistent.impl; 17 package org.onosproject.store.consistent.impl;
18 18
19 +import org.onlab.util.Match;
19 import org.onosproject.store.service.Transaction; 20 import org.onosproject.store.service.Transaction;
20 import org.onosproject.store.service.Versioned; 21 import org.onosproject.store.service.Versioned;
21 22
......
...@@ -19,6 +19,7 @@ package org.onosproject.store.consistent.impl; ...@@ -19,6 +19,7 @@ package org.onosproject.store.consistent.impl;
19 import java.nio.ByteBuffer; 19 import java.nio.ByteBuffer;
20 20
21 import org.onlab.util.KryoNamespace; 21 import org.onlab.util.KryoNamespace;
22 +import org.onlab.util.Match;
22 import org.onosproject.cluster.NodeId; 23 import org.onosproject.cluster.NodeId;
23 import org.onosproject.store.serializers.KryoNamespaces; 24 import org.onosproject.store.serializers.KryoNamespaces;
24 import org.onosproject.store.serializers.KryoSerializer; 25 import org.onosproject.store.serializers.KryoSerializer;
......
...@@ -20,6 +20,8 @@ import net.kuujo.copycat.state.Command; ...@@ -20,6 +20,8 @@ import net.kuujo.copycat.state.Command;
20 import net.kuujo.copycat.state.Initializer; 20 import net.kuujo.copycat.state.Initializer;
21 import net.kuujo.copycat.state.Query; 21 import net.kuujo.copycat.state.Query;
22 import net.kuujo.copycat.state.StateContext; 22 import net.kuujo.copycat.state.StateContext;
23 +
24 +import org.onlab.util.Match;
23 import org.onosproject.store.service.Transaction; 25 import org.onosproject.store.service.Transaction;
24 import org.onosproject.store.service.Versioned; 26 import org.onosproject.store.service.Versioned;
25 27
......
...@@ -22,6 +22,7 @@ import com.google.common.cache.LoadingCache; ...@@ -22,6 +22,7 @@ import com.google.common.cache.LoadingCache;
22 import com.google.common.collect.Maps; 22 import com.google.common.collect.Maps;
23 23
24 import org.onlab.util.HexString; 24 import org.onlab.util.HexString;
25 +import org.onlab.util.Match;
25 import org.onlab.util.SharedExecutors; 26 import org.onlab.util.SharedExecutors;
26 import org.onlab.util.Tools; 27 import org.onlab.util.Tools;
27 import org.onosproject.core.ApplicationId; 28 import org.onosproject.core.ApplicationId;
......
...@@ -17,12 +17,15 @@ ...@@ -17,12 +17,15 @@
17 package org.onosproject.store.consistent.impl; 17 package org.onosproject.store.consistent.impl;
18 18
19 import com.google.common.collect.Sets; 19 import com.google.common.collect.Sets;
20 +
20 import net.kuujo.copycat.resource.internal.AbstractResource; 21 import net.kuujo.copycat.resource.internal.AbstractResource;
21 import net.kuujo.copycat.resource.internal.ResourceManager; 22 import net.kuujo.copycat.resource.internal.ResourceManager;
22 import net.kuujo.copycat.state.StateMachine; 23 import net.kuujo.copycat.state.StateMachine;
23 import net.kuujo.copycat.state.internal.DefaultStateMachine; 24 import net.kuujo.copycat.state.internal.DefaultStateMachine;
24 import net.kuujo.copycat.util.concurrent.Futures; 25 import net.kuujo.copycat.util.concurrent.Futures;
25 import net.kuujo.copycat.util.function.TriConsumer; 26 import net.kuujo.copycat.util.function.TriConsumer;
27 +
28 +import org.onlab.util.Match;
26 import org.onosproject.store.service.Transaction; 29 import org.onosproject.store.service.Transaction;
27 import org.onosproject.store.service.Versioned; 30 import org.onosproject.store.service.Versioned;
28 31
......
...@@ -21,8 +21,11 @@ import com.google.common.collect.ImmutableList; ...@@ -21,8 +21,11 @@ import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.ImmutableSet; 21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.collect.Lists; 22 import com.google.common.collect.Lists;
23 import com.google.common.collect.Maps; 23 import com.google.common.collect.Maps;
24 +
24 import net.kuujo.copycat.state.Initializer; 25 import net.kuujo.copycat.state.Initializer;
25 import net.kuujo.copycat.state.StateContext; 26 import net.kuujo.copycat.state.StateContext;
27 +
28 +import org.onlab.util.Match;
26 import org.onosproject.store.service.DatabaseUpdate; 29 import org.onosproject.store.service.DatabaseUpdate;
27 import org.onosproject.store.service.Transaction; 30 import org.onosproject.store.service.Transaction;
28 import org.onosproject.store.service.Versioned; 31 import org.onosproject.store.service.Versioned;
......
...@@ -20,9 +20,12 @@ import com.google.common.collect.ImmutableList; ...@@ -20,9 +20,12 @@ import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Lists; 20 import com.google.common.collect.Lists;
21 import com.google.common.collect.Maps; 21 import com.google.common.collect.Maps;
22 import com.google.common.collect.Sets; 22 import com.google.common.collect.Sets;
23 +
23 import net.kuujo.copycat.Task; 24 import net.kuujo.copycat.Task;
24 import net.kuujo.copycat.cluster.Cluster; 25 import net.kuujo.copycat.cluster.Cluster;
25 import net.kuujo.copycat.resource.ResourceState; 26 import net.kuujo.copycat.resource.ResourceState;
27 +
28 +import org.onlab.util.Match;
26 import org.onosproject.store.service.DatabaseUpdate; 29 import org.onosproject.store.service.DatabaseUpdate;
27 import org.onosproject.store.service.Transaction; 30 import org.onosproject.store.service.Transaction;
28 import org.onosproject.store.service.Versioned; 31 import org.onosproject.store.service.Versioned;
......
...@@ -32,6 +32,7 @@ import java.util.function.Consumer; ...@@ -32,6 +32,7 @@ import java.util.function.Consumer;
32 import org.junit.After; 32 import org.junit.After;
33 import org.junit.Before; 33 import org.junit.Before;
34 import org.junit.Test; 34 import org.junit.Test;
35 +import org.onlab.util.Match;
35 import org.onosproject.core.ApplicationId; 36 import org.onosproject.core.ApplicationId;
36 import org.onosproject.core.DefaultApplicationId; 37 import org.onosproject.core.DefaultApplicationId;
37 import org.onosproject.store.service.Serializer; 38 import org.onosproject.store.service.Serializer;
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 -package org.onosproject.store.consistent.impl; 16 +package org.onlab.util;
17 17
18 import static com.google.common.base.MoreObjects.toStringHelper; 18 import static com.google.common.base.MoreObjects.toStringHelper;
19 19
...@@ -28,16 +28,21 @@ import java.util.function.Function; ...@@ -28,16 +28,21 @@ import java.util.function.Function;
28 */ 28 */
29 public final class Match<T> { 29 public final class Match<T> {
30 30
31 + public static final Match ANY = new Match<>();
32 + public static final Match NULL = new Match<>(null, false);
33 + public static final Match NOT_NULL = new Match<>(null, true);
34 +
31 private final boolean matchAny; 35 private final boolean matchAny;
32 private final T value; 36 private final T value;
37 + private final boolean negation;
33 38
34 /** 39 /**
35 - * Returns a Match that matches any value. 40 + * Returns a Match that matches any value including null.
36 * @param <T> match type 41 * @param <T> match type
37 * @return new instance 42 * @return new instance
38 */ 43 */
39 public static <T> Match<T> any() { 44 public static <T> Match<T> any() {
40 - return new Match<>(); 45 + return ANY;
41 } 46 }
42 47
43 /** 48 /**
...@@ -46,27 +51,48 @@ public final class Match<T> { ...@@ -46,27 +51,48 @@ public final class Match<T> {
46 * @return new instance 51 * @return new instance
47 */ 52 */
48 public static <T> Match<T> ifNull() { 53 public static <T> Match<T> ifNull() {
49 - return ifValue(null); 54 + return NULL;
55 + }
56 +
57 + /**
58 + * Returns a Match that matches all non-null values.
59 + * @param <T> match type
60 + * @return new instance
61 + */
62 + public static <T> Match<T> ifNotNull() {
63 + return NOT_NULL;
50 } 64 }
51 65
52 /** 66 /**
53 - * Returns a Match that matches only specified value. 67 + * Returns a Match that only matches the specified value.
54 * @param value value to match 68 * @param value value to match
55 * @param <T> match type 69 * @param <T> match type
56 * @return new instance 70 * @return new instance
57 */ 71 */
58 public static <T> Match<T> ifValue(T value) { 72 public static <T> Match<T> ifValue(T value) {
59 - return new Match<>(value); 73 + return new Match<>(value, false);
74 + }
75 +
76 + /**
77 + * Returns a Match that matches any value except the specified value.
78 + * @param value value to not match
79 + * @param <T> match type
80 + * @return new instance
81 + */
82 + public static <T> Match<T> ifNotValue(T value) {
83 + return new Match<>(value, true);
60 } 84 }
61 85
62 private Match() { 86 private Match() {
63 matchAny = true; 87 matchAny = true;
88 + negation = false;
64 value = null; 89 value = null;
65 } 90 }
66 91
67 - private Match(T value) { 92 + private Match(T value, boolean negation) {
68 matchAny = false; 93 matchAny = false;
69 this.value = value; 94 this.value = value;
95 + this.negation = negation;
70 } 96 }
71 97
72 /** 98 /**
...@@ -79,9 +105,9 @@ public final class Match<T> { ...@@ -79,9 +105,9 @@ public final class Match<T> {
79 if (matchAny) { 105 if (matchAny) {
80 return any(); 106 return any();
81 } else if (value == null) { 107 } else if (value == null) {
82 - return ifNull(); 108 + return negation ? ifNotNull() : ifNull();
83 } else { 109 } else {
84 - return ifValue(mapper.apply(value)); 110 + return negation ? ifNotValue(mapper.apply(value)) : ifValue(mapper.apply(value));
85 } 111 }
86 } 112 }
87 113
...@@ -94,21 +120,21 @@ public final class Match<T> { ...@@ -94,21 +120,21 @@ public final class Match<T> {
94 if (matchAny) { 120 if (matchAny) {
95 return true; 121 return true;
96 } else if (other == null) { 122 } else if (other == null) {
97 - return value == null; 123 + return negation ? value != null : value == null;
98 } else { 124 } else {
99 if (value instanceof byte[]) { 125 if (value instanceof byte[]) {
100 - return Arrays.equals((byte[]) value, (byte[]) other); 126 + boolean equal = Arrays.equals((byte[]) value, (byte[]) other);
127 + return negation ? !equal : equal;
101 } 128 }
102 - return Objects.equals(value, other); 129 + return negation ? !Objects.equals(value, other) : Objects.equals(value, other);
103 } 130 }
104 } 131 }
105 132
106 @Override 133 @Override
107 public int hashCode() { 134 public int hashCode() {
108 - return Objects.hash(matchAny, value); 135 + return Objects.hash(matchAny, value, negation);
109 } 136 }
110 137
111 - @SuppressWarnings("unchecked")
112 @Override 138 @Override
113 public boolean equals(Object other) { 139 public boolean equals(Object other) {
114 if (!(other instanceof Match)) { 140 if (!(other instanceof Match)) {
...@@ -116,13 +142,15 @@ public final class Match<T> { ...@@ -116,13 +142,15 @@ public final class Match<T> {
116 } 142 }
117 Match<T> that = (Match<T>) other; 143 Match<T> that = (Match<T>) other;
118 return Objects.equals(this.matchAny, that.matchAny) && 144 return Objects.equals(this.matchAny, that.matchAny) &&
119 - Objects.equals(this.value, that.value); 145 + Objects.equals(this.value, that.value) &&
146 + Objects.equals(this.negation, that.negation);
120 } 147 }
121 148
122 @Override 149 @Override
123 public String toString() { 150 public String toString() {
124 return toStringHelper(this) 151 return toStringHelper(this)
125 .add("matchAny", matchAny) 152 .add("matchAny", matchAny)
153 + .add("negation", negation)
126 .add("value", value) 154 .add("value", value)
127 .toString(); 155 .toString();
128 } 156 }
......
...@@ -54,6 +54,7 @@ import java.util.stream.StreamSupport; ...@@ -54,6 +54,7 @@ import java.util.stream.StreamSupport;
54 54
55 import org.slf4j.Logger; 55 import org.slf4j.Logger;
56 56
57 +import com.google.common.base.Charsets;
57 import com.google.common.base.Strings; 58 import com.google.common.base.Strings;
58 import com.google.common.primitives.UnsignedLongs; 59 import com.google.common.primitives.UnsignedLongs;
59 import com.google.common.util.concurrent.ThreadFactoryBuilder; 60 import com.google.common.util.concurrent.ThreadFactoryBuilder;
...@@ -210,6 +211,20 @@ public abstract class Tools { ...@@ -210,6 +211,20 @@ public abstract class Tools {
210 } 211 }
211 212
212 /** 213 /**
214 + * Returns the UTF-8 encoded byte[] representation of a String.
215 + */
216 + public static byte[] getBytesUtf8(String input) {
217 + return input.getBytes(Charsets.UTF_8);
218 + }
219 +
220 + /**
221 + * Returns the String representation of UTF-8 encoded byte[].
222 + */
223 + public static String toStringUtf8(byte[] input) {
224 + return new String(input, Charsets.UTF_8);
225 + }
226 +
227 + /**
213 * Returns a copy of the input byte array. 228 * Returns a copy of the input byte array.
214 * 229 *
215 * @param original input 230 * @param original input
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 -package org.onosproject.store.consistent.impl; 16 +package org.onlab.util;
17 17
18 import static junit.framework.TestCase.assertEquals; 18 import static junit.framework.TestCase.assertEquals;
19 import static junit.framework.TestCase.assertFalse; 19 import static junit.framework.TestCase.assertFalse;
......