1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.lifecycle.internal;
20
21 import java.util.Comparator;
22 import java.util.List;
23
24 /**
25 * Compares phases within the context of a specific lifecycle with secondary sorting based on the {@link PhaseId}.
26 */
27 public class PhaseComparator implements Comparator<String> {
28 /**
29 * The lifecycle phase ordering.
30 */
31 private final List<String> lifecyclePhases;
32
33 /**
34 * Constructor.
35 *
36 * @param lifecyclePhases the lifecycle phase ordering.
37 */
38 public PhaseComparator(List<String> lifecyclePhases) {
39 this.lifecyclePhases = lifecyclePhases;
40 }
41
42 @Override
43 public int compare(String o1, String o2) {
44 PhaseId p1 = PhaseId.of(o1);
45 PhaseId p2 = PhaseId.of(o2);
46 int i1 = lifecyclePhases.indexOf(p1.phase());
47 int i2 = lifecyclePhases.indexOf(p2.phase());
48 if (i1 == -1 && i2 == -1) {
49 // unknown phases, leave in existing order
50 return 0;
51 }
52 if (i1 == -1) {
53 // second one is known, so it comes first
54 return 1;
55 }
56 if (i2 == -1) {
57 // first one is known, so it comes first
58 return -1;
59 }
60 int rv = Integer.compare(i1, i2);
61 if (rv != 0) {
62 return rv;
63 }
64 // same phase, now compare execution points
65 i1 = p1.executionPoint().ordinal();
66 i2 = p2.executionPoint().ordinal();
67 rv = Integer.compare(i1, i2);
68 if (rv != 0) {
69 return rv;
70 }
71 // same execution point, now compare priorities
72 return Integer.compare(p1.priority(), p2.priority());
73 }
74 }