Skip to content

Commit aa5d408

Browse files
aepedrazaSanne
authored andcommitted
HHH-10661 Reduce code duplication in serialize method of StatefulPersistenceContext
1 parent ff9f6ef commit aa5d408

File tree

1 file changed

+68
-99
lines changed

1 file changed

+68
-99
lines changed

hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java

Lines changed: 68 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,117 +1572,86 @@ public void serialize(ObjectOutputStream oos) throws IOException {
15721572
oos.writeBoolean( defaultReadOnly );
15731573
oos.writeBoolean( hasNonReadOnlyEntities );
15741574

1575-
if ( entitiesByKey == null ) {
1576-
oos.writeInt( 0 );
1577-
}
1578-
else {
1579-
oos.writeInt( entitiesByKey.size() );
1580-
if ( LOG.isTraceEnabled() ) {
1581-
LOG.trace( "Starting serialization of [" + entitiesByKey.size() + "] entitiesByKey entries" );
1582-
}
1583-
for ( Map.Entry<EntityKey,Object> entry : entitiesByKey.entrySet() ) {
1584-
entry.getKey().serialize( oos );
1585-
oos.writeObject( entry.getValue() );
1586-
}
1587-
}
1588-
1589-
if ( entitiesByUniqueKey == null ) {
1590-
oos.writeInt( 0 );
1591-
}
1592-
else {
1593-
oos.writeInt( entitiesByUniqueKey.size() );
1594-
if ( LOG.isTraceEnabled() ) {
1595-
LOG.trace( "Starting serialization of [" + entitiesByUniqueKey.size() + "] entitiesByUniqueKey entries" );
1596-
}
1597-
for ( Map.Entry<EntityUniqueKey,Object> entry : entitiesByUniqueKey.entrySet() ) {
1598-
entry.getKey().serialize( oos );
1599-
oos.writeObject( entry.getValue() );
1600-
}
1601-
}
1602-
1603-
if ( proxiesByKey == null ) {
1604-
oos.writeInt( 0 );
1605-
}
1606-
else {
1607-
oos.writeInt( proxiesByKey.size() );
1608-
if ( LOG.isTraceEnabled() ) {
1609-
LOG.trace( "Starting serialization of [" + proxiesByKey.size() + "] proxiesByKey entries" );
1610-
}
1611-
for ( Map.Entry<EntityKey,Object> entry : proxiesByKey.entrySet() ) {
1612-
entry.getKey().serialize( oos );
1613-
oos.writeObject( entry.getValue() );
1614-
}
1615-
}
1616-
1617-
if ( entitySnapshotsByKey == null ) {
1618-
oos.writeInt( 0 );
1619-
}
1620-
else {
1621-
oos.writeInt( entitySnapshotsByKey.size() );
1622-
if ( LOG.isTraceEnabled() ) {
1623-
LOG.trace( "Starting serialization of [" + entitySnapshotsByKey.size() + "] entitySnapshotsByKey entries" );
1624-
}
1625-
for ( Map.Entry<EntityKey,Object> entry : entitySnapshotsByKey.entrySet() ) {
1626-
entry.getKey().serialize( oos );
1627-
oos.writeObject( entry.getValue() );
1628-
}
1629-
}
1575+
final Serializer<Map.Entry<EntityKey, Object>> entityKeySerializer = (entry, stream) -> {
1576+
entry.getKey().serialize( stream );
1577+
stream.writeObject( entry.getValue() );
1578+
};
1579+
1580+
writeMapToStream( entitiesByKey, oos, "entitiesByKey", entityKeySerializer );
1581+
writeMapToStream(
1582+
entitiesByUniqueKey,
1583+
oos, "entitiesByUniqueKey", (entry, stream) -> {
1584+
entry.getKey().serialize( stream );
1585+
stream.writeObject( entry.getValue() );
1586+
}
1587+
);
1588+
writeMapToStream( proxiesByKey, oos, "proxiesByKey", entityKeySerializer );
1589+
writeMapToStream( entitySnapshotsByKey, oos, "entitySnapshotsByKey", entityKeySerializer );
16301590

16311591
entityEntryContext.serialize( oos );
1592+
writeMapToStream(
1593+
collectionsByKey,
1594+
oos,
1595+
"collectionsByKey",
1596+
(entry, stream) -> {
1597+
entry.getKey().serialize( stream );
1598+
stream.writeObject( entry.getValue() );
1599+
}
1600+
);
1601+
writeMapToStream(
1602+
collectionEntries,
1603+
oos,
1604+
"collectionEntries",
1605+
(entry, stream) -> {
1606+
stream.writeObject( entry.getKey() );
1607+
entry.getValue().serialize( stream );
1608+
}
1609+
);
1610+
writeMapToStream(
1611+
arrayHolders,
1612+
oos,
1613+
"arrayHolders",
1614+
(entry, stream) -> {
1615+
stream.writeObject( entry.getKey() );
1616+
stream.writeObject( entry.getValue() );
1617+
}
1618+
);
1619+
writeCollectionToStream( nullifiableEntityKeys, oos, "nullifiableEntityKey", EntityKey::serialize );
1620+
}
16321621

1633-
if ( collectionsByKey == null ) {
1634-
oos.writeInt( 0 );
1635-
}
1636-
else {
1637-
oos.writeInt( collectionsByKey.size() );
1638-
if ( LOG.isTraceEnabled() ) {
1639-
LOG.trace( "Starting serialization of [" + collectionsByKey.size() + "] collectionsByKey entries" );
1640-
}
1641-
for ( Map.Entry<CollectionKey, PersistentCollection> entry : collectionsByKey.entrySet() ) {
1642-
entry.getKey().serialize( oos );
1643-
oos.writeObject( entry.getValue() );
1644-
}
1645-
}
1622+
private interface Serializer<E> {
16461623

1647-
if ( collectionEntries == null ) {
1648-
oos.writeInt( 0 );
1649-
}
1650-
else {
1651-
oos.writeInt( collectionEntries.size() );
1652-
if ( LOG.isTraceEnabled() ) {
1653-
LOG.trace( "Starting serialization of [" + collectionEntries.size() + "] collectionEntries entries" );
1654-
}
1655-
for ( Map.Entry<PersistentCollection,CollectionEntry> entry : collectionEntries.entrySet() ) {
1656-
oos.writeObject( entry.getKey() );
1657-
entry.getValue().serialize( oos );
1658-
}
1659-
}
1624+
void serialize(E element, ObjectOutputStream oos) throws IOException;
1625+
}
16601626

1661-
if ( arrayHolders == null ) {
1627+
private <K, V> void writeMapToStream(
1628+
Map<K, V> map,
1629+
ObjectOutputStream oos,
1630+
String keysName,
1631+
Serializer<Entry<K, V>> serializer) throws IOException {
1632+
if ( map == null ) {
16621633
oos.writeInt( 0 );
16631634
}
16641635
else {
1665-
oos.writeInt( arrayHolders.size() );
1666-
if ( LOG.isTraceEnabled() ) {
1667-
LOG.trace( "Starting serialization of [" + arrayHolders.size() + "] arrayHolders entries" );
1668-
}
1669-
for ( Map.Entry<Object,PersistentCollection> entry : arrayHolders.entrySet() ) {
1670-
oos.writeObject( entry.getKey() );
1671-
oos.writeObject( entry.getValue() );
1672-
}
1636+
writeCollectionToStream( map.entrySet(), oos, keysName, serializer );
16731637
}
1638+
}
16741639

1675-
if ( nullifiableEntityKeys == null ) {
1640+
private <E> void writeCollectionToStream(
1641+
Collection<E> collection,
1642+
ObjectOutputStream oos,
1643+
String keysName,
1644+
Serializer<E> serializer) throws IOException {
1645+
if ( collection == null ) {
16761646
oos.writeInt( 0 );
16771647
}
16781648
else {
1679-
final int size = nullifiableEntityKeys.size();
1649+
oos.writeInt( collection.size() );
16801650
if ( LOG.isTraceEnabled() ) {
1681-
LOG.trace( "Starting serialization of [" + size + "] nullifiableEntityKey entries" );
1651+
LOG.trace( "Starting serialization of [" + collection.size() + "] " + keysName + " entries" );
16821652
}
1683-
oos.writeInt( size );
1684-
for ( EntityKey entry : nullifiableEntityKeys ) {
1685-
entry.serialize( oos );
1653+
for ( E entry : collection ) {
1654+
serializer.serialize( entry, oos );
16861655
}
16871656
}
16881657
}
@@ -2142,9 +2111,9 @@ public Object[] removeLocalNaturalIdCrossReference(EntityPersister persister, Se
21422111
final Object[] naturalIdValues = getNaturalIdValues( state, persister );
21432112

21442113
final Object[] localNaturalIdValues = getNaturalIdXrefDelegate().removeNaturalIdCrossReference(
2145-
persister,
2146-
id,
2147-
naturalIdValues
2114+
persister,
2115+
id,
2116+
naturalIdValues
21482117
);
21492118

21502119
return localNaturalIdValues != null ? localNaturalIdValues : naturalIdValues;

0 commit comments

Comments
 (0)