001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc;
019
020/**
021 * Implements {@link #hashCode()}, {@link #equals(Object)}, {@link #toString()}, and {@link #compareTo(Object)} for named objects.
022 * @author Paul Ferraro
023 * @param <N> the name type
024 * @param <T> this type
025 */
026public abstract class AbstractNamed<N extends Comparable<N>, T extends Named<N, T>> implements Named<N, T>
027{
028        private final N name;
029        
030        protected AbstractNamed(N name)
031        {
032                this.name = name;
033        }
034        
035        @Override
036        public N getName()
037        {
038                return this.name;
039        }
040
041        @Override
042        public int hashCode()
043        {
044                return this.name.hashCode();
045        }
046
047        @Override
048        public boolean equals(Object object)
049        {
050                return ((object != null) && this.getClass().isInstance(object)) ? this.name.equals(this.getClass().cast(object).getName()) : false;
051        }
052
053        @Override
054        public String toString()
055        {
056                return this.name.toString();
057        }
058
059        /**
060         * @see java.lang.Comparable#compareTo(java.lang.Object)
061         */
062        @Override
063        public int compareTo(T named)
064        {
065                return this.getName().compareTo(named.getName());
066        }
067}