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

org.gradle.api.Namer Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.api;

/**
 * A namer is capable of providing a name based on some inherent characteristic of an object.
 *
 * @param  The type of object that the namer can name
 */
public interface Namer {

    /**
     * Determines the name of the given object.
     *
     * @param object The object to determine the name of
     * @return The object's inherent name. Never null.
     * @throws RuntimeException If the name cannot be determined or is null
     */
    String determineName(T object);

    /**
     * A comparator implementation based on the names returned by the given namer.
     *
     * @param  The type of object that the namer can name
     */
    class Comparator implements java.util.Comparator {

        private final Namer namer;

        public Comparator(Namer namer) {
            this.namer = namer;
        }

        @Override
        public int compare(T o1, T o2) {
            return namer.determineName(o1).compareTo(namer.determineName(o2));
        }

        public boolean equals(Object obj) {
            return getClass().equals(obj.getClass()) && namer.equals(((Comparator)obj).namer);
        }

        public int hashCode() {
            return 31 * namer.hashCode();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy