JAL-2110 random stuff
[jalview.git] / src / jalview / util / DBRefUtils.java
index db52647..ed6d860 100755 (executable)
@@ -151,8 +151,8 @@ public class DBRefUtils
   }
 
   /**
-   * Returns an array of those references that match the given entry, or null if
-   * no matches. Currently uses a comparator which matches if
+   * Returns a (possibly empty) list of those references that match the given
+   * entry. Currently uses a comparator which matches if
    * <ul>
    * <li>database sources are the same</li>
    * <li>accession ids are the same</li>
@@ -165,34 +165,35 @@ public class DBRefUtils
    *          pattern to match
    * @return
    */
-  public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
+  public static List<DBRefEntry> searchRefs(DBRefEntry[] ref,
+          DBRefEntry entry)
   {
     return searchRefs(ref, entry,
             matchDbAndIdAndEitherMapOrEquivalentMapList);
   }
 
   /**
-   * Returns an array of those references that match the given accession id
+   * Returns a list of those references that match the given accession id
    * <ul>
    * <li>database sources are the same</li>
    * <li>accession ids are the same</li>
    * <li>both have no mapping, or the mappings are the same</li>
    * </ul>
    * 
-   * @param ref
+   * @param refs
    *          Set of references to search
-   * @param entry
-   *          pattern to match
+   * @param accId
+   *          accession id to match
    * @return
    */
-  public static DBRefEntry[] searchRefs(DBRefEntry[] ref, String accId)
+  public static List<DBRefEntry> searchRefs(DBRefEntry[] refs, String accId)
   {
-    return searchRefs(ref, new DBRefEntry("", "", accId), matchId);
+    return searchRefs(refs, new DBRefEntry("", "", accId), matchId);
   }
 
   /**
-   * Returns an array of those references that match the given entry, according
-   * to the given comparator. Returns null if no matches.
+   * Returns a (possibly empty) list of those references that match the given
+   * entry, according to the given comparator.
    * 
    * @param refs
    *          an array of database references to search
@@ -201,14 +202,14 @@ public class DBRefUtils
    * @param comparator
    * @return
    */
-  static DBRefEntry[] searchRefs(DBRefEntry[] refs, DBRefEntry entry,
+  static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
           DbRefComp comparator)
   {
+    List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
     if (refs == null || entry == null)
     {
-      return null;
+      return rfs;
     }
-    List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
     for (int i = 0; i < refs.length; i++)
     {
       if (comparator.matches(entry, refs[i]))
@@ -216,7 +217,7 @@ public class DBRefUtils
         rfs.add(refs[i]);
       }
     }
-    return rfs.size() == 0 ? null : rfs.toArray(new DBRefEntry[rfs.size()]);
+    return rfs;
   }
 
   interface DbRefComp
@@ -543,4 +544,29 @@ public class DBRefUtils
     // (not dna, not protein seq)
   }
 
+  /**
+   * Returns the (possibly empty) list of those supplied dbrefs which have the
+   * specified source databse
+   * 
+   * @param dbRefs
+   * @param source
+   * @return
+   */
+  public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
+          String source)
+  {
+    List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
+    if (dbRefs != null && source != null)
+    {
+      for (DBRefEntry dbref : dbRefs)
+      {
+        if (source.equals(dbref.getSource()))
+        {
+          matches.add(dbref);
+        }
+      }
+    }
+    return matches;
+  }
+
 }