Source for net.dpml.cli.option.HelpLineImpl

   1: /**
   2:  * Copyright 2003-2004 The Apache Software Foundation
   3:  * Copyright 2005 Stephen McConnell
   4:  *
   5:  * Licensed under the Apache License, Version 2.0 (the "License");
   6:  * you may not use this file except in compliance with the License.
   7:  * You may obtain a copy of the License at
   8:  *
   9:  *     http://www.apache.org/licenses/LICENSE-2.0
  10:  *
  11:  * Unless required by applicable law or agreed to in writing, software
  12:  * distributed under the License is distributed on an "AS IS" BASIS,
  13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14:  * See the License for the specific language governing permissions and
  15:  * limitations under the License.
  16:  */
  17: package net.dpml.cli.option;
  18: 
  19: import java.util.Comparator;
  20: import java.util.Set;
  21: 
  22: import net.dpml.cli.HelpLine;
  23: import net.dpml.cli.Option;
  24: 
  25: /**
  26:  * Represents a line in the help screen.
  27:  * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
  28:  * @version @PROJECT-VERSION@
  29:  */
  30: public class HelpLineImpl implements HelpLine
  31: {
  32:     /** The option that this HelpLineImpl describes */
  33:     private final Option m_option;
  34: 
  35:     /** The level of indenting for this item */
  36:     private final int m_indent;
  37: 
  38:     /** The help settings used to obtain the previous usage */
  39:     private transient Set m_cachedHelpSettings;
  40:     
  41:     /** The comparator used to obtain the previous usage */
  42:     private transient Comparator m_cachedComparator;
  43:     
  44:     /** The previously obtained usage */
  45:     private transient String m_cachedUsage;
  46:     
  47:     /**
  48:      * Creates a new HelpLineImpl to represent a particular Option in the online
  49:      * help.
  50:      * 
  51:      * @param option the Option that the HelpLineImpl describes
  52:      * @param indent the level of indentation for this line
  53:      */
  54:     public HelpLineImpl( final Option option, final int indent )
  55:     {
  56:         m_option = option;
  57:         m_indent = indent;
  58:     }
  59: 
  60:     /**
  61:      * @return The description of the option
  62:      */
  63:     public String getDescription() 
  64:     {
  65:         return m_option.getDescription();
  66:     }
  67: 
  68:     /**
  69:      * @return The level of indentation for this line
  70:      */
  71:     public int getIndent()
  72:     {
  73:         return m_indent;
  74:     }
  75: 
  76:     /**
  77:      * @return The Option that the help line relates to
  78:      */
  79:     public Option getOption()
  80:     {
  81:         return m_option;
  82:     }
  83:     
  84:     /**
  85:      * Builds a usage string for the option using the specified settings and 
  86:      * comparator.
  87:      * 
  88:      * @param helpSettings the settings to apply
  89:      * @param comparator a comparator to sort options when applicable
  90:      * @return the usage string
  91:      */
  92:     public String usage( final Set helpSettings, final Comparator comparator )
  93:     {
  94:         if( m_cachedUsage == null
  95:             || m_cachedHelpSettings != helpSettings
  96:             || m_cachedComparator != comparator ) 
  97:         {
  98:             
  99:             // cache the arguments to avoid redoing work
 100:             m_cachedHelpSettings = helpSettings;
 101:             m_cachedComparator = comparator;
 102:             
 103:             // build the new buffer
 104:             final StringBuffer buffer = new StringBuffer();
 105:             for( int i = 0; i < m_indent; ++i )
 106:             {
 107:                 buffer.append( "  " );
 108:             }
 109:             m_option.appendUsage( buffer, helpSettings, comparator );
 110:             
 111:             // cache the usage string
 112:             m_cachedUsage = buffer.toString();
 113:         }
 114:         return m_cachedUsage;
 115:     }
 116: }