com.jenkov.mrpersister.impl.mapping
Class Key

java.lang.Object
  extended by com.jenkov.mrpersister.impl.mapping.Key
All Implemented Interfaces:
IKey

public class Key
extends java.lang.Object
implements IKey

Represents a key, either primary or foreign key, in a database table. The key consists of one or more columns. To create key values for a compound primary or foreign key, use the IKeyValue interface and the KeyValue implementation.

Author:
Jakob Jenkov - Copyright 2005 Jenkov Development
See Also:
IKeyValue, KeyValue

Field Summary
protected  java.util.Collection columns
           
protected  IObjectMapping objectMapping
           
protected  java.lang.String table
           
 
Constructor Summary
Key()
          Creates an empty key instance.
Key(java.util.Collection columns)
          Creates a key consisting of the given columns.
Key(java.lang.String column)
          Creates a key consisting of a single column.
Key(java.lang.String[] columns)
          Creates a key consisting of the given columns.
 
Method Summary
 void addColumn(java.lang.String column)
          Adds a column to this key mapping.
 boolean equals(java.lang.Object obj)
           
 java.lang.String getColumn()
          If this key only consists of one column, this method will return that column.
 java.util.Collection getColumns()
          Returns the collection of columns used in this key mapping.
 java.lang.String getTable()
          Returns the table this key mapping points to.
 boolean isValid(IKeyValue keyValue)
          Validates an IKeyValue instance against this key.
 void removeColumn(java.lang.String column)
          Removes a column from this key mapping
 void setColumns(java.util.Collection columns)
          Sets the collection of columns to use in this key mapping.
 void setTable(java.lang.String table)
          Sets the table this key points to.
 int size()
          Returns the number of columns in this key.
 IKeyValue toKeyValue(java.lang.Object keyValueObject)
          Wraps the given key value object in a IKeyValue instance.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

objectMapping

protected IObjectMapping objectMapping

table

protected java.lang.String table

columns

protected java.util.Collection columns
Constructor Detail

Key

public Key()
Creates an empty key instance. You can add columns to the key after creation.


Key

public Key(java.util.Collection columns)
Creates a key consisting of the given columns. You can add more columns to the key after creation.

Parameters:
columns - The names of the columns in the key.

Key

public Key(java.lang.String column)
Creates a key consisting of a single column. You can add more columns to the key after creation.

Parameters:
column - The name of the key column.

Key

public Key(java.lang.String[] columns)
Creates a key consisting of the given columns. You can add more columns to the key after creation.

Parameters:
columns - The names of the columns in the key.
Method Detail

getTable

public java.lang.String getTable()
Description copied from interface: IKey
Returns the table this key mapping points to. For a primary key this is the same table as the key exists in. For a foreign key this is the table the primary key the foreign key points to exists in.

Specified by:
getTable in interface IKey
Returns:
The table this key mapping points to.

setTable

public void setTable(java.lang.String table)
Description copied from interface: IKey
Sets the table this key points to. For a primary key this is the same table as the key exists in. For a foreign key this is the table the primary key the foreign key points to exists in.

Specified by:
setTable in interface IKey
Parameters:
table - The table this key mapping is to point to.

getColumns

public java.util.Collection getColumns()
Description copied from interface: IKey
Returns the collection of columns used in this key mapping.

Specified by:
getColumns in interface IKey
Returns:
The collection of columns used in this key mapping.

setColumns

public void setColumns(java.util.Collection columns)
Description copied from interface: IKey
Sets the collection of columns to use in this key mapping.

Specified by:
setColumns in interface IKey
Parameters:
columns - The collection of columns to use in this key mapping.

size

public int size()
Description copied from interface: IKey
Returns the number of columns in this key.

Specified by:
size in interface IKey
Returns:
The number of columns in this key.

addColumn

public void addColumn(java.lang.String column)
Description copied from interface: IKey
Adds a column to this key mapping.

Specified by:
addColumn in interface IKey
Parameters:
column - The column to add to this key mapping.

removeColumn

public void removeColumn(java.lang.String column)
Description copied from interface: IKey
Removes a column from this key mapping

Specified by:
removeColumn in interface IKey
Parameters:
column - The column to remove

isValid

public boolean isValid(IKeyValue keyValue)
Description copied from interface: IKey
Validates an IKeyValue instance against this key. If the key value contains values for all columns in this key, this method returns true. False if not.

If the key value contains values for columns that are not part of this key, these column values are ignored.

Specified by:
isValid in interface IKey
Parameters:
keyValue - The key value to validate.
Returns:
True if the key is valid. False if not.

getColumn

public java.lang.String getColumn()
                           throws PersistenceException
Description copied from interface: IKey
If this key only consists of one column, this method will return that column. Otherwise a PersistenceException is thrown.

Specified by:
getColumn in interface IKey
Returns:
The column of this key, if it consists of only one column.
Throws:
PersistenceException - If this key contains zero, or more than one column.

toKeyValue

public IKeyValue toKeyValue(java.lang.Object keyValueObject)
                     throws PersistenceException
Description copied from interface: IKey
Wraps the given key value object in a IKeyValue instance. If the object to be wrapped is already a IKeyValue instance it will be returned unchanged. This will only work if this IKey instance only consists of a single column.

Specified by:
toKeyValue in interface IKey
Parameters:
keyValueObject - The object to wrap in a IKeyValue instance.
Returns:
An IKeyValue instance wrapping the given object.
Throws:
PersistenceException - if this IKey instance is a compound key (multi column key), or if empty.

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object