KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > aikp > IntKey


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.aikp;
22
23 import com.methodhead.persistable.Key;
24 import com.methodhead.persistable.Persistable;
25 import com.methodhead.persistable.PersistableException;
26
27 /**
28  * A key that requires persistables that use it to define an integer field
29  * named <tt>id</tt>.
30  */

31 public class IntKey
32 implements
33   Key {
34
35   // constructors /////////////////////////////////////////////////////////////
36

37   // constants ////////////////////////////////////////////////////////////////
38

39   // classes //////////////////////////////////////////////////////////////////
40

41   public IntKey() {
42     value_ = 0;
43   }
44
45   public IntKey( int i ) {
46     value_ = i;
47   }
48
49   public IntKey( Object JavaDoc o ) {
50     try {
51       value_ = Integer.parseInt( o.toString() );
52     }
53     catch ( NumberFormatException JavaDoc e ) {
54     }
55   }
56
57   public IntKey( String JavaDoc s ) {
58     try {
59       value_ = Integer.parseInt( s );
60     }
61     catch ( NumberFormatException JavaDoc e ) {
62     }
63   }
64
65   // methods //////////////////////////////////////////////////////////////////
66

67   public boolean equals( Object JavaDoc o ) {
68     if ( o == null )
69       return false;
70
71     if ( !getClass().isInstance( o ) )
72       return false;
73
74     return value_ == ( ( IntKey )o ).value_;
75   }
76
77   public int hashCode() {
78     return value_;
79   }
80
81   public String JavaDoc toString() {
82     return String.valueOf( value_ );
83   }
84
85   public String JavaDoc getWhereClause() {
86     return "id=" + value_;
87   }
88
89   public void setProperties(
90     Persistable persistable ) {
91
92     persistable.set( "id", new Integer JavaDoc( value_ ) );
93   }
94
95   public void setKeyValue(
96     Persistable persistable )
97   throws
98     PersistableException {
99
100     value_ = ( ( Integer JavaDoc )persistable.get( "id" ) ).intValue();
101   }
102
103   // properties ///////////////////////////////////////////////////////////////
104

105   // attributes ///////////////////////////////////////////////////////////////
106

107   int value_;
108 }
109
110
111
112
113
Popular Tags