src/core/net/sf/basedb/util/ListUtil.java

Code
Comments
Other
Rev Date Author Line
5686 05 Aug 11 nicklas 1 /**
5686 05 Aug 11 nicklas 2   $Id$
5686 05 Aug 11 nicklas 3
5686 05 Aug 11 nicklas 4   Copyright (C) 2011 Nicklas Nordborg
5686 05 Aug 11 nicklas 5
5686 05 Aug 11 nicklas 6   This file is part of BASE - BioArray Software Environment.
5686 05 Aug 11 nicklas 7   Available at http://base.thep.lu.se/
5686 05 Aug 11 nicklas 8
5686 05 Aug 11 nicklas 9   BASE is free software; you can redistribute it and/or
5686 05 Aug 11 nicklas 10   modify it under the terms of the GNU General Public License
5686 05 Aug 11 nicklas 11   as published by the Free Software Foundation; either version 3
5686 05 Aug 11 nicklas 12   of the License, or (at your option) any later version.
5686 05 Aug 11 nicklas 13
5686 05 Aug 11 nicklas 14   BASE is distributed in the hope that it will be useful,
5686 05 Aug 11 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5686 05 Aug 11 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5686 05 Aug 11 nicklas 17   GNU General Public License for more details.
5686 05 Aug 11 nicklas 18
5686 05 Aug 11 nicklas 19   You should have received a copy of the GNU General Public License
5686 05 Aug 11 nicklas 20   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 28   @author Nicklas
5686 05 Aug 11 nicklas 29   @since 3.0
5686 05 Aug 11 nicklas 30   @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 36     Find the first entry in the master collection that is also present in the
5686 05 Aug 11 nicklas 37     check collection. If there is no such entry, the notFound value is returned.
5686 05 Aug 11 nicklas 38     @param master The master collection
5686 05 Aug 11 nicklas 39     @param check The collection to check
5686 05 Aug 11 nicklas 40     @param notFound The value to return if there are no common elements 
5686 05 Aug 11 nicklas 41       (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 49     // 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 }