• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 package com.cooliris.media;
18 
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 
22 public final class ArrayUtils {
computeSortedIntersection(ArrayList<MediaItem> firstList, final ArrayList<MediaItem> secondList, int maxSize, ArrayList<MediaItem> intersectionList, MediaItem[] hash)23     public static final void computeSortedIntersection(ArrayList<MediaItem> firstList, final ArrayList<MediaItem> secondList,
24             int maxSize, ArrayList<MediaItem> intersectionList, MediaItem[] hash) {
25         // Assumes that firstList is generally larger than the second list.
26         // Build a simple filter to speed up containment testing.
27         int mask = hash.length - 1;
28         int numItemsToHash = Math.min(secondList.size(), 2 * hash.length);
29         for (int i = 0; i < numItemsToHash; ++i) {
30             MediaItem item = secondList.get(i);
31             if (item != null) {
32                 hash[item.hashCode() & mask] = item;
33             }
34         }
35 
36         // Build the intersection array.
37         int firstListSize = firstList.size();
38         for (int i = 0; i < firstListSize; ++i) {
39             MediaItem firstListItem = firstList.get(i);
40             if (firstListItem == null)
41                 continue;
42             MediaItem hashItem = (hash != null) ? hash[firstListItem.hashCode() & mask] : null;
43             if (hashItem != null
44                     && ((hashItem.mId != Shared.INVALID && hashItem.hashCode() == firstListItem.hashCode()) || contains(secondList, firstListItem))) {
45                 intersectionList.add(firstListItem);
46                 if (--maxSize == 0) {
47                     break;
48                 }
49             }
50         }
51 
52         // Clear the hash table.
53         Arrays.fill(hash, null);
54     }
55 
contains(Object[] array, Object object)56     public static final boolean contains(Object[] array, Object object) {
57         if (object == null) {
58             return false;
59         }
60         int length = array.length;
61         for (int i = 0; i < length; ++i) {
62             if (object.equals(array[i])) {
63                 return true;
64             }
65         }
66         return false;
67     }
68 
clear(Object[] array)69     public static void clear(Object[] array) {
70         int length = array.length;
71         for (int i = 0; i < length; i++) {
72             array[i] = null;
73         }
74     }
75 
contains(ArrayList<MediaItem> items, MediaItem item)76     public static final boolean contains(ArrayList<MediaItem> items, MediaItem item) {
77         final int numItems = items.size();
78         if (item.mId == Shared.INVALID)
79             return false;
80         for (int i = 0; i < numItems; ++i) {
81             MediaItem thisItem = items.get(i);
82             if (item.hashCode() == thisItem.hashCode())
83                 return true;
84         }
85         return false;
86     }
87 
addAll(final String[] first, final String[] second)88     public static final String[] addAll(final String[] first, final String[] second) {
89         if (first == null && second == null)
90             return null;
91         if (first == null)
92             return second;
93         if (second == null)
94             return first;
95         final int numFirst = first.length;
96         final int numSecond = second.length;
97         String[] newArray = new String[numFirst + numSecond];
98         for (int i = 0; i < numFirst; ++i) {
99             newArray[i] = first[i];
100         }
101         for (int i = 0; i < numSecond; ++i) {
102             newArray[numFirst + i] = second[i];
103         }
104         return newArray;
105     }
106 }
107