001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.tools.plugin.extractor;
020
021import java.util.Objects;
022
023/**
024 * Group key: defines "grouping" for descriptor (based on source of extraction) and rank within
025 * group.
026 *
027 * @since TBD
028 */
029public final class GroupKey implements Comparable<GroupKey> {
030    /**
031     * Java group is handled a bit special: is always first to be scanned.
032     */
033    public static final String JAVA_GROUP = "java";
034
035    private final String group;
036
037    private final int order;
038
039    public GroupKey(String group, int order) {
040        if (group == null) {
041            throw new NullPointerException("GroupKey.group null");
042        }
043        this.group = group;
044        this.order = order;
045    }
046
047    /**
048     * Returns the group this key belongs to, never {@code null}.
049     */
050    public String getGroup() {
051        return group;
052    }
053
054    /**
055     * Returns the order within same group of this key. Returns int should be used for ordering only.
056     */
057    public int getOrder() {
058        return order;
059    }
060
061    @Override
062    public boolean equals(Object o) {
063        if (this == o) {
064            return true;
065        }
066        if (o == null || getClass() != o.getClass()) {
067            return false;
068        }
069        GroupKey groupKey = (GroupKey) o;
070        return order == groupKey.order && group.equals(groupKey.group);
071    }
072
073    @Override
074    public int hashCode() {
075        return Objects.hash(group, order);
076    }
077
078    /**
079     * Compares by group then by order, with special case of {@link #JAVA_GROUP} group:
080     * <ul>
081     *     <li>First, {@link #group} is considered, if equals to {@link #JAVA_GROUP}, is always first, other
082     *     groups are in natural order (string)</li>
083     *     <li>within same named groups, order is defined by {@link #order}</li>
084     * </ul>
085     */
086    @Override
087    public int compareTo(final GroupKey o) {
088        if (JAVA_GROUP.equals(this.group) && !JAVA_GROUP.equals(o.group)) {
089            return -1;
090        } else if (!JAVA_GROUP.equals(this.group) && JAVA_GROUP.equals(o.group)) {
091            return 1;
092        }
093        int result = this.group.compareTo(o.group);
094        if (result != 0) {
095            return result;
096        }
097        return Integer.compare(this.order, o.order);
098    }
099
100    @Override
101    public String toString() {
102        return group + ":" + order;
103    }
104}