001    /*
002     * Created on Oct 17, 2010
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * the License. You may obtain a copy of the License at
006     * 
007     * http://www.apache.org/licenses/LICENSE-2.0
008     * 
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     * 
013     * Copyright @2010-2011 the original author or authors.
014     */
015    package org.fest.assertions.error;
016    
017    import static org.fest.assertions.util.ArrayWrapperList.wrap;
018    
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.Comparator;
022    import java.util.List;
023    
024    /**
025     * Creates an error message indicating that an assertion that verifies a group of elements is sorted failed.<br>
026     * A group of elements can be a collection or an array.
027     * 
028     * @author Joel Costigliola
029     */
030    public class ShouldBeSorted extends BasicErrorMessageFactory {
031    
032      /**
033       * Creates a new <code>{@link ShouldBeSorted}</code>.
034       * @param i the index of elements whose not naturally ordered with the next.
035       * @param group the actual group in the failed assertion (either collection or an array).
036       * @return an instance of {@code ErrorMessageFactory}.
037       */
038      public static ErrorMessageFactory shouldBeSorted(int i, Object group) {
039        List<?> groupAsList = groupAsList(group);
040        return new ShouldBeSorted("group is not sorted because element %s:<%s> is not less or equal than element %s:<%s>.\n"
041            + "group was:\n"
042            + "<%s>", i, groupAsList.get(i), i + 1, groupAsList.get(i + 1), groupAsList);
043      }
044    
045      public static ErrorMessageFactory shouldBeSortedAccordingToGivenComparator(int i, Object group,
046          Comparator<?> comparator) {
047        List<?> arrayWrapper = groupAsList(group);
048        return new ShouldBeSorted(
049            "group is not sorted according to %s comparator because element %s:<%s> is not less or equal than element %s:<%s>.\n"
050                + "group was:\n"
051                + "<%s>", comparator, i, arrayWrapper.get(i), i + 1, arrayWrapper.get(i + 1), arrayWrapper);
052      }
053    
054      public static ErrorMessageFactory shouldHaveMutuallyComparableElements(Object actual) {
055        return new ShouldBeSorted("some elements are not mutually comparable in group:<%s>", actual);
056      }
057    
058      public static ErrorMessageFactory shouldHaveComparableElementsAccordingToGivenComparator(Object actual,
059          Comparator<?> comparator) {
060        return new ShouldBeSorted("some elements are not mutually comparable according to %s comparator in group:<%s>",
061            comparator, actual);
062      }
063    
064      private ShouldBeSorted(String format, Object... arguments) {
065        super(format, arguments);
066      }
067    
068      /**
069       * Convert the given group (which is either an array or a Collection) to a List.
070       * @param group the group to convert
071       * @return the corresponding List
072       * @throws IllegalArgumentException if group can't be converted to a List
073       */
074      @SuppressWarnings("unchecked")
075      private static List<?> groupAsList(Object group) {
076        if (group.getClass().isArray()) {
077          return wrap(group);
078        } else if (group instanceof Collection<?>) {
079          List<Object> asList = new ArrayList<Object>();
080          asList.addAll(((Collection<Object>) group));
081          return asList;
082        }
083        throw new IllegalArgumentException("Parameter should be an array or a collection but was " + group);
084      }
085    
086    }