001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. 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 */ 017package org.apache.commons.cli2.option; 018 019import java.util.Comparator; 020import java.util.Set; 021 022import org.apache.commons.cli2.HelpLine; 023import org.apache.commons.cli2.Option; 024 025/** 026 * Represents a line in the help screen. 027 */ 028public class HelpLineImpl implements HelpLine { 029 030 /** The option that this HelpLineImpl describes */ 031 private final Option option; 032 033 /** The level of indenting for this item */ 034 private final int indent; 035 036 /** The help settings used to obtain the previous usage */ 037 private transient Set cachedHelpSettings; 038 039 /** The comparator used to obtain the previous usage */ 040 private transient Comparator cachedComparator; 041 042 /** The previously obtained usage */ 043 private transient String cachedUsage; 044 045 /** 046 * Creates a new HelpLineImpl to represent a particular Option in the online 047 * help. 048 * 049 * @param option 050 * Option that the HelpLineImpl describes 051 * @param indent 052 * Level of indentation for this line 053 */ 054 public HelpLineImpl(final Option option, final int indent) { 055 this.option = option; 056 this.indent = indent; 057 } 058 059 /** 060 * @return The description of the option 061 */ 062 public String getDescription() { 063 return option.getDescription(); 064 } 065 066 /** 067 * @return The level of indentation for this line 068 */ 069 public int getIndent() { 070 return indent; 071 } 072 073 /** 074 * @return The Option that the help line relates to 075 */ 076 public Option getOption() { 077 return option; 078 } 079 080 /** 081 * Builds a usage string for the option using the specified settings and 082 * comparator. 083 * 084 * 085 * @param helpSettings the settings to apply 086 * @param comparator a comparator to sort options when applicable 087 * @return the usage string 088 */ 089 public String usage(final Set helpSettings, final Comparator comparator) { 090 if (cachedUsage == null 091 || cachedHelpSettings != helpSettings 092 || cachedComparator != comparator) { 093 094 // cache the arguments to avoid redoing work 095 cachedHelpSettings = helpSettings; 096 cachedComparator = comparator; 097 098 // build the new buffer 099 final StringBuffer buffer = new StringBuffer(); 100 for (int i = 0; i < indent; ++i) { 101 buffer.append(" "); 102 } 103 option.appendUsage(buffer, helpSettings, comparator); 104 105 // cache the usage string 106 cachedUsage = buffer.toString(); 107 } 108 return cachedUsage; 109 } 110}