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

com.hivemq.client.internal.util.collections.ImmutableEmptyList 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.util.collections;

import com.hivemq.client.annotations.Immutable;
import com.hivemq.client.internal.util.Checks;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.function.Consumer;

/**
 * @author Silvio Giebl
 */
@Immutable class ImmutableEmptyList implements ImmutableList {

    private static final @NotNull ImmutableEmptyList INSTANCE = new ImmutableEmptyList();
    private static final @NotNull Object @NotNull [] EMPTY = {};

    static  @NotNull ImmutableList of() {
        //noinspection unchecked
        return (ImmutableList) ImmutableEmptyList.INSTANCE;
    }

    private ImmutableEmptyList() {}

    @Override
    public int size() {
        return 0;
    }

    @Override
    public @NotNull Object get(final int index) {
        throw new IndexOutOfBoundsException("Empty list");
    }

    @Override
    public @NotNull Object @NotNull [] toArray() {
        return EMPTY;
    }

    @Override
    public  T @NotNull [] toArray(final T @NotNull [] other) {
        Checks.notNull(other, "Array");
        if (other.length > 0) {
            other[0] = null;
        }
        return other;
    }

    @Override
    public int indexOf(final @Nullable Object o) {
        return -1;
    }

    @Override
    public int lastIndexOf(final @Nullable Object o) {
        return -1;
    }

    @Override
    public @NotNull ImmutableListIterator listIterator(final int index) {
        Checks.cursorIndex(index, 0);
        return EmptyIterator.of();
    }

    @Override
    public @NotNull Spliterator spliterator() {
        return EmptySpliterator.of();
    }

    @Override
    public void forEach(final @Nullable Consumer consumer) {
        Checks.notNull(consumer, "Consumer");
    }

    @Override
    public @NotNull ImmutableList subList(final int fromIndex, final int toIndex) {
        Checks.indexRange(fromIndex, toIndex, 0);
        return this;
    }

    @Override
    public boolean equals(final @Nullable Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof List)) {
            return false;
        }
        return ((List) o).size() == 0;
    }

    @Override
    public int hashCode() {
        return 1;
    }

    @Override
    public @NotNull String toString() {
        return "[]";
    }

    private static class EmptyIterator implements ImmutableListIterator {

        private static final @NotNull EmptyIterator INSTANCE = new EmptyIterator();

        static  @NotNull ImmutableListIterator of() {
            //noinspection unchecked
            return (ImmutableListIterator) INSTANCE;
        }

        private EmptyIterator() {}

        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public @NotNull Object next() {
            throw new NoSuchElementException();
        }

        @Override
        public int nextIndex() {
            return 0;
        }

        @Override
        public boolean hasPrevious() {
            return false;
        }

        @Override
        public @NotNull Object previous() {
            throw new NoSuchElementException();
        }

        @Override
        public int previousIndex() {
            return -1;
        }

        @Override
        public void forEachRemaining(final @Nullable Consumer consumer) {
            Checks.notNull(consumer, "Consumer");
        }
    }

    private static class EmptySpliterator implements Spliterator {

        private static final @NotNull EmptySpliterator INSTANCE = new EmptySpliterator();

        static  @NotNull Spliterator of() {
            //noinspection unchecked
            return (Spliterator) INSTANCE;
        }

        private EmptySpliterator() {}

        @Override
        public boolean tryAdvance(final @Nullable Consumer consumer) {
            Checks.notNull(consumer, "Consumer");
            return false;
        }

        @Override
        public @Nullable Spliterator trySplit() {
            return null;
        }

        @Override
        public long estimateSize() {
            return 0;
        }

        @Override
        public long getExactSizeIfKnown() {
            return 0;
        }

        @Override
        public int characteristics() {
            return Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.IMMUTABLE | Spliterator.NONNULL |
                    Spliterator.DISTINCT | Spliterator.ORDERED;
        }

        @Override
        public void forEachRemaining(final @Nullable Consumer consumer) {
            Checks.notNull(consumer, "Consumer");
        }
    }
}