001    package helliker.id3;
002    
003    import java.util.Comparator;
004    
005    /*
006       Copyright (C) 2001,2002 Jonathan Hilliker
007       This library is free software; you can redistribute it and/or
008       modify it under the terms of the GNU Lesser General Public
009       License as published by the Free Software Foundation; either
010       version 2.1 of the License, or (at your option) any later version.
011       This library is distributed in the hope that it will be useful,
012       but WITHOUT ANY WARRANTY; without even the implied warranty of
013       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014       Lesser General Public License for more details.
015       You should have received a copy of the GNU Lesser General Public
016       License along with this library; if not, write to the Free Software
017       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018      */
019    /**
020     * Description: This class is a comparator that is ideal for sorting mp3s. The
021     * MP3File class uses this object to implement its compareTo method. This works
022     * really well for playlists too because it has an order of precedence that
023     * attempts to sort the mp3s so that the tracks of the same artists and albums
024     * are together and that they are sorted by track number. If an error occurs
025     * while doing the comparison, the paths of the mp3s are compared.
026     */
027    
028    public class MP3Comparator implements Comparator {
029    
030      /**
031       * Returns true if the specified object is an MP3Comparator
032       *
033       *@param obj  the object to test
034       *@return     true if the object is an MP3Comparator
035       */
036      public boolean equals(Object obj) {
037        return (obj instanceof MP3Comparator);
038      }
039    
040    
041      /**
042       * Compares the objects. Non-MP3File objects will always be less then an
043       * MP3File object and if neither objects are MP3Files then they are considered
044       * equal. A series of tests are conducted to determine the the outcome. First
045       * the artist is tested, then the album, then the track, then the title, then
046       * the path.
047       *
048       *@param o1  one object to compare
049       *@param o2  another object to compare
050       *@return    a positive number if o1 > o2, zero if o1 = o2, and a negative
051       *      number if o1 < o2
052       */
053      public int compare(Object o1, Object o2) {
054        int retval = 0;
055    
056        if ((o1 instanceof MP3File) && (o2 instanceof MP3File)) {
057          MP3File m1 = (MP3File) o1;
058          MP3File m2 = (MP3File) o2;
059    
060          try {
061            if (accept(m1.getArtist(), m2.getArtist())) {
062              retval = m1.getArtist().compareToIgnoreCase(
063                m2.getArtist());
064    
065            } else if (accept(m1.getAlbum(), m2.getAlbum())) {
066              retval = m1.getAlbum().compareToIgnoreCase(m2.getAlbum());
067            } else if (accept(m1.getTrackString(), m2.getTrackString())) {
068                final Integer t1 = m1.getTrack();
069                final Integer t2 = m2.getTrack();
070                retval = ((t1 == null)? 0 : t1.intValue())
071                        -((t2 == null)? 0 : t2.intValue());
072            } else {
073              retval = m1.getPath().compareToIgnoreCase(m2.getPath());
074            }
075          } catch (ID3v2FormatException e) {
076            retval = m1.getPath().compareToIgnoreCase(m2.getPath());
077          }
078        } else {
079          if (o1 instanceof MP3File) {
080            retval = 1;
081          } else if (o2 instanceof MP3File) {
082            retval = -1;
083          } else {
084            retval = 0;
085          }
086        }
087    
088        return retval;
089      }
090    
091    
092      /**
093       * Returns true if the two parameters are acceptable comparison values. In
094       * order to be acceptable, both parameters must not be empty and they must not
095       * be equal.
096       *
097       *@param str1  the first parameter
098       *@param str2  the second parameter
099       *@return      true if the two parameters are acceptable comparison values
100       */
101      private boolean accept(String str1, String str2) {
102        return (!str1.equalsIgnoreCase(str2) && (str1.length() != 0)
103                && (str2.length() != 0));
104      }
105    
106    }// MP3Comparator
107    
108