All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bt.tracker.AnnounceKey Maven / Gradle / Ivy

There is a newer version: 1.10
Show newest version
package bt.tracker;

import java.util.Collections;
import java.util.List;

/**
 * This class encapsulates information about trackers,
 * that can be used for getting a particular torrent.
 *
 * @since 1.0
 */
public class AnnounceKey {

    private final String trackerUrl;
    private final List> trackerUrls;

    /**
     * Create a single-tracker announce key
     *
     * @since 1.0
     */
    public AnnounceKey(String trackerUrl) {
        this.trackerUrl = trackerUrl;
        this.trackerUrls = null;
    }

    /**
     * Create a multi-tracker announce key
     * See BEP-12: Multitracker Metadata Extension for more details
     *
     * @param trackerUrls List of tiers of trackers (first list contains primary trackers,
     *                    subsequent lists contain backup trackers)
     * @since 1.0
     */
    public AnnounceKey(List> trackerUrls) {
        this.trackerUrl = null;
        this.trackerUrls = Collections.unmodifiableList(trackerUrls);
    }

    /**
     * @return true if this announce key supports multi-trackers
     * @since 1.0
     */
    public boolean isMultiKey() {
        return trackerUrls != null;
    }

    /**
     * @return Tracker URL if {@link #isMultiKey()} is false, null otherwise
     * @since 1.0
     */
    public String getTrackerUrl() {
        return trackerUrl;
    }

    /**
     * @return List of Tracker tiers if {@link #isMultiKey()} is true, null otherwise
     * @since 1.0
     */
    public List> getTrackerUrls() {
        return trackerUrls;
    }

    @Override
    public String toString() {
        if (isMultiKey()) {
            return trackerUrls.toString();
        } else {
            return trackerUrl;
        }
    }

    @Override
    public int hashCode() {
        return isMultiKey()? trackerUrls.hashCode() : trackerUrl.hashCode();
    }

    @Override
    public boolean equals(Object o) {

        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        AnnounceKey that = (AnnounceKey) o;

        return isMultiKey()? trackerUrls.equals(that.trackerUrls) : trackerUrl.equals(that.trackerUrl);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy