<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 org.apache.isis.commons.internal.collections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.springframework.lang.Nullable;

import org.apache.isis.commons.internal.base._NullSafe;

import lombok.NonNull;
import lombok.val;

/**
 * <h1>- internal use only -</h1>
 * <p>
 * Common List creation idioms.
 * </p>
 * <p>
 * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
 * These may be changed or removed without notice!
 * </p>
 *
 * @since 2.0
 */
public final class _Lists {

    private _Lists(){}

    // -- LIST ACCESS

    public static <T> T lastElementIfAny(final @Nullable List<T> list) {
        if(_NullSafe.isEmpty(list)) {
            return null;
        }
        return list.get(list.size()-1);
    }

    public static <T> Optional<T> lastElement(final @Nullable List<T> list) {
        if(_NullSafe.isEmpty(list)) {
            return Optional.empty();
        }
        return Optional.ofNullable(list.get(list.size()-1));
    }

    // -- LIST CONCATENATION

    /**
     * Returns an unmodifiable list containing all elements from given list
     * and the specified element.
     */
    public static <T> List<T> append(final List<T> list, final T element) {
        val resultList = new ArrayList<T>(list.size() + 1);
        resultList.addAll(list);
        resultList.add(element);
        return Collections.unmodifiableList(resultList);
    }

    /**
     * Returns an unmodifiable list containing all elements from given lists
     * list1 and list2.
     */
    public static <T> List<T> concat(final List<T> list1, final List<T> list2) {
        val resultList = new ArrayList<T>(list1.size() + list2.size());
        resultList.addAll(list1);
        resultList.addAll(list2);
        return Collections.unmodifiableList(resultList);
    }

    // -- UNMODIFIABLE LIST

    /**
     * Returns an unmodifiable list containing only the specified element.
     * @param element (required)
     * @return non null
     */
    public static <T> List<T> singleton(final @NonNull T element) {
        return Collections.singletonList(element);
    }

    /**
     * Returns an unmodifiable list containing only the specified element or
     * the empty list if the element is null.<commit_msg>ISIS-3084: commons: make _List concat utils null-safe<commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 org.apache.isis.commons.internal.collections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.springframework.lang.Nullable;

import org.apache.isis.commons.internal.base._NullSafe;

import lombok.NonNull;
import lombok.val;

/**
 * <h1>- internal use only -</h1>
 * <p>
 * Common List creation idioms.
 * </p>
 * <p>
 * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
 * These may be changed or removed without notice!
 * </p>
 *
 * @since 2.0
 */
public final class _Lists {

    private _Lists(){}

    // -- LIST ACCESS

    public static <T> T lastElementIfAny(final @Nullable List<T> list) {
        if(_NullSafe.isEmpty(list)) {
            return null;
        }
        return list.get(list.size()-1);
    }

    public static <T> Optional<T> lastElement(final @Nullable List<T> list) {
        if(_NullSafe.isEmpty(list)) {
            return Optional.empty();
        }
        return Optional.ofNullable(list.get(list.size()-1));
    }

    // -- LIST CONCATENATION

    /**
     * Returns an unmodifiable list containing all elements from given list
     * and the specified element.
     */
    public static <T> List<T> append(final @Nullable List<T> list, final @Nullable T element) {
        if(_NullSafe.isEmpty(list)) {
            return Collections.singletonList(element);
        }
        val resultList = new ArrayList<T>(list.size() + 1);
        resultList.addAll(list);
        resultList.add(element);
        return Collections.unmodifiableList(resultList);
    }

    /**
     * Returns an unmodifiable list containing all elements from given lists
     * list1 and list2.
     */
    public static <T> List<T> concat(final @Nullable List<T> list1, final @Nullable List<T> list2) {
        val isEmpty1 = _NullSafe.isEmpty(list1);
        val isEmpty2 = _NullSafe.isEmpty(list2);

        if(isEmpty1) {
            return isEmpty2
                    ? Collections.emptyList()
                    : Collections.unmodifiableList(new ArrayList<T>(list2));
        }

        if(isEmpty2) {
            // at this point list1 is not empty
            return Collections.unmodifiableList(new ArrayList<T>(list1));
        }

        val resultList = new ArrayList<T>(list1.size() + list2.size());
        resultList.addAll(list1);
        resultList.addAll(list2);
        return Collections.unmodifiableList(resultList);
    }

    // -- UNMODIFIABLE LIST

    /**
     * Returns an unmodifiable list containing only the specified element.
     * @param element (required)
     * @return non null
     */
    public static <T> List<T> singleton(final @NonNull T element) {
        return Collections.singletonList(element);
    }

    /**
     * Returns an unmodifiable list containing only the specified element or
     * the empty list if the element is null.
{'fraction_non_alphanumeric': 0.10311719058266473, 'fraction_numerical': 0.006839405497829804, 'mean_word_length': 2.9236326109391126, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '142451', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2162, 'n_words': 697}