import static java.util.Objects.requireNonNull;
-import java.io.Serial;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
* duplicate registration or two different components within the same process trying to register a Candidate.
*/
public class CandidateAlreadyRegisteredException extends Exception {
- @Serial
+ @java.io.Serial
private static final long serialVersionUID = 1L;
private final @NonNull GenericEntity<?> entity;
* Gets the entity for which a Candidate has already been registered in the current process.
*
* @param <T> the instance identifier path type
- *
* @return the entity.
*/
@SuppressWarnings("unchecked")
*/
package org.opendaylight.mdsal.eos.common.api;
-import static com.google.common.base.Preconditions.checkArgument;
-
import com.google.common.collect.ImmutableMap;
+import org.eclipse.jdt.annotation.NonNull;
/**
* Enumerates the ownership change states for an entity.
static {
final var builder = ImmutableMap.<Key, EntityOwnershipChangeState>builder();
- for (final EntityOwnershipChangeState e : values()) {
+ for (var e : values()) {
builder.put(new Key(e.wasOwner, e.isOwner, e.hasOwner), e);
}
BY_KEY = builder.build();
return name() + " [wasOwner=" + wasOwner + ", isOwner=" + isOwner + ", hasOwner=" + hasOwner + "]";
}
- public static EntityOwnershipChangeState from(
- final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
- final EntityOwnershipChangeState state = BY_KEY.get(new Key(wasOwner, isOwner, hasOwner));
- checkArgument(state != null, "Invalid combination of wasOwner: %s, isOwner: %s, hasOwner: %s",
- wasOwner, isOwner, hasOwner);
- return state;
- }
-
- private static final class Key {
- private final boolean wasOwner;
- private final boolean isOwner;
- private final boolean hasOwner;
-
- Key(final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
- this.wasOwner = wasOwner;
- this.isOwner = isOwner;
- this.hasOwner = hasOwner;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + Boolean.hashCode(hasOwner);
- result = prime * result + Boolean.hashCode(isOwner);
- result = prime * result + Boolean.hashCode(wasOwner);
- return result;
+ public static @NonNull EntityOwnershipChangeState from(final boolean wasOwner, final boolean isOwner,
+ final boolean hasOwner) {
+ final var state = BY_KEY.get(new Key(wasOwner, isOwner, hasOwner));
+ if (state != null) {
+ return state;
}
+ throw new IllegalArgumentException("Invalid combination of wasOwner: %s, isOwner: %s, hasOwner: %s".formatted(
+ wasOwner, isOwner, hasOwner));
+ }
- @Override
- public boolean equals(final Object obj) {
- return obj == this || obj instanceof Key other
- && hasOwner == other.hasOwner && isOwner == other.isOwner && wasOwner == other.wasOwner;
- }
+ private record Key(boolean wasOwner, boolean isOwner, boolean hasOwner) {
+ // Nothing else
}
}
*/
package org.opendaylight.mdsal.eos.common.api;
+import org.eclipse.jdt.annotation.NonNull;
+
/**
* Enumerates the current ownership state for an entity.
*
*/
NO_OWNER;
- public static EntityOwnershipState from(boolean isOwner, boolean hasOwner) {
+ public static @NonNull EntityOwnershipState from(final boolean isOwner, final boolean hasOwner) {
if (isOwner) {
return IS_OWNER;
- } else if (hasOwner) {
- return OWNED_BY_OTHER;
- } else {
- return NO_OWNER;
}
+ return hasOwner ? OWNED_BY_OTHER : NO_OWNER;
}
}
import static java.util.Objects.requireNonNull;
-import java.io.Serial;
import java.io.Serializable;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
*
* <p>
*
- * @author Thomas Pantelis
- *
* @param <T> the entity identifier type
+ * @author Thomas Pantelis
*/
public class GenericEntity<T extends HierarchicalIdentifier<T>> implements Serializable, Identifiable<T> {
- @Serial
+ @java.io.Serial
private static final long serialVersionUID = 1L;
private final @NonNull String type;
* @return the id.
*/
@Override
- public final @NonNull T getIdentifier() {
+ public final T getIdentifier() {
return id;
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
* @author Thomas Pantelis
*/
public class EntityOwnershipChangeStateTest {
-
@Test
- public void testFromWithValid() throws Exception {
+ public void testFromWithValid() {
assertEquals("from(false, true, true)", EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED,
EntityOwnershipChangeState.from(false, true, true));
assertEquals("from(true, false, true)", EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NEW_OWNER,
EntityOwnershipChangeState.from(true, true, true));
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void testFromWithInvalidFalseTrueFalse() {
- EntityOwnershipChangeState.from(false, true, false);
+ assertThrows(IllegalArgumentException.class, () -> EntityOwnershipChangeState.from(false, true, false));
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void testFromWithInvalidTrueTrueFalse() {
- EntityOwnershipChangeState.from(true, true, false);
+ assertThrows(IllegalArgumentException.class, () -> EntityOwnershipChangeState.from(true, true, false));
}
@Test
- public void basicTest() throws Exception {
- EntityOwnershipChangeState entityOwnershipChangeState = EntityOwnershipChangeState.from(false, true, true);
+ public void basicTest() {
+ final var entityOwnershipChangeState = EntityOwnershipChangeState.from(false, true, true);
assertTrue(entityOwnershipChangeState.hasOwner());
assertTrue(entityOwnershipChangeState.isOwner());
assertFalse(entityOwnershipChangeState.wasOwner());
*/
package org.opendaylight.mdsal.eos.common.api;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
-import org.junit.Test;
-
-public class EntityOwnershipStateTest {
+import org.junit.jupiter.api.Test;
+class EntityOwnershipStateTest {
@Test
- public void fromTest() throws Exception {
- assertEquals(EntityOwnershipState.NO_OWNER, EntityOwnershipState.from(false, false));
- assertEquals(EntityOwnershipState.IS_OWNER, EntityOwnershipState.from(true, false));
- assertEquals(EntityOwnershipState.OWNED_BY_OTHER, EntityOwnershipState.from(false, true));
+ void fromTest() {
+ assertSame(EntityOwnershipState.NO_OWNER, EntityOwnershipState.from(false, false));
+ assertSame(EntityOwnershipState.IS_OWNER, EntityOwnershipState.from(true, false));
+ assertSame(EntityOwnershipState.OWNED_BY_OTHER, EntityOwnershipState.from(false, true));
}
}
\ No newline at end of file
*/
package org.opendaylight.mdsal.eos.common.api;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
-public class GenericEntityOwnershipChangeTest {
+@ExtendWith(MockitoExtension.class)
+class GenericEntityOwnershipChangeTest {
+ @Mock
+ private GenericEntity<?> genericEntity;
@Test
- public void basicTest() throws Exception {
- final GenericEntity genericEntity = mock(GenericEntity.class);
+ void basicTest() {
doReturn("testEntity").when(genericEntity).toString();
- final GenericEntityOwnershipChange genericEntityOwnershipChange =
- new GenericEntityOwnershipChange(genericEntity, EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED);
+ final var genericEntityOwnershipChange = new GenericEntityOwnershipChange<>(genericEntity,
+ EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED);
assertEquals(genericEntity, genericEntityOwnershipChange.getEntity());
assertEquals(EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED, genericEntityOwnershipChange.getState());
*/
package org.opendaylight.mdsal.eos.common.api;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
-import java.io.Serial;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
-public class GenericEntityTest {
-
+class GenericEntityTest {
@Test
- public void basicTest() throws Exception {
- final TestClass testClass = new TestClass();
- final GenericEntity<?> genericEntity = new GenericEntity<>("testType", testClass);
- final GenericEntity<?> genericEntityDiff = new GenericEntity<>("differentTestType", new TestClassDiff());
+ void basicTest() {
+ final var testClass = new TestClass();
+ final var genericEntity = new GenericEntity<>("testType", testClass);
+ final var genericEntityDiff = new GenericEntity<>("differentTestType", new TestClassDiff());
assertEquals(TestClass.class, genericEntity.getIdentifier().getClass());
assertEquals("testType", genericEntity.getType());
}
private static final class TestClass implements HierarchicalIdentifier<TestClass> {
- @Serial
+ @java.io.Serial
private static final long serialVersionUID = 1L;
@Override
}
private static final class TestClassDiff implements HierarchicalIdentifier<TestClassDiff> {
- @Serial
+ @java.io.Serial
private static final long serialVersionUID = 1L;
@Override