public final class

CollectionUtils

extends java.lang.Object

 java.lang.Object

↳androidx.car.app.utils.CollectionUtils

Gradle dependencies

compile group: 'androidx.car.app', name: 'app', version: '1.2.0-rc01'

  • groupId: androidx.car.app
  • artifactId: app
  • version: 1.2.0-rc01

Artifact androidx.car.app:app:1.2.0-rc01 it located at Google repository (https://maven.google.com/)

Overview

Assorted collection utilities.

Summary

Methods
public static java.util.List<java.lang.Object>emptyIfNull(java.util.List<java.lang.Object> list)

Returns the input list if not null, or an empty list otherwise.

public static java.util.List<java.lang.Object>unmodifiableCopy(java.util.List<java.lang.Object> list)

Returns a copy of the input java.util.List if it is not null, or a emptyList otherwise.

public static java.util.Map<java.lang.Object, java.lang.Object>unmodifiableCopy(java.util.Map<java.lang.Object, java.lang.Object> map)

Returns a copy of the input java.util.Map if it is not null, or a emptyMap otherwise.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static java.util.List<java.lang.Object> emptyIfNull(java.util.List<java.lang.Object> list)

Returns the input list if not null, or an empty list otherwise.

public static java.util.Map<java.lang.Object, java.lang.Object> unmodifiableCopy(java.util.Map<java.lang.Object, java.lang.Object> map)

Returns a copy of the input java.util.Map if it is not null, or a emptyMap otherwise.

public static java.util.List<java.lang.Object> unmodifiableCopy(java.util.List<java.lang.Object> list)

Returns a copy of the input java.util.List if it is not null, or a emptyList otherwise.

Source

/*
 * Copyright 2020 The Android Open Source Project
 *
 * 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 androidx.car.app.utils;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Assorted collection utilities.
 *
 * @hide
 */
@RestrictTo(LIBRARY_GROUP)
public final class CollectionUtils {
    /** Returns the input {@code list} if not {@code null}, or an empty list otherwise. */
    @NonNull
    public static <T> List<T> emptyIfNull(@Nullable List<T> list) {
        return list != null ? list : Collections.emptyList();
    }

    /**
     * Returns a copy of the input {@link Map} if it is not {@code null}, or a
     * {@link Collections#emptyMap} otherwise.
     */
    @NonNull
    public static <K, V> Map<K, V> unmodifiableCopy(@Nullable Map<K, V> map) {
        return map == null ? Collections.emptyMap() : Collections.unmodifiableMap(
                new HashMap<>(map));
    }

    /**
     * Returns a copy of the input {@link List} if it is not {@code null}, or a
     * {@link Collections#emptyList()} otherwise.
     */
    @NonNull
    public static <T> List<T> unmodifiableCopy(@Nullable List<T> list) {
        return list == null ? Collections.emptyList() : Collections.unmodifiableList(
                new ArrayList<>(list));
    }

    private CollectionUtils() {
    }
}