001    package edu.nrao.sss.model.resource.sort;
002    
003    import java.util.Comparator;
004    import java.util.List;
005    
006    import edu.nrao.sss.model.resource.Resource;
007    import edu.nrao.sss.sort.SortKey;
008    import edu.nrao.sss.sort.SortOrder;
009    import edu.nrao.sss.sort.StringSortKey;
010    
011    /**
012     * Sorts {@link Resource resources} based on their first Note (comments field).
013     * <p>
014     * Because this class is a {@link SortKey}, you may configure its instances with
015     * a particular {@code SortOrder}.  Because it is a {@link StringSortKey}, you
016     * may tell it to ignore differences in case.  Finally, because it is a
017     * {@link Comparator}, you may use it for sorting collections on its own, or
018     * place it in a {@link edu.nrao.sss.sort.CompoundComparator} to use as one of
019     * many keys in a single sort.</p>
020     * <p/>
021     * The default values for this key's attributes are:
022     * <ul>
023     *   <li>{@link #setOrder(SortOrder) Sort order}:
024     *       {@link SortOrder#NATURAL natural}</li>
025     *   <li>{@link #setIgnoreCase(boolean) Ignore case}: true</li>
026     * </ul>
027     * <p>
028     * <b>Version Info:</b>
029     * <table style="margin-left:2em">
030     *   <tr><td>$Revision: 1710 $</td></tr>
031     *   <tr><td>$Date: 2008-11-14 11:54:07 -0700 (Fri, 14 Nov 2008) $</td></tr>
032     *   <tr><td>$Author: dharland $</td></tr>
033     * </table></p>
034     */
035    public class CommentsSortKey
036      extends StringSortKey
037      implements Comparator<Resource>
038    {
039      public int compare(Resource a, Resource b)
040      {
041        List<String> notesA = a.getNotes();
042        List<String> notesB = b.getNotes();
043    
044        if (notesA == null || notesA.isEmpty())
045          return -1;
046    
047        else if (notesB == null || notesB.isEmpty())
048          return 1;
049    
050        else
051          return compareObjects(notesA.get(0), notesB.get(0));
052      }
053    }