File Source: MediaSourceComparator.java

         /* 
    P/P   *  Method: com.dmdirc.addons.nowplaying.MediaSourceComparator__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.addons.nowplaying;
    24  
    25  import java.io.Serializable;
    26  import java.util.Comparator;
    27  import java.util.List;
    28  
    29  /**
    30   * Sorts media sources according to an ordered list of their names.
    31   *
    32   * @author chris
    33   */
         /* 
    P/P   *  Method: int compare(Object, Object)
          * 
          *  Preconditions:
          *    this.order != null
          *    x0 != null
          *    x1 != null
          * 
          *  Postconditions:
          *    return_value == 0
          */
    34  public class MediaSourceComparator implements Comparator<MediaSource>, Serializable {
    35      
    36      /**
    37       * A version number for this class. It should be changed whenever the class
    38       * structure is changed (or anything else that would prevent serialized
    39       * objects being unserialized with the new class).
    40       */
    41      private static final long serialVersionUID = 1;
    42      
    43      /** The order that the sources should be checked. */
    44      private final List<String> order;
    45      
    46      /**
    47       * Creates a new instance of MediaSourceComparator.
    48       * NB: The order list may be altered during comparisons.
    49       *
    50       * @param order An ordered list of media source names
    51       */
             /* 
    P/P       *  Method: void com.dmdirc.addons.nowplaying.MediaSourceComparator(List)
              * 
              *  Postconditions:
              *    this.order == order
              *    init'ed(this.order)
              */
    52      public MediaSourceComparator(final List<String> order) {
    53          this.order = order;
    54      }
    55  
    56      /** {@inheritDoc} */
    57      @Override
    58      public int compare(final MediaSource o1, final MediaSource o2) {
                 /* 
    P/P           *  Method: int compare(MediaSource, MediaSource)
                  * 
                  *  Preconditions:
                  *    o1 != null
                  *    o2 != null
                  *    this.order != null
                  * 
                  *  Presumptions:
                  *    java.util.List:indexOf(...)@73 - java.util.List:indexOf(...)@73 in {-232+1..231}
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    59          return getPosition(o1) - getPosition(o2);
    60      }
    61      
    62      /**
    63       * Retrieves the position of the source within the order list.
    64       * If the source is not present it is appended to the list.
    65       *
    66       * @param source The media source to be tested
    67       */
    68      private int getPosition(final MediaSource source) {
                 /* 
    P/P           *  Method: int getPosition(MediaSource)
                  * 
                  *  Preconditions:
                  *    source != null
                  *    this.order != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.util.List:contains(...)@69: {1}, {0}
                  */
    69          if (!order.contains(source.getAppName())) {
    70              order.add(source.getAppName());
    71          }
    72          
    73          return order.indexOf(source.getAppName());
    74      }
    75      
    76  }








SofCheck Inspector Build Version : 2.17854
MediaSourceComparator.java 2009-Jun-25 01:54:24
MediaSourceComparator.class 2009-Sep-02 17:04:15