Changeset 887 for MetadataBeans/trunk/src/main/java/org/eobjects/metamodel/schema/ImmutableRelationship.java
- Timestamp:
- 02/04/10 21:56:24 (2 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
MetadataBeans/trunk/src/main/java/org/eobjects/metamodel/schema/ImmutableRelationship.java
r886 r887 22 22 import org.apache.commons.lang.builder.EqualsBuilder; 23 23 import org.apache.commons.lang.builder.HashCodeBuilder; 24 25 /** 26 * Represents a relationship between two tables where one set of columns is the 27 * primary key, and another set is the foreign key. Relationship is unlike most 28 * of the MetaModel an immutable type. The immutability help ensure integrity of 29 * object-relationships. To create relationsips use the 30 * <code>createRelationship</code> method and to remove them use the 31 * <code>remove</remove> method. 32 * 33 * @see Table 34 * @see Column 35 */ 36 public class Relationship implements Serializable, Comparable<Relationship> { 24 import org.apache.commons.lang.builder.ToStringBuilder; 25 import org.apache.commons.lang.builder.ToStringStyle; 26 27 public class ImmutableRelationship implements Serializable, Relationship { 37 28 38 29 private static final long serialVersionUID = 238786848828528822L; 39 private Table _primaryTable;30 private MutableTable _primaryTable; 40 31 private Column[] _primaryColumns; 41 private Table _foreignTable;32 private MutableTable _foreignTable; 42 33 private Column[] _foreignColumns; 43 34 … … 54 45 public static Relationship createRelationship(Column[] primaryColumns, 55 46 Column[] foreignColumns) { 56 Table primaryTable = checkSameTable(primaryColumns);57 Table foreignTable = checkSameTable(foreignColumns);58 Relationship relation = new Relationship(primaryTable, primaryColumns,59 foreignTable, foreignColumns);47 MutableTable primaryTable = checkSameTable(primaryColumns); 48 MutableTable foreignTable = checkSameTable(foreignColumns); 49 ImmutableRelationship relation = new ImmutableRelationship(primaryTable, 50 primaryColumns, foreignTable, foreignColumns); 60 51 primaryTable.addRelationship(relation); 61 52 … … 74 65 } 75 66 76 private static Table checkSameTable(Column[] columns) {67 private static MutableTable checkSameTable(Column[] columns) { 77 68 if (columns == null || columns.length == 0) { 78 69 throw new IllegalArgumentException( … … 93 84 } 94 85 } 95 return table; 96 } 97 98 public void remove() { 86 if (table instanceof MutableTable) { 87 return (MutableTable) table; 88 } 89 throw new IllegalStateException( 90 "Can only create MutableRelationship for MutableTables"); 91 } 92 93 private void remove() { 99 94 _primaryTable.removeRelationship(this); 100 95 _foreignTable.removeRelationship(this); … … 108 103 * Prevent external instantiation 109 104 */ 110 private Relationship(Table primaryTable, Column[] primaryColumns, 111 Table foreignTable, Column[] foreignColumns) { 105 private ImmutableRelationship(MutableTable primaryTable, 106 Column[] primaryColumns, MutableTable foreignTable, 107 Column[] foreignColumns) { 112 108 _primaryTable = primaryTable; 113 109 _primaryColumns = primaryColumns; … … 116 112 } 117 113 114 @Override 118 115 public Table getPrimaryTable() { 119 116 return _primaryTable; 120 117 } 121 118 119 @Override 122 120 public Column[] getPrimaryColumns() { 123 121 return _primaryColumns; 124 122 } 125 123 124 @Override 126 125 public Table getForeignTable() { 127 126 return _foreignTable; 128 127 } 129 128 129 @Override 130 130 public Column[] getForeignColumns() { 131 131 return _foreignColumns; … … 134 134 @Override 135 135 public String toString() { 136 StringBuilder sb = new StringBuilder(); 137 sb.append("Relationship["); 138 sb.append("primaryTable=" + _primaryTable.getName()); 136 StringBuilder sb = new StringBuilder("{"); 139 137 Column[] columns = getPrimaryColumns(); 140 sb.append(",primaryColumns={");141 138 for (int i = 0; i < columns.length; i++) { 142 139 if (i != 0) { … … 146 143 } 147 144 sb.append("}"); 148 sb.append(",foreignTable=" + _foreignTable.getName()); 145 String primaryColumns = sb.toString(); 146 147 sb = new StringBuilder("{"); 149 148 columns = getForeignColumns(); 150 sb.append(",foreignColumns={");151 149 for (int i = 0; i < columns.length; i++) { 152 150 if (i != 0) { … … 156 154 } 157 155 sb.append("}"); 158 sb.append("]"); 159 return sb.toString(); 160 } 161 156 String foreignColumns = sb.toString(); 157 158 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 159 .append("primaryTable", _primaryTable.getName()).append( 160 "primaryColumns", primaryColumns).append( 161 "foreignTable", _foreignTable.getName()).append("foreignColumns", foreignColumns).toString(); 162 } 163 164 @Override 162 165 public int compareTo(Relationship that) { 163 166 CompareToBuilder ctb = new CompareToBuilder(); … … 198 201 * columns as a part of the relation 199 202 */ 203 @Override 200 204 public boolean containsColumnPair(Column pkColumn, Column fkColumn) { 201 205 if (pkColumn != null && fkColumn != null) { … … 209 213 return false; 210 214 } 215 216 public static void remove(Relationship relationship) { 217 ((ImmutableRelationship) relationship).remove(); 218 } 211 219 }
Note: See TracChangeset
for help on using the changeset viewer.
