@@ -56,7 +56,7 @@ public abstract class Model extends CallbackSupport implements Externalizable {
56
56
private final static Logger logger = LoggerFactory .getLogger (Model .class );
57
57
58
58
private Map <String , Object > attributes = new CaseInsensitiveMap <Object >();
59
- private final CaseInsensitiveSet dirtyAttributes = new CaseInsensitiveSet ();
59
+ private final Set < String > dirtyAttributeNames = new CaseInsensitiveSet ();
60
60
private boolean frozen = false ;
61
61
private MetaModel metaModelLocal ;
62
62
private ModelRegistry modelRegistryLocal ;
@@ -143,11 +143,11 @@ public static MetaModel getMetaModel() {
143
143
}
144
144
145
145
protected Map <String , Object > getAttributes () {
146
- return attributes ;
146
+ return Collections . unmodifiableMap ( attributes ) ;
147
147
}
148
148
149
- protected CaseInsensitiveSet getDirtyAttributes () {
150
- return dirtyAttributes ;
149
+ protected Set < String > dirtyAttributeNames () {
150
+ return Collections . unmodifiableSet ( dirtyAttributeNames ) ;
151
151
}
152
152
153
153
/**
@@ -160,7 +160,7 @@ protected CaseInsensitiveSet getDirtyAttributes() {
160
160
*/
161
161
public <T extends Model > T fromMap (Map input ) {
162
162
hydrate (input );
163
- dirtyAttributes .addAll (input .keySet ());
163
+ dirtyAttributeNames .addAll (input .keySet ());
164
164
return (T ) this ;
165
165
}
166
166
@@ -287,16 +287,17 @@ private <T extends Model> T setRaw(String attributeName, Object value) {
287
287
getMetaModelLocal ().checkAttributeOrAssociation (attributeName );
288
288
289
289
attributes .put (attributeName , value );
290
- dirtyAttributes .add (attributeName );
290
+ dirtyAttributeNames .add (attributeName );
291
291
return (T ) this ;
292
292
}
293
293
294
294
/**
295
295
* Will return true if any attribute of this instance was changed after latest load/save.
296
+ * (Instance state differs from state in DB)
296
297
* @return true if this instance was modified.
297
298
*/
298
299
public boolean isModified () {
299
- return !dirtyAttributes .isEmpty ();
300
+ return !dirtyAttributeNames .isEmpty ();
300
301
}
301
302
302
303
/**
@@ -309,6 +310,14 @@ public boolean isFrozen(){
309
310
return frozen ;
310
311
}
311
312
313
+ /**
314
+ * Synonym for {@link #isModified()}.
315
+ *
316
+ * @return true if this instance was modified.
317
+ */
318
+ public boolean modified () {
319
+ return isModified ();
320
+ }
312
321
313
322
/**
314
323
* Returns names of all attributes from this model.
@@ -1138,27 +1147,27 @@ public void setParent(Model parent) {
1138
1147
* @param other target model.
1139
1148
*/
1140
1149
public void copyTo (Model other ) {
1150
+ other .copyFrom (this );
1151
+ }
1152
+
1153
+ /**
1154
+ * Copies all attribute values (except for ID, created_at and updated_at) from other instance to this one.
1155
+ *
1156
+ * @param other source model.
1157
+ */
1158
+ public void copyFrom (Model other ) {
1141
1159
if (!getMetaModelLocal ().getTableName ().equals (other .getMetaModelLocal ().getTableName ())) {
1142
1160
throw new IllegalArgumentException ("can only copy between the same types" );
1143
1161
}
1144
-
1162
+ Map < String , Object > otherAttributes = other . getAttributes ();
1145
1163
for (String name : getMetaModelLocal ().getAttributeNamesSkipId ()) {
1146
- other . getAttributes (). put (name , get (name ));
1147
- other . getDirtyAttributes () .add (name );
1164
+ attributes . put (name , otherAttributes . get (name ));
1165
+ dirtyAttributeNames .add (name );
1148
1166
// Why not use setRaw() here? Does the same and avoids duplication of code... (Garagoth)
1149
1167
// other.setRaw(name, getRaw(name));
1150
1168
}
1151
1169
}
1152
1170
1153
- /**
1154
- * Copies all attribute values (except for ID, created_at and updated_at) from this instance to the other.
1155
- *
1156
- * @param other target model.
1157
- */
1158
- public void copyFrom (Model other ) {
1159
- other .copyTo (this );
1160
- }
1161
-
1162
1171
/**
1163
1172
* This method should be called from all instance methods for performance.
1164
1173
*
@@ -1196,7 +1205,7 @@ public void refresh() {
1196
1205
"this ID does not exist anymore. Stale model: " + this );
1197
1206
}
1198
1207
fresh .copyTo (this );
1199
- dirtyAttributes .clear ();
1208
+ dirtyAttributeNames .clear ();
1200
1209
}
1201
1210
1202
1211
/**
@@ -2425,7 +2434,7 @@ public void reset() {
2425
2434
*/
2426
2435
public void thaw (){
2427
2436
attributes .put (getIdName (), null );
2428
- dirtyAttributes .addAll (attributes .keySet ());
2437
+ dirtyAttributeNames .addAll (attributes .keySet ());
2429
2438
frozen = false ;
2430
2439
}
2431
2440
@@ -2551,7 +2560,7 @@ public boolean insert() {
2551
2560
attributes .put (metaModel .getVersionColumn (), 1 );
2552
2561
}
2553
2562
2554
- dirtyAttributes .clear (); // Clear all dirty attribute names as all were inserted. What about versionColumn ?
2563
+ dirtyAttributeNames .clear (); // Clear all dirty attribute names as all were inserted. What about versionColumn ?
2555
2564
fireAfterCreate ();
2556
2565
2557
2566
return done ;
@@ -2582,7 +2591,7 @@ private boolean update() {
2582
2591
MetaModel metaModel = getMetaModelLocal ();
2583
2592
StringBuilder query = new StringBuilder ().append ("UPDATE " ).append (metaModel .getTableName ()).append (" SET " );
2584
2593
Set <String > attributeNames = metaModel .getAttributeNamesSkipGenerated (manageTime );
2585
- attributeNames .retainAll (dirtyAttributes );
2594
+ attributeNames .retainAll (dirtyAttributeNames );
2586
2595
if (attributeNames .size () > 0 ) {
2587
2596
join (query , attributeNames , " = ?, " );
2588
2597
query .append (" = ?" );
@@ -2624,7 +2633,7 @@ private boolean update() {
2624
2633
if (metaModel .cached ()){
2625
2634
QueryCache .instance ().purgeTableCache (metaModel .getTableName ());
2626
2635
}
2627
- dirtyAttributes .clear ();
2636
+ dirtyAttributeNames .clear ();
2628
2637
fireAfterUpdate ();
2629
2638
return updated > 0 ;
2630
2639
}
0 commit comments