View Javadoc
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.tools.plugin.extractor;
20  
21  import java.util.Objects;
22  
23  /**
24   * Group key: defines "grouping" for descriptor (based on source of extraction) and rank within
25   * group.
26   *
27   * @since TBD
28   */
29  public final class GroupKey implements Comparable<GroupKey> {
30      /**
31       * Java group is handled a bit special: is always first to be scanned.
32       */
33      public static final String JAVA_GROUP = "java";
34  
35      private final String group;
36  
37      private final int order;
38  
39      public GroupKey(String group, int order) {
40          if (group == null) {
41              throw new NullPointerException("GroupKey.group null");
42          }
43          this.group = group;
44          this.order = order;
45      }
46  
47      /**
48       * Returns the group this key belongs to, never {@code null}.
49       */
50      public String getGroup() {
51          return group;
52      }
53  
54      /**
55       * Returns the order within same group of this key. Returns int should be used for ordering only.
56       */
57      public int getOrder() {
58          return order;
59      }
60  
61      @Override
62      public boolean equals(Object o) {
63          if (this == o) {
64              return true;
65          }
66          if (o == null || getClass() != o.getClass()) {
67              return false;
68          }
69          GroupKey groupKey = (GroupKey) o;
70          return order == groupKey.order && group.equals(groupKey.group);
71      }
72  
73      @Override
74      public int hashCode() {
75          return Objects.hash(group, order);
76      }
77  
78      /**
79       * Compares by group then by order, with special case of {@link #JAVA_GROUP} group:
80       * <ul>
81       *     <li>First, {@link #group} is considered, if equals to {@link #JAVA_GROUP}, is always first, other
82       *     groups are in natural order (string)</li>
83       *     <li>within same named groups, order is defined by {@link #order}</li>
84       * </ul>
85       */
86      @Override
87      public int compareTo(final GroupKey o) {
88          if (JAVA_GROUP.equals(this.group) && !JAVA_GROUP.equals(o.group)) {
89              return -1;
90          } else if (!JAVA_GROUP.equals(this.group) && JAVA_GROUP.equals(o.group)) {
91              return 1;
92          }
93          int result = this.group.compareTo(o.group);
94          if (result != 0) {
95              return result;
96          }
97          return Integer.compare(this.order, o.order);
98      }
99  
100     @Override
101     public String toString() {
102         return group + ":" + order;
103     }
104 }