|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.sql.results.internal; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Objects; |
| 12 | + |
| 13 | +import org.hibernate.spi.NavigablePath; |
| 14 | +import org.hibernate.sql.results.ResultsLogger; |
| 15 | +import org.hibernate.sql.results.graph.Initializer; |
| 16 | + |
| 17 | + |
| 18 | +/** |
| 19 | + * This is in all practical terms a {@code Map<NavigablePath, Initializer>} |
| 20 | + * but wrapping an HashMap so to keep the client code readable as we need |
| 21 | + * to: |
| 22 | + * a) have a way to log all initializers |
| 23 | + * b) prevent type pollution from happening on Initializer retrieval |
| 24 | + * I also consider it good practice to only expose the minimal set of |
| 25 | + * operations the client actually needs. |
| 26 | + */ |
| 27 | +public final class NavigablePathMapToInitializer { |
| 28 | + |
| 29 | + private HashMap<NavigablePath, InitializerHolder> map = null; |
| 30 | + |
| 31 | + public Initializer get(final NavigablePath navigablePath) { |
| 32 | + if ( map != null && navigablePath != null ) { |
| 33 | + final InitializerHolder h = map.get( navigablePath ); |
| 34 | + if ( h != null ) { |
| 35 | + return h.initializer; |
| 36 | + } |
| 37 | + } |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + public void put(final NavigablePath navigablePath, final Initializer initializer) { |
| 42 | + Objects.requireNonNull( navigablePath ); |
| 43 | + Objects.requireNonNull( initializer ); |
| 44 | + if ( map == null ) { |
| 45 | + map = new HashMap<>(); |
| 46 | + } |
| 47 | + map.put( navigablePath, new InitializerHolder( initializer ) ); |
| 48 | + } |
| 49 | + |
| 50 | + public void logInitializers() { |
| 51 | + ResultsLogger logger = ResultsLogger.RESULTS_MESSAGE_LOGGER; |
| 52 | + if ( !logger.isDebugEnabled() ) { |
| 53 | + return; |
| 54 | + } |
| 55 | + if ( map == null ) { |
| 56 | + logger.debug( "Initializer list is empty" ); |
| 57 | + } |
| 58 | + else { |
| 59 | + //Apparently we want to log this on multiple lines (existing code did this - not sure if that was by design): |
| 60 | + //using a StringBuilder to avoid potentially interleaving the logs from different operations. |
| 61 | + final StringBuilder sb = new StringBuilder( "Initializer list:\n" ); |
| 62 | + for ( Map.Entry<NavigablePath, InitializerHolder> holderEntry : map.entrySet() ) { |
| 63 | + final NavigablePath navigablePath = holderEntry.getKey(); |
| 64 | + final Initializer initializer = holderEntry.getValue().initializer; |
| 65 | + String formatted = String.format( |
| 66 | + " %s -> %s@%s (%s)", |
| 67 | + navigablePath, |
| 68 | + initializer, |
| 69 | + initializer.hashCode(), |
| 70 | + initializer.getInitializedPart() |
| 71 | + ); |
| 72 | + sb.append( '\t' ); |
| 73 | + sb.append( formatted ); |
| 74 | + sb.append( '\n' ); |
| 75 | + } |
| 76 | + logger.debug( sb.toString() ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + //Custom holder to avoid type pollution: |
| 81 | + //we make the type explicit, and this is a concrete class. |
| 82 | + private static final class InitializerHolder { |
| 83 | + final Initializer initializer; |
| 84 | + |
| 85 | + private InitializerHolder(final Initializer init) { |
| 86 | + this.initializer = init; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments