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

org.patternfly.component.HasItems Maven / Gradle / Ivy

There is a newer version: 0.2.11
Show newest version
/*
 *  Copyright 2023 Red Hat
 *
 *  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
 *
 *      https://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 org.patternfly.component;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import org.jboss.elemento.IsElement;
import org.jboss.elemento.TypedBuilder;

import elemental2.dom.Element;

/**
 * The HasItems interface represents a component that can contain a collection of items.
 * It provides methods for adding, removing, and manipulating items within the component.
 *
 * @param  the element type of the main component
 * @param  the builder type of the main component
 * @param  the type of the subcomponent (representing an item)
 */
public interface HasItems, S>
        extends Iterable, TypedBuilder, IsElement {

    default  B addItems(Iterable items, Function display) {
        for (T item : items) {
            S subComponent = display.apply(item);
            addItem(subComponent);
        }
        return that();
    }

    default B addItem(S item) {
        return add(item);
    }

    B add(S item);

    default List items() {
        List items = new ArrayList<>();
        this.forEach(items::add);
        return items;
    }

    int size();

    boolean isEmpty();

    void clear();
}