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