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 17package com.android.server.backup.utils; 18 19import android.util.SparseArray; 20 21import java.util.HashSet; 22 23/** 24 * Helper functions for manipulating instances of {@link SparseArray}. 25 */ 26public final class SparseArrayUtils { 27 // Statics only 28 private SparseArrayUtils() {} 29 30 /** 31 * Given a {@link SparseArray<HashSet>}, returns a new {@link HashSet} containing every element 32 * from every set in the array. 33 * 34 * @param sets The array of sets from which to take the union. 35 * @param <V> The type of element contained in the set. 36 * @return The complete set. 37 */ 38 public static<V> HashSet<V> union(SparseArray<HashSet<V>> sets) { 39 HashSet<V> unionSet = new HashSet<>(); 40 int n = sets.size(); 41 for (int i = 0; i < n; i++) { 42 HashSet<V> ithSet = sets.valueAt(i); 43 if (ithSet != null) { 44 unionSet.addAll(ithSet); 45 } 46 } 47 return unionSet; 48 } 49} 50