/*
 * Copyright (c) 2012 by The Author(s)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.ferstl.maven.pomenforcers.artifact;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import com.github.ferstl.maven.pomenforcers.priority.PriorityComparatorFactory;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;


public class ArtifactSorter<T, F extends PriorityComparatorFactory<String, T>> {

  private final Set<F> orderBy;
  private final Multimap<F, String> priorityMap;

  public ArtifactSorter() {
    this.orderBy = Sets.newLinkedHashSet();
    this.priorityMap = LinkedHashMultimap.create();
  }

  public void orderBy(Collection<F> artifactElements) {
    this.orderBy.clear();
    this.orderBy.addAll(artifactElements);
  }

  public void setPriorities(F artifactElement, Iterable<String> priorities) {
    this.priorityMap.putAll(artifactElement, priorities);
  }

  public Collection<F> getOrderBy() {
    return Collections.unmodifiableCollection(this.orderBy);
  }

  public Collection<String> getPriorities(F artifactElement) {
    return this.priorityMap.get(artifactElement);
  }

  public Ordering<T> createOrdering() {
    List<Comparator<T>> comparators = Lists.newArrayListWithCapacity(this.orderBy.size());
    for (F artifactElement : this.orderBy) {
      Comparator<T> comparator =
          artifactElement.createPriorityComparator(this.priorityMap.get(artifactElement));
      comparators.add(comparator);
    }

    Ordering<T> ordering;
    if (comparators.size() > 0) {
      ordering = Ordering.from(comparators.get(0));
      for (Comparator<T> comparator : comparators.subList(1, comparators.size())) {
        ordering = ordering.compound(comparator);
      }
    } else {
      throw new IllegalStateException("Undefined artifact order.");
    }
    return ordering;
  }
}

@@ -649,24 +649,52 @@
 .artifact;%0A%0A
+import java.util.ArrayList;%0A
 import java.
@@ -710,16 +710,16 @@
 ection;%0A
-
 import j
@@ -769,16 +769,48 @@
 arator;%0A
+import java.util.LinkedHashSet;%0A
 import j
@@ -984,48 +984,8 @@
 ap;%0A
-import com.google.common.collect.Lists;%0A
 impo
@@ -1070,47 +1070,8 @@
 ng;%0A
-import com.google.common.collect.Sets;%0A
 %0A%0Apu
@@ -1283,16 +1283,12 @@
 y = 
-Sets.
 new
+ 
 Link
@@ -1296,16 +1296,18 @@
 dHashSet
+%3C%3E
 ();%0A    
@@ -1934,17 +1934,12 @@
 s = 
-Lists.
 new
+ 
 Arra
@@ -1947,20 +1947,10 @@
 List
-WithCapacity
+%3C%3E
 (thi
@@ -2187,364 +2187,45 @@
     
-Ordering%3CT%3E ordering;%0A    if (comparators.size() %3E 0) %7B%0A      ordering = Ordering.from(comparators.get(0));%0A      for (Comparator%3CT%3E comparator : comparators.subList(1, comparators.size())) %7B%0A        ordering = ordering.compound(comparator);%0A      %7D%0A    %7D else %7B%0A      throw new IllegalStateException(%22Undefined artifact order.%22);%0A    %7D%0A    return ordering
+return Ordering.compound(comparators)
 ;%0A  

simplify createOrdering() method