001    package com.sptci.echo2.style;
002    
003    import java.util.Map;
004    import java.util.concurrent.ConcurrentHashMap;
005    
006    /**
007     * A custom extent class to use in styles.  It is recomended that you
008     * use the {@link #getInstance} method or its overloaded derivatives to avoid
009     * replication across sessions.
010     *
011     * <p>The following shows the recommended way to use this class:</p>
012     * <pre>
013     *   import com.sptci.echo2.style.Extent;
014     *
015     *     ...
016     *     Extent width = Extent.getInstance( 400 );
017     *     Extent height = Extent.getInstance( 300 );
018     * </pre>
019     *
020     * <p>&copy; Copyright 2007 Sans Pareil Technologies, Inc.</p>
021     * @author Rakesh Vidyadharan 2007-06-08
022     * @version $Id: Extent.java 3334 2007-06-08 16:25:49Z rakesh $
023     */
024    public class Extent extends nextapp.echo2.app.Extent
025    {
026      /**
027       * The map used to maintain a cache of extent instances required by the
028       * application.
029       */
030      protected static final Map<String,Extent> cache =
031          new ConcurrentHashMap<String,Extent>();
032      
033      /**
034       * Creates a new instance of the specified size in {@link #PX} units.
035       *
036       * @deprecated Use {@link #getInstance( int )} instead.
037       * @param size The size represented by this extent.
038       */
039      @Deprecated public Extent( final int size )
040      {
041        super( size );
042      }
043    
044      /**
045       * Creates a new instance of the specified size in the specified units.
046       *
047       * @deprecated Use {@link #getInstance( int, int )} instead.
048       * @param size The size represented by this extent.
049       * @param units The unit of measure used to express this extent.
050       */
051      @Deprecated public Extent( final int size, final int units )
052      {
053        super( size, units );
054      }
055      
056      /**
057       * Fetch an extent instance that represents the specified size in the default
058       * units.
059       *
060       * @see #getInstance( int, int )
061       * @param size The size represented by this extent.
062       */
063      public static Extent getInstance( final int size )
064      {
065        return getInstance( size, PX );
066      }
067      
068      /**
069       * Fetch an extent instance that represents the specified size in the
070       * specified units.
071       *
072       * @see #getKey( int, int )
073       * @param size The size represented by this extent.
074       * @param units The unit of measure used to express this extent.
075       */
076      public static Extent getInstance( final int size, final int units )
077      {
078        final String key = getKey( size, units );
079        Extent extent = cache.get( key );
080        
081        if ( extent == null )
082        {
083          extent = new Extent( size, units );
084          cache.put( key, extent );
085        }
086        
087        return extent;
088      }
089      
090      /**
091       * Return a unique key to use for the specified extent size expressed in the
092       * specified units.
093       *
094       * @param size The size for which a key is to be generated.
095       * @param units The unit of measure used to express the <code>size</code>.
096       */
097      protected static String getKey( final int size, final int units )
098      {
099        StringBuilder builder = new StringBuilder();
100        builder.append( size ).append( "::" ).append( units );
101        return builder.toString();
102      }
103    }