All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hivemq.client.internal.mqtt.message.unsubscribe.MqttUnsubscribeBuilder Maven / Gradle / Ivy

Go to download

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support

The newest version!
/*
 * 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.client.internal.mqtt.message.unsubscribe;

import com.hivemq.client.internal.mqtt.datatypes.MqttTopicFilterImpl;
import com.hivemq.client.internal.mqtt.datatypes.MqttTopicFilterImplBuilder;
import com.hivemq.client.internal.mqtt.datatypes.MqttUserPropertiesImpl;
import com.hivemq.client.internal.mqtt.datatypes.MqttUserPropertiesImplBuilder;
import com.hivemq.client.internal.mqtt.message.subscribe.MqttSubscription;
import com.hivemq.client.internal.mqtt.util.MqttChecks;
import com.hivemq.client.internal.util.Checks;
import com.hivemq.client.internal.util.collections.ImmutableList;
import com.hivemq.client.mqtt.datatypes.MqttTopicFilter;
import com.hivemq.client.mqtt.mqtt5.datatypes.Mqtt5UserProperties;
import com.hivemq.client.mqtt.mqtt5.message.subscribe.Mqtt5Subscribe;
import com.hivemq.client.mqtt.mqtt5.message.unsubscribe.Mqtt5UnsubscribeBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * @author Silvio Giebl
 */
public abstract class MqttUnsubscribeBuilder> {

    private final ImmutableList.@NotNull Builder topicFiltersBuilder;
    private @NotNull MqttUserPropertiesImpl userProperties = MqttUserPropertiesImpl.NO_USER_PROPERTIES;

    MqttUnsubscribeBuilder() {
        topicFiltersBuilder = ImmutableList.builder();
    }

    MqttUnsubscribeBuilder(final @NotNull MqttUnsubscribe unsubscribe) {
        final ImmutableList topicFilters = unsubscribe.getTopicFilters();
        topicFiltersBuilder = ImmutableList.builder(topicFilters.size() + 1);
        topicFiltersBuilder.addAll(topicFilters);
    }

    abstract @NotNull B self();

    public @NotNull B addTopicFilter(final @Nullable String topicFilter) {
        topicFiltersBuilder.add(MqttTopicFilterImpl.of(topicFilter));
        return self();
    }

    public @NotNull B addTopicFilter(final @Nullable MqttTopicFilter topicFilter) {
        topicFiltersBuilder.add(MqttChecks.topicFilter(topicFilter));
        return self();
    }

    public MqttTopicFilterImplBuilder.@NotNull Nested addTopicFilter() {
        return new MqttTopicFilterImplBuilder.Nested<>(this::addTopicFilter);
    }

    public @NotNull B addTopicFilters(final @Nullable MqttTopicFilter @Nullable ... topicFilters) {
        Checks.notNull(topicFilters, "Topic Filters");
        topicFiltersBuilder.ensureFree(topicFilters.length);
        for (final MqttTopicFilter topicFilter : topicFilters) {
            addTopicFilter(topicFilter);
        }
        ensureAtLeastOneSubscription();
        return self();
    }

    public @NotNull B addTopicFilters(final @Nullable Collection<@Nullable ? extends MqttTopicFilter> topicFilters) {
        Checks.notNull(topicFilters, "Topic Filters");
        topicFiltersBuilder.ensureFree(topicFilters.size());
        topicFilters.forEach(this::addTopicFilter);
        ensureAtLeastOneSubscription();
        return self();
    }

    public @NotNull B addTopicFilters(final @Nullable Stream<@Nullable ? extends MqttTopicFilter> topicFilters) {
        Checks.notNull(topicFilters, "Topic Filters");
        topicFilters.forEach(this::addTopicFilter);
        ensureAtLeastOneSubscription();
        return self();
    }

    public @NotNull B reverse(final @Nullable Mqtt5Subscribe subscribe) {
        final ImmutableList subscriptions = MqttChecks.subscribe(subscribe).getSubscriptions();
        for (final MqttSubscription subscription : subscriptions) {
            topicFiltersBuilder.add(subscription.getTopicFilter());
        }
        return self();
    }

    public @NotNull B userProperties(final @Nullable Mqtt5UserProperties userProperties) {
        this.userProperties = MqttChecks.userProperties(userProperties);
        return self();
    }

    public MqttUserPropertiesImplBuilder.@NotNull Nested userProperties() {
        return new MqttUserPropertiesImplBuilder.Nested<>(userProperties, this::userProperties);
    }

    public @NotNull B topicFilter(final @Nullable String topicFilter) {
        return addTopicFilter(topicFilter);
    }

    public @NotNull B topicFilter(final @Nullable MqttTopicFilter topicFilter) {
        return addTopicFilter(topicFilter);
    }

    public MqttTopicFilterImplBuilder.@NotNull Nested topicFilter() {
        return new MqttTopicFilterImplBuilder.Nested<>(this::topicFilter);
    }

    private void ensureAtLeastOneSubscription() {
        Checks.state(topicFiltersBuilder.getSize() > 0, "At least one topic filter must be added.");
    }

    public @NotNull MqttUnsubscribe build() {
        ensureAtLeastOneSubscription();
        return new MqttUnsubscribe(topicFiltersBuilder.build(), userProperties);
    }

    public static class Default extends MqttUnsubscribeBuilder
            implements Mqtt5UnsubscribeBuilder.Complete, Mqtt5UnsubscribeBuilder.Start {

        public Default() {}

        Default(final @NotNull MqttUnsubscribe unsubscribe) {
            super(unsubscribe);
        }

        @Override
        @NotNull Default self() {
            return this;
        }
    }

    public static class Nested

extends MqttUnsubscribeBuilder> implements Mqtt5UnsubscribeBuilder.Nested.Complete

, Mqtt5UnsubscribeBuilder.Nested.Start

{ private final @NotNull Function parentConsumer; public Nested(final @NotNull Function parentConsumer) { this.parentConsumer = parentConsumer; } @Override @NotNull Nested

self() { return this; } @Override public @NotNull P applyUnsubscribe() { return parentConsumer.apply(build()); } } public static class Send

extends MqttUnsubscribeBuilder> implements Mqtt5UnsubscribeBuilder.Send.Complete

, Mqtt5UnsubscribeBuilder.Send.Start

{ private final @NotNull Function parentConsumer; public Send(final @NotNull Function parentConsumer) { this.parentConsumer = parentConsumer; } @Override @NotNull Send

self() { return this; } @Override public @NotNull P send() { return parentConsumer.apply(build()); } } }