1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 package com.android.internal.util;
19 
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 
23 /** @hide */
24 public class ObjectUtils {
ObjectUtils()25     private ObjectUtils() {}
26 
27     @NonNull
firstNotNull(@ullable T a, @NonNull T b)28     public static <T> T firstNotNull(@Nullable T a, @NonNull T b) {
29         return a != null ? a : Preconditions.checkNotNull(b);
30     }
31 
compare(@ullable T a, @Nullable T b)32     public static <T extends Comparable> int compare(@Nullable T a, @Nullable T b) {
33         if (a != null) {
34             return (b != null) ? a.compareTo(b) : 1;
35         } else {
36             return (b != null) ? -1 : 0;
37         }
38     }
39 
40     /**
41      * Returns its first argument if non-null, and the second otherwise.
42      */
43     @Nullable
getOrElse(@ullable final T object, @Nullable final T otherwise)44     public static <T> T getOrElse(@Nullable final T object, @Nullable final T otherwise) {
45         return null != object ? object : otherwise;
46     }
47 }
48