| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveutils.collections; |
| 16 | |
| 17 | import java.util.Arrays; |
| 18 | |
| 19 | /** |
| 20 | * Helper class that performs some operations on "sets". |
| 21 | * |
| 22 | * @author Jean-Francois Poilpret |
| 23 | */ |
| 24 | public class SetOperations |
| 25 | { |
| 26 | protected SetOperations() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Checks if two lists have common items. |
| 32 | * @param list1 first list of items. This list must be sorted. |
| 33 | * @param list2 second list of items. This list must be sorted. |
| 34 | * @return <code>true</code> if <code>list1</code> and <code>list2</code> |
| 35 | * have at least one item in common |
| 36 | */ |
| 37 | static public boolean intersects(String[] list1, String[] list2) |
| 38 | { |
| 39 | //#### To be improved to O(n) algo (instead of current O(nlogn)) |
| 40 | String[] current1 = list1; |
| 41 | String[] current2 = list2; |
| 42 | if (current1.length > list2.length) |
| 43 | { |
| 44 | current1 = list2; |
| 45 | current2 = list1; |
| 46 | } |
| 47 | |
| 48 | for (int i = 0; i < current1.length; i++) |
| 49 | { |
| 50 | if (Arrays.binarySearch(current2, current1[i]) >= 0) |
| 51 | { |
| 52 | return true; |
| 53 | } |
| 54 | } |
| 55 | return false; |
| 56 | } |
| 57 | } |