File Source: FrameContainerComparator.java

         /* 
    P/P   *  Method: com.dmdirc.FrameContainerComparator__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;
    24  
    25  import java.io.Serializable;
    26  import java.util.Comparator;
    27  
    28  /**
    29   * Compares FrameContainers by name.
    30   */
         /* 
    P/P   *  Method: int compare(Object, Object)
          * 
          *  Preconditions:
          *    (soft) x0 != null
          *    (soft) x1 != null
          * 
          *  Postconditions:
          *    init'ed(return_value)
          */
    31  public final class FrameContainerComparator implements Comparator<FrameContainer>,
    32          Serializable {
    33  
    34      /**
    35       * A version number for this class. It should be changed whenever the class
    36       * structure is changed (or anything else that would prevent serialized
    37       * objects being unserialized with the new class).
    38       */
    39      private static final long serialVersionUID = 1;
    40  
    41      /**
    42       * Creates a new instance of FrameContainerComparator.
    43       */
    44      public FrameContainerComparator() {
                 /* 
    P/P           *  Method: void com.dmdirc.FrameContainerComparator()
                  */
    45          super();
    46      }
    47  
    48      /**
    49       * Compares two frame containers names.
    50       *
    51       * @param item1 The first container to compare
    52       * @param item2 The second container to compare
    53       * @return -1 if item1 is before item2, 0 if they're equal,
    54       * +1 if item1 is after item2.
    55       */
    56      public int compare(final FrameContainer item1, final FrameContainer item2) {
                 /* 
    P/P           *  Method: int compare(FrameContainer, FrameContainer)
                  * 
                  *  Preconditions:
                  *    (soft) item1 != null
                  *    (soft) item2 != null
                  * 
                  *  Presumptions:
                  *    java.lang.Integer:valueOf(...)@65 != null
                  *    toString(...)@62 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:compareToIgnoreCase(...)@62: {-231..-1, 1..232-1}, {0}
                  */
    57          if (sortBefore(item1, item2)) {
    58              return -1;
    59          } else if (sortAfter(item1, item2)) {
    60              return 1;
    61          } else {
    62              final int position = item1.toString().compareToIgnoreCase(
    63                      item2.toString());
    64              if (position == 0) {
    65                  return Integer.valueOf(item1.hashCode()).compareTo(
    66                          Integer.valueOf(item2.hashCode()));
    67              } else {
    68                  return position;
    69              }
    70          }
    71      }
    72  
    73      /**
    74       * Compares frame container types and checks order preferences.
    75       *
    76       * @param item1 The new container to be tested
    77       * @param item2 The existing container to test against
    78       * @return True iff the new container should be before the old container
    79       */
    80      private boolean sortBefore(final FrameContainer item1,
    81              final FrameContainer item2) {
    82  
                 /* 
    P/P           *  Method: bool sortBefore(FrameContainer, FrameContainer)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    83          return getPosition(item1) < getPosition(item2);
    84      }
    85  
    86      /**
    87       * Compares frame container types and checks order preferences.
    88       *
    89       * @param item1 The new container to be tested
    90       * @param item2 The existing container to test against
    91       * @return True iff the new container should be after the old container
    92       */
    93      private boolean sortAfter(final FrameContainer item1,
    94              final FrameContainer item2) {
                 /* 
    P/P           *  Method: bool sortAfter(FrameContainer, FrameContainer)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    95          return getPosition(item1) > getPosition(item2);
    96      }
    97  
    98      /**
    99       * Returns an integer corresponding to the expected order of a frame
   100       * container.
   101       *
   102       * @param item The frame container to be tested
   103       * @return Position of the frame container
   104       */
   105      private int getPosition(final FrameContainer item) {
                 /* 
    P/P           *  Method: int getPosition(FrameContainer)
                  * 
                  *  Postconditions:
                  *    return_value in {0..5}
                  */
   106          if (item instanceof GlobalWindow) {
   107              return 0;
   108          } else if (item instanceof Server) {
   109              return 1;
   110          } else if (item instanceof Raw) {
   111              return 2;
   112          } else if (item instanceof Channel) {
   113              return 3;
   114          } else if (item instanceof Query) {
   115              return 4;
   116          } else {
   117              return 5;
   118          }
   119      }
   120  
   121  }








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