001    /**
002     * 
003     * $Revision: 13468 $ $Date: 2008-04-28 17:49:59 +0200 (Mo, 28 Apr 2008) $
004     *
005     * This file is part of ** M y C o R e **
006     * Visit our homepage at http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, normally in the file license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     *
023     **/
024    package org.mycore.datamodel.classifications2;
025    
026    import java.io.Serializable;
027    
028    /**
029     * The composite identifier of a MCRCategory. If <code>rootID == ID</code> the
030     * associated MCRCategory instance is a root category (a classification).
031     * 
032     * @author Thomas Scheffler (yagee)
033     * @version $Revision: 13468 $ $Date: 2008-04-11 09:14:19 +0000 (Fr, 11 Apr
034     *          2008) $
035     * @since 2.0
036     */
037    public class MCRCategoryID implements Serializable {
038    
039        private static final long serialVersionUID = -5672923571406252855L;
040    
041        private String rootID;
042    
043        private String ID;
044    
045        MCRCategoryID() {
046            super();
047        }
048    
049        /**
050         * @param rootID
051         *            aka Classification ID
052         * @param id
053         *            aka Category ID
054         */
055        public MCRCategoryID(String rootID, String id) {
056            super();
057            setID(id);
058            setRootID(rootID);
059        }
060    
061        public static MCRCategoryID rootID(String rootID) {
062            String root = rootID.intern();
063            return new MCRCategoryID(root, "");
064        }
065    
066        public boolean isRootID() {
067            return ID == null || ID.equals("");
068        }
069    
070        /*
071         * (non-Javadoc)
072         * 
073         * @see java.lang.Object#hashCode()
074         */
075        @Override
076        public int hashCode() {
077            final int PRIME = 31;
078            int result = 1;
079            result = PRIME * result + ((ID == null || ID.length() == 0) ? 0 : ID.hashCode());
080            result = PRIME * result + ((rootID == null) ? 0 : rootID.hashCode());
081            return result;
082        }
083    
084        /*
085         * (non-Javadoc)
086         * 
087         * @see java.lang.Object#equals(java.lang.Object)
088         */
089        @Override
090        public boolean equals(Object obj) {
091            if (this == obj)
092                return true;
093            if (obj == null)
094                return false;
095            if (getClass() != obj.getClass())
096                return false;
097            final MCRCategoryID other = (MCRCategoryID) obj;
098            if (ID == null) {
099                if (other.ID != null && other.ID.length() > 0)
100                    return false;
101            } else if (!ID.equals(other.ID) && (ID.length() > 0 || (other.ID != null && other.ID.length() >= 0)))
102                return false;
103            if (rootID == null) {
104                if (other.rootID != null)
105                    return false;
106            } else if (!rootID.equals(other.rootID))
107                return false;
108            return true;
109        }
110    
111        /**
112         * @return the ID
113         */
114        public String getID() {
115            return ID == null ? "" : ID;
116        }
117    
118        /**
119         * @param id
120         *            the ID to set
121         */
122        void setID(String id) {
123            ID = id;
124        }
125    
126        /**
127         * @return the rootID
128         */
129        public String getRootID() {
130            return rootID;
131        }
132    
133        /**
134         * @param rootID
135         *            the rootID to set
136         */
137        void setRootID(String rootID) {
138            this.rootID = rootID.intern();
139        }
140    
141        /*
142         * (non-Javadoc)
143         * 
144         * @see java.lang.Object#toString()
145         */
146        @Override
147        public String toString() {
148            if (ID == null || ID.length() == 0)
149                return rootID;
150            StringBuilder str = new StringBuilder(this.rootID).append(':').append(ID);
151            return str.toString();
152        }
153    
154    }