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.eclipse.aether.internal.impl;
020
021public final class PrioritizedComponent<T> implements Comparable<PrioritizedComponent<?>> {
022
023    private final T component;
024
025    private final Class<?> type;
026
027    private final float priority;
028
029    private final int index;
030
031    PrioritizedComponent(T component, Class<?> type, float priority, int index) {
032        this.component = component;
033        this.type = type;
034        this.priority = priority;
035        this.index = index;
036    }
037
038    public T getComponent() {
039        return component;
040    }
041
042    public Class<?> getType() {
043        return type;
044    }
045
046    public float getPriority() {
047        return priority;
048    }
049
050    public boolean isDisabled() {
051        return Float.isNaN(priority);
052    }
053
054    public int compareTo(PrioritizedComponent<?> o) {
055        int rel = (isDisabled() ? 1 : 0) - (o.isDisabled() ? 1 : 0);
056        if (rel == 0) {
057            rel = Float.compare(o.priority, priority);
058            if (rel == 0) {
059                rel = index - o.index;
060            }
061        }
062        return rel;
063    }
064
065    @Override
066    public String toString() {
067        return priority + " (#" + index + "): " + component;
068    }
069}