@SuppressWarnings("unchecked") // generic arrays and unchecked cast
    void testMinAndMax() {
      List<T> shuffledList = Lists.newArrayList(strictlyOrderedList);
      shuffledList = shuffledCopy(shuffledList, new Random(5));

      T min = strictlyOrderedList.get(0);
      T max = strictlyOrderedList.get(strictlyOrderedList.size() - 1);

      T first = shuffledList.get(0);
      T second = shuffledList.get(1);
      T third = shuffledList.get(2);
      T[] rest = shuffledList.subList(3, shuffledList.size()).toArray(emptyArray);

      assertEquals(min, ordering.min(shuffledList));
      assertEquals(min, ordering.min(shuffledList.iterator()));
      assertEquals(min, ordering.min(first, second, third, rest));
      assertEquals(min, ordering.min(min, max));
      assertEquals(min, ordering.min(max, min));

      assertEquals(max, ordering.max(shuffledList));
      assertEquals(max, ordering.max(shuffledList.iterator()));
      assertEquals(max, ordering.max(first, second, third, rest));
      assertEquals(max, ordering.max(min, max));
      assertEquals(max, ordering.max(max, min));
    }
  public void testParameterMinAndMax() {
    assertEquals(5, (int) numberOrdering.max(3, 5));
    assertEquals(5, (int) numberOrdering.max(5, 3));
    assertEquals(3, (int) numberOrdering.min(3, 5));
    assertEquals(3, (int) numberOrdering.min(5, 3));

    // when the values are the same, the first argument should be returned
    Integer a = new Integer(4);
    Integer b = new Integer(4);
    assertSame(a, numberOrdering.max(a, b));
    assertSame(a, numberOrdering.min(a, b));
  }
  public void testIterableMinAndMax() {
    List<Integer> ints = Lists.newArrayList(5, 3, 0, 9);
    assertEquals(9, (int) numberOrdering.max(ints));
    assertEquals(0, (int) numberOrdering.min(ints));

    // when the values are the same, the first argument should be returned
    Integer a = new Integer(4);
    Integer b = new Integer(4);
    ints = Lists.newArrayList(a, b, b);
    assertSame(a, numberOrdering.max(ints));
    assertSame(a, numberOrdering.min(ints));
  }
  @Test(dependsOnMethods = "testGetLocation")
  public void testResolveVolumeOffering() throws Exception {

    Ordering<StorageOffering> cheapestOrdering =
        new Ordering<StorageOffering>() {
          public int compare(StorageOffering left, StorageOffering right) {
            return ComparisonChain.start()
                .compare(left.getPrice().getRate(), right.getPrice().getRate())
                .result();
          }
        }.reverse();

    Iterable<? extends StorageOffering> storageOfferingsThatAreInOurLocationAndCorrectFormat =
        filter(
            connection.listStorageOfferings(),
            new Predicate<StorageOffering>() {
              @Override
              public boolean apply(StorageOffering arg0) {

                return arg0.getLocation().equals(location.getId())
                    && Iterables.any(
                        arg0.getFormats(),
                        new Predicate<StorageOffering.Format>() {

                          @Override
                          public boolean apply(Format arg0) {
                            return arg0.getId().equals(FORMAT);
                          }
                        });
              }
            });
    cheapestStorage = cheapestOrdering.max(storageOfferingsThatAreInOurLocationAndCorrectFormat);
    System.out.println(cheapestStorage);
  }
  public void testVarargsMinAndMax() {
    // try the min and max values in all positions, since some values are proper
    // parameters and others are from the varargs array
    assertEquals(9, (int) numberOrdering.max(9, 3, 0, 5, 8));
    assertEquals(9, (int) numberOrdering.max(5, 9, 0, 3, 8));
    assertEquals(9, (int) numberOrdering.max(5, 3, 9, 0, 8));
    assertEquals(9, (int) numberOrdering.max(5, 3, 0, 9, 8));
    assertEquals(9, (int) numberOrdering.max(5, 3, 0, 8, 9));
    assertEquals(0, (int) numberOrdering.min(0, 3, 5, 9, 8));
    assertEquals(0, (int) numberOrdering.min(5, 0, 3, 9, 8));
    assertEquals(0, (int) numberOrdering.min(5, 3, 0, 9, 8));
    assertEquals(0, (int) numberOrdering.min(5, 3, 9, 0, 8));
    assertEquals(0, (int) numberOrdering.min(5, 3, 0, 9, 0));

    // when the values are the same, the first argument should be returned
    Integer a = new Integer(4);
    Integer b = new Integer(4);
    assertSame(a, numberOrdering.max(a, b, b));
    assertSame(a, numberOrdering.min(a, b, b));
  }
 @Test(groups = "ordering")
 public void testAdHocOrdering() {
   Ordering<String> byLengthOrdering =
       new Ordering<String>() {
         public int compare(String left, String right) {
           return (left.length() - right.length());
         }
       };
   assertEquals(
       byLengthOrdering.max(hanSolo.getName(), lukeSkywalker.getName(), princessLeia.getName()),
       "Luke Skywalker");
 }
  @Test(dependsOnMethods = "testGetLocation")
  public void resolveImageAndInstanceType() throws Exception {
    Iterable<? extends Image> imagesThatAreInOurLocationAndNotBYOL =
        filter(
            connection.listImages(),
            new Predicate<Image>() {
              @Override
              public boolean apply(Image arg0) {
                return arg0.getLocation().equals(location.getId())
                    && arg0.getPlatform().equals(PLATFORM)
                    && !arg0.getName().contains("BYOL")
                    && !arg0.getName().contains("PAYG");
              }
            });

    Ordering<InstanceType> cheapestOrdering =
        new Ordering<InstanceType>() {
          public int compare(InstanceType left, InstanceType right) {
            return ComparisonChain.start()
                .compare(left.getPrice().getRate(), right.getPrice().getRate())
                .result();
          }
        }.reverse();

    Set<InstanceType> instanceTypes = Sets.newLinkedHashSet();

    for (Image image : imagesThatAreInOurLocationAndNotBYOL)
      Iterables.addAll(instanceTypes, image.getSupportedInstanceTypes());

    instanceType = cheapestOrdering.max(instanceTypes);

    final InstanceType cheapestInstanceType = instanceType;
    System.err.println(cheapestInstanceType);

    image =
        Iterables.find(
            imagesThatAreInOurLocationAndNotBYOL,
            new Predicate<Image>() {

              @Override
              public boolean apply(Image arg0) {
                return arg0.getSupportedInstanceTypes().contains(cheapestInstanceType);
              }
            });
    System.err.println(image);
    connection.getManifest(connection.getImage(image.getId()).getManifest());
    connection.getManifest(connection.getImage(image.getId()).getDocumentation());
  }
 @Nullable
 public TaskDescriptor<IndexCommandResult> getLastindexTask() {
   final Collection<TaskDescriptor<?>> indexingTasks =
       taskManager.findTasks(
           new TaskMatcher() {
             @Override
             public boolean match(final TaskDescriptor<?> descriptor) {
               return descriptor.getTaskContext() instanceof IndexTaskContext;
             }
           });
   if (indexingTasks.size() > 0) {
     return (TaskDescriptor<IndexCommandResult>) byIdOrdering.max(indexingTasks);
   } else {
     return null;
   }
 }
 public void testIteratorMaxExhaustsIterator() {
   List<Integer> ints = Lists.newArrayList(9, 0, 3, 5);
   Iterator<Integer> iterator = ints.iterator();
   assertEquals(9, (int) numberOrdering.max(iterator));
   assertFalse(iterator.hasNext());
 }