001 package com.croftsoft.core.util.id; 002 003 /********************************************************************* 004 * An Id implementation that uses an integer as its value. 005 * 006 * <p> 007 * Maintains a static variable "next" which it increments with each 008 * new IntId object created using the zero-argument constructor. 009 * This ensures uniqueness among IntegerId objects created within the 010 * current process. 011 * </p> 012 * 013 * @version 014 * $Id: IntId.java,v 1.3 2008/09/20 05:11:34 croft Exp $ 015 * @since 016 * 2000-01-16 017 * @author 018 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 019 *********************************************************************/ 020 021 public class IntId 022 implements Id 023 ////////////////////////////////////////////////////////////////////// 024 ////////////////////////////////////////////////////////////////////// 025 { 026 027 private static final long serialVersionUID = 0L; 028 029 // 030 031 private static int next = 0; 032 033 // 034 035 private int i; 036 037 ////////////////////////////////////////////////////////////////////// 038 // constructor methods 039 ////////////////////////////////////////////////////////////////////// 040 041 public IntId ( int i ) 042 ////////////////////////////////////////////////////////////////////// 043 { 044 this.i = i; 045 } 046 047 /********************************************************************* 048 * this ( next++ ); 049 *********************************************************************/ 050 public IntId ( ) 051 ////////////////////////////////////////////////////////////////////// 052 { 053 this ( next ( ) ); 054 } 055 056 ////////////////////////////////////////////////////////////////////// 057 // accessor method 058 ////////////////////////////////////////////////////////////////////// 059 060 public int getI ( ) { return i; } 061 062 ////////////////////////////////////////////////////////////////////// 063 // Id interface methods 064 ////////////////////////////////////////////////////////////////////// 065 066 @Override 067 public boolean equals ( Object other ) 068 ////////////////////////////////////////////////////////////////////// 069 { 070 if ( other == null ) return false; 071 072 if ( !getClass ( ).equals ( other.getClass ( ) ) ) return false; 073 074 return ( i == ( ( IntId ) other ).i ); 075 } 076 077 @Override 078 public int hashCode ( ) 079 ////////////////////////////////////////////////////////////////////// 080 { 081 return i; 082 } 083 084 @Override 085 public Object clone ( ) 086 ////////////////////////////////////////////////////////////////////// 087 { 088 try 089 { 090 return super.clone ( ); 091 } 092 catch ( CloneNotSupportedException ex ) 093 { 094 // This will never happen. 095 096 throw new RuntimeException ( ); 097 } 098 } 099 100 ////////////////////////////////////////////////////////////////////// 101 // Other methods 102 ////////////////////////////////////////////////////////////////////// 103 104 /********************************************************************* 105 * return Integer.toString ( i ); 106 *********************************************************************/ 107 @Override 108 public String toString ( ) 109 ////////////////////////////////////////////////////////////////////// 110 { 111 return Integer.toString ( i ); 112 } 113 114 ////////////////////////////////////////////////////////////////////// 115 // private methods 116 ////////////////////////////////////////////////////////////////////// 117 118 private static synchronized int next ( ) { return next++; } 119 120 ////////////////////////////////////////////////////////////////////// 121 ////////////////////////////////////////////////////////////////////// 122 }