1 package org.apache.maven.tools.plugin.extractor;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.Objects;
23
24 /**
25 * Group key: defines "grouping" for descriptor (based on source of extraction) and rank within
26 * group.
27 *
28 * @since TBD
29 */
30 public final class GroupKey
31 implements Comparable<GroupKey>
32 {
33 /**
34 * Java group is handled a bit special: is always first to be scanned.
35 */
36 public static final String JAVA_GROUP = "java";
37
38 private final String group;
39
40 private final int order;
41
42 public GroupKey( String group, int order )
43 {
44 if ( group == null )
45 {
46 throw new NullPointerException( "GroupKey.group null" );
47 }
48 this.group = group;
49 this.order = order;
50 }
51
52 /**
53 * Returns the group this key belongs to, never {@code null}.
54 */
55 public String getGroup()
56 {
57 return group;
58 }
59
60 /**
61 * Returns the order within same group of this key. Returns int should be used for ordering only.
62 */
63 public int getOrder()
64 {
65 return order;
66 }
67
68 @Override
69 public boolean equals( Object o )
70 {
71 if ( this == o )
72 {
73 return true;
74 }
75 if ( o == null || getClass() != o.getClass() )
76 {
77 return false;
78 }
79 GroupKey groupKey = (GroupKey) o;
80 return order == groupKey.order && group.equals( groupKey.group );
81 }
82
83 @Override
84 public int hashCode()
85 {
86 return Objects.hash( group, order );
87 }
88
89 /**
90 * Compares by group then by order, with special case of {@link #JAVA_GROUP} group:
91 * <ul>
92 * <li>First, {@link #group} is considered, if equals to {@link #JAVA_GROUP}, is always first, other
93 * groups are in natural order (string)</li>
94 * <li>within same named groups, order is defined by {@link #order}</li>
95 * </ul>
96 */
97 @Override
98 public int compareTo( final GroupKey o )
99 {
100 if ( JAVA_GROUP.equals( this.group ) && !JAVA_GROUP.equals( o.group ) )
101 {
102 return -1;
103 }
104 else if ( !JAVA_GROUP.equals( this.group ) && JAVA_GROUP.equals( o.group ) )
105 {
106 return 1;
107 }
108 int result = this.group.compareTo( o.group );
109 if ( result != 0 )
110 {
111 return result;
112 }
113 return Integer.compare( this.order, o.order );
114 }
115
116 @Override
117 public String toString()
118 {
119 return group + ":" + order;
120 }
121 }