001 /**
002 * Copyright 2003-2004 The Apache Software Foundation
003 * Copyright 2005 Stephen McConnell
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package net.dpml.cli.option;
018
019 import java.util.Comparator;
020 import java.util.Set;
021
022 import net.dpml.cli.HelpLine;
023 import net.dpml.cli.Option;
024
025 /**
026 * Represents a line in the help screen.
027 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
028 * @version 1.0.0
029 */
030 public class HelpLineImpl implements HelpLine
031 {
032 /** The option that this HelpLineImpl describes */
033 private final Option m_option;
034
035 /** The level of indenting for this item */
036 private final int m_indent;
037
038 /** The help settings used to obtain the previous usage */
039 private transient Set m_cachedHelpSettings;
040
041 /** The comparator used to obtain the previous usage */
042 private transient Comparator m_cachedComparator;
043
044 /** The previously obtained usage */
045 private transient String m_cachedUsage;
046
047 /**
048 * Creates a new HelpLineImpl to represent a particular Option in the online
049 * help.
050 *
051 * @param option the Option that the HelpLineImpl describes
052 * @param indent the level of indentation for this line
053 */
054 public HelpLineImpl( final Option option, final int indent )
055 {
056 m_option = option;
057 m_indent = indent;
058 }
059
060 /**
061 * @return The description of the option
062 */
063 public String getDescription()
064 {
065 return m_option.getDescription();
066 }
067
068 /**
069 * @return The level of indentation for this line
070 */
071 public int getIndent()
072 {
073 return m_indent;
074 }
075
076 /**
077 * @return The Option that the help line relates to
078 */
079 public Option getOption()
080 {
081 return m_option;
082 }
083
084 /**
085 * Builds a usage string for the option using the specified settings and
086 * comparator.
087 *
088 * @param helpSettings the settings to apply
089 * @param comparator a comparator to sort options when applicable
090 * @return the usage string
091 */
092 public String usage( final Set helpSettings, final Comparator comparator )
093 {
094 if( m_cachedUsage == null
095 || m_cachedHelpSettings != helpSettings
096 || m_cachedComparator != comparator )
097 {
098
099 // 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 }