diff --git a/src/main/java/com/hivemq/client2/util/TypeSwitch.java b/src/main/java/com/hivemq/client2/util/TypeSwitch.java deleted file mode 100644 index 32a6c6090..000000000 --- a/src/main/java/com/hivemq/client2/util/TypeSwitch.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2018-present HiveMQ and the HiveMQ Community - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hivemq.client2.util; - -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.function.Consumer; - -/** - * Util to enable switching over types. - *

- * Example: - *

- * {@code
- * Mqtt5MessageException e;
- * TypeSwitch.when(e)
- *         .is(Mqtt5ConnAckException.class, c -> System.out.println(c.getMqttMessage().getReasonCode()))
- *         .is(Mqtt5DisconnectException.class, d -> System.out.println(d.getMqttMessage().getServerReference()));
- * }
- * 
- * - * @param the super type to switch over. - * @author Silvio Giebl - */ -@ApiStatus.NonExtendable -public interface TypeSwitch { - - /** - * Returns a TypeSwitch object which does not match any type. - * - * @param the super type to switch over. - * @return the TypeSwitch object. - */ - static @NotNull TypeSwitch never() { - //noinspection unchecked - return (TypeSwitch) Never.INSTANCE; - } - - /** - * Returns a TypeSwitch object for switching over an object of type T. - * - * @param t the object of type T. - * @param the super type to switch over. - * @return the TypeSwitch object. - */ - static @NotNull TypeSwitch when(final @Nullable T t) { - return (t == null) ? never() : new TypeSwitch.Default<>(t); - } - - /** - * Checks if the object that is switched over is of a given type and if so executes a callback. - *

- * If the type matches the returned TypeSwitch will not match any further type. - * - * @param type the class of the type to check. - * @param consumer the callback to execute if the type matches. - * @param the type to check - * @return the TypeSwitch object. - */ - @NotNull TypeSwitch is(final @NotNull Class type, final @NotNull Consumer consumer); - - class Default implements TypeSwitch { - - private final @Nullable T t; - - Default(final @Nullable T t) { - this.t = t; - } - - @Override - public @NotNull TypeSwitch is( - final @NotNull Class type, final @NotNull Consumer consumer) { - - if (type.isInstance(t)) { - //noinspection unchecked - consumer.accept((I) t); - return never(); - } - return this; - } - } - - class Never implements TypeSwitch { - - static final @NotNull Never INSTANCE = new Never(); - - private Never() {} - - @Override - public @NotNull Never is(final @NotNull Class type, final @NotNull Consumer consumer) { - return this; - } - } -} diff --git a/src/test/java/com/hivemq/client2/util/TypeSwitchTest.java b/src/test/java/com/hivemq/client2/util/TypeSwitchTest.java deleted file mode 100644 index 638b2c10a..000000000 --- a/src/test/java/com/hivemq/client2/util/TypeSwitchTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2018-present HiveMQ and the HiveMQ Community - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hivemq.client2.util; - -import org.junit.jupiter.api.Test; - -import java.util.concurrent.atomic.AtomicInteger; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * @author Silvio Giebl - */ -class TypeSwitchTest { - - @Test - void never_alwaysSame() { - final TypeSwitch never1 = TypeSwitch.never(); - final TypeSwitch never2 = TypeSwitch.never(); - assertSame(never1, never2); - } - - @Test - void when_true() { - final Interface i = new Impl1(); - final AtomicInteger counter = new AtomicInteger(); - TypeSwitch.when(i).is(Impl1.class, impl1 -> counter.incrementAndGet()); - assertEquals(1, counter.get()); - } - - @Test - void when_false() { - final Interface i = new Impl2(); - final AtomicInteger counter = new AtomicInteger(); - TypeSwitch.when(i).is(Impl1.class, impl1 -> counter.incrementAndGet()); - assertEquals(0, counter.get()); - } - - @Test - void when_true_false() { - final Interface i = new Impl1(); - final AtomicInteger counter = new AtomicInteger(); - TypeSwitch.when(i).is(Impl1.class, impl1 -> counter.incrementAndGet()).is(Impl2.class, impl2 -> fail()); - assertEquals(1, counter.get()); - } - - @Test - void when_false_true() { - final Interface i = new Impl1(); - final AtomicInteger counter = new AtomicInteger(); - TypeSwitch.when(i).is(Impl2.class, impl2 -> fail()).is(Impl1.class, impl1 -> counter.incrementAndGet()); - assertEquals(1, counter.get()); - } - - private interface Interface {} - - private static class Impl1 implements Interface {} - - private static class Impl2 implements Interface {} -} \ No newline at end of file