001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.aikp;
022:
023: import com.methodhead.persistable.Key;
024: import com.methodhead.persistable.Persistable;
025: import com.methodhead.persistable.PersistableException;
026:
027: /**
028: * A key that requires persistables that use it to define an integer field
029: * named <tt>id</tt>.
030: */
031: public class IntKey implements Key {
032:
033: // constructors /////////////////////////////////////////////////////////////
034:
035: // constants ////////////////////////////////////////////////////////////////
036:
037: // classes //////////////////////////////////////////////////////////////////
038:
039: public IntKey() {
040: value_ = 0;
041: }
042:
043: public IntKey(int i) {
044: value_ = i;
045: }
046:
047: public IntKey(Object o) {
048: try {
049: value_ = Integer.parseInt(o.toString());
050: } catch (NumberFormatException e) {
051: }
052: }
053:
054: public IntKey(String s) {
055: try {
056: value_ = Integer.parseInt(s);
057: } catch (NumberFormatException e) {
058: }
059: }
060:
061: // methods //////////////////////////////////////////////////////////////////
062:
063: public boolean equals(Object o) {
064: if (o == null)
065: return false;
066:
067: if (!getClass().isInstance(o))
068: return false;
069:
070: return value_ == ((IntKey) o).value_;
071: }
072:
073: public int hashCode() {
074: return value_;
075: }
076:
077: public String toString() {
078: return String.valueOf(value_);
079: }
080:
081: public String getWhereClause() {
082: return "id=" + value_;
083: }
084:
085: public void setProperties(Persistable persistable) {
086:
087: persistable.set("id", new Integer(value_));
088: }
089:
090: public void setKeyValue(Persistable persistable)
091: throws PersistableException {
092:
093: value_ = ((Integer) persistable.get("id")).intValue();
094: }
095:
096: // properties ///////////////////////////////////////////////////////////////
097:
098: // attributes ///////////////////////////////////////////////////////////////
099:
100: int value_;
101: }
|