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 Apache.CLI; 018 019import java.io.Serializable; 020import java.util.Collection; 021import java.util.Iterator; 022import java.util.LinkedHashMap; 023import java.util.Map; 024 025/** A group of mutually exclusive options. */ 026@Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="LICENSE") 027public class OptionGroup implements Serializable 028{ 029 // The serial version UID. 030 private static final long serialVersionUID = 1L; 031 032 // Hold the options 033 private final Map<String, Option> optionMap = new LinkedHashMap<>(); 034 035 // The name of the selected option 036 private String selected; 037 038 // Specified whether this group is required 039 private boolean required; 040 041 /** 042 * Add the specified {@code Option} to this group. 043 * @param option the option to add to this group 044 * @return this option group with the option added 045 */ 046 public OptionGroup addOption(final Option option) 047 { 048 // key - option name 049 // value - the option 050 optionMap.put(option.getKey(), option); 051 052 return this; 053 } 054 055 /** @return the names of the options in this group as a {@code Collection} */ 056 public Collection<String> getNames() 057 { 058 // the key set is the collection of names 059 return optionMap.keySet(); 060 } 061 062 /** @return the options in this group as a {@code Collection} */ 063 public Collection<Option> getOptions() 064 { 065 // the values are the collection of options 066 return optionMap.values(); 067 } 068 069 /** @return the selected option name */ 070 public String getSelected() 071 { return selected; } 072 073 /** 074 * Tests whether this option group is required. 075 * @return whether this option group is required 076 */ 077 public boolean isRequired() 078 { return required; } 079 080 /** @param required specifies if this group is required */ 081 public void setRequired(final boolean required) 082 { this.required = required; } 083 084 /** 085 * Sets the selected option of this group to {@code name}. 086 * @param option the option that is selected 087 * @throws AlreadySelectedException if an option from this group has already been selected. 088 */ 089 public void setSelected(final Option option) throws AlreadySelectedException 090 { 091 if (option == null) 092 { 093 // reset the option previously selected 094 selected = null; 095 return; 096 } 097 098 // if no option has already been selected or the 099 // same option is being reselected then set the 100 // selected member variable 101 if (selected != null && !selected.equals(option.getKey())) 102 throw new AlreadySelectedException(this, option); 103 104 selected = option.getKey(); 105 } 106 107 /** 108 * Returns the stringified version of this OptionGroup. 109 * @return the stringified representation of this group 110 */ 111 @Override 112 public String toString() 113 { 114 final StringBuilder buff = new StringBuilder(); 115 116 final Iterator<Option> iter = getOptions().iterator(); 117 118 buff.append("["); 119 120 while (iter.hasNext()) 121 { 122 final Option option = iter.next(); 123 124 if (option.getOpt() != null) 125 { 126 buff.append("-"); 127 buff.append(option.getOpt()); 128 } 129 130 else 131 { 132 buff.append("--"); 133 buff.append(option.getLongOpt()); 134 } 135 136 if (option.getDescription() != null) 137 { 138 buff.append(" "); 139 buff.append(option.getDescription()); 140 } 141 142 if (iter.hasNext()) buff.append(", "); 143 } 144 145 buff.append("]"); 146 147 return buff.toString(); 148 } 149}