5686 |
05 Aug 11 |
nicklas |
1 |
/** |
5686 |
05 Aug 11 |
nicklas |
$Id$ |
5686 |
05 Aug 11 |
nicklas |
3 |
|
5686 |
05 Aug 11 |
nicklas |
Copyright (C) 2011 Nicklas Nordborg |
5686 |
05 Aug 11 |
nicklas |
5 |
|
5686 |
05 Aug 11 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5686 |
05 Aug 11 |
nicklas |
Available at http://base.thep.lu.se/ |
5686 |
05 Aug 11 |
nicklas |
8 |
|
5686 |
05 Aug 11 |
nicklas |
BASE is free software; you can redistribute it and/or |
5686 |
05 Aug 11 |
nicklas |
modify it under the terms of the GNU General Public License |
5686 |
05 Aug 11 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5686 |
05 Aug 11 |
nicklas |
of the License, or (at your option) any later version. |
5686 |
05 Aug 11 |
nicklas |
13 |
|
5686 |
05 Aug 11 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5686 |
05 Aug 11 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5686 |
05 Aug 11 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5686 |
05 Aug 11 |
nicklas |
GNU General Public License for more details. |
5686 |
05 Aug 11 |
nicklas |
18 |
|
5686 |
05 Aug 11 |
nicklas |
You should have received a copy of the GNU General Public License |
5686 |
05 Aug 11 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5686 |
05 Aug 11 |
nicklas |
21 |
*/ |
5686 |
05 Aug 11 |
nicklas |
22 |
package net.sf.basedb.util; |
5686 |
05 Aug 11 |
nicklas |
23 |
|
5686 |
05 Aug 11 |
nicklas |
24 |
import java.util.Collection; |
5686 |
05 Aug 11 |
nicklas |
25 |
|
5686 |
05 Aug 11 |
nicklas |
26 |
/** |
5686 |
05 Aug 11 |
nicklas |
27 |
|
5686 |
05 Aug 11 |
nicklas |
@author Nicklas |
5686 |
05 Aug 11 |
nicklas |
@since 3.0 |
5686 |
05 Aug 11 |
nicklas |
@base.modified $Date$ |
5686 |
05 Aug 11 |
nicklas |
31 |
*/ |
5686 |
05 Aug 11 |
nicklas |
32 |
public class ListUtil |
5686 |
05 Aug 11 |
nicklas |
33 |
{ |
5686 |
05 Aug 11 |
nicklas |
34 |
|
5686 |
05 Aug 11 |
nicklas |
35 |
/** |
5686 |
05 Aug 11 |
nicklas |
Find the first entry in the master collection that is also present in the |
5686 |
05 Aug 11 |
nicklas |
check collection. If there is no such entry, the notFound value is returned. |
5686 |
05 Aug 11 |
nicklas |
@param master The master collection |
5686 |
05 Aug 11 |
nicklas |
@param check The collection to check |
5686 |
05 Aug 11 |
nicklas |
@param notFound The value to return if there are no common elements |
5686 |
05 Aug 11 |
nicklas |
(including the case were one collection is null or empty) |
5686 |
05 Aug 11 |
nicklas |
42 |
*/ |
5686 |
05 Aug 11 |
nicklas |
43 |
public static <T> T findFirstCommon(Collection<T> master, Collection<T> check, T notFound) |
5686 |
05 Aug 11 |
nicklas |
44 |
{ |
5686 |
05 Aug 11 |
nicklas |
45 |
if (master == null || check == null || master.isEmpty() || check.isEmpty()) |
5686 |
05 Aug 11 |
nicklas |
46 |
{ |
5686 |
05 Aug 11 |
nicklas |
47 |
return notFound; |
5686 |
05 Aug 11 |
nicklas |
48 |
} |
5686 |
05 Aug 11 |
nicklas |
// Now we know that both collections have at least one element |
5686 |
05 Aug 11 |
nicklas |
50 |
for (T t : master) |
5686 |
05 Aug 11 |
nicklas |
51 |
{ |
5686 |
05 Aug 11 |
nicklas |
52 |
if (check.contains(t)) return t; |
5686 |
05 Aug 11 |
nicklas |
53 |
} |
5686 |
05 Aug 11 |
nicklas |
54 |
return notFound; |
5686 |
05 Aug 11 |
nicklas |
55 |
} |
5686 |
05 Aug 11 |
nicklas |
56 |
|
5686 |
05 Aug 11 |
nicklas |
57 |
} |