001    /*-----------------------------------------------------------------------
002     *  Copyright (C) 2006
003     *  Associated Universities, Inc. Washington DC, USA.
004     *  This program is free software; you can redistribute it and/or
005     *  modify it under the terms of the GNU General Public License as
006     *  published by the Free Software Foundation; either version 2 of
007     *  the License, or (at your option) any later version.
008     *
009     *  This program is distributed in the hope that it will be useful,
010     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *  GNU General Public License for more details.
013     *
014     *  Correspondence concerning this software should be addressed as follows:
015     *         Internet email: switz@nrao.edu
016     *         Postal address: User Database
017     *                         National Radio Astronomy Observatory
018     *                         Post Office Box 0
019     *                         Socorro, NM 87801  USA
020     *-----------------------------------------------------------------------*/
021    package edu.nrao.sss.model.user;
022    
023    import java.io.Serializable;
024    
025    public abstract class ObjectId implements Serializable {
026    
027      private static final long serialVersionUID = -1L;
028    
029            private int hashCode;
030    
031            private int id;
032    
033            public ObjectId() {
034                    this.id = this.hashCode = 0;
035            }
036    
037            public ObjectId(ObjectId objectId) {
038                    setId(objectId.getId());
039            }
040    
041            public abstract boolean equals(Object obj);
042    
043            public int getId() {
044                    return id;
045            }
046    
047            public int hashCode() {
048                    if (this.hashCode == Integer.MIN_VALUE) {
049                            return (int) this.getId();
050                    }
051                    return this.hashCode;
052            }
053    
054            public boolean isNew() {
055                    return (this.hashCode == Integer.MIN_VALUE) ? false : true;
056            }
057    
058            public void setId(int id) {
059                    if (id < 0) {
060                            this.id = 0;
061                            this.hashCode = 0;
062                    } else {
063                            this.id = id;
064                            this.hashCode = Integer.MIN_VALUE;
065                    }
066            }
067    
068    }