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.builder; 018 019import java.util.HashSet; 020import java.util.Set; 021 022import org.apache.commons.cli2.Argument; 023import org.apache.commons.cli2.Group; 024import org.apache.commons.cli2.option.Switch; 025import org.apache.commons.cli2.resource.ResourceConstants; 026import org.apache.commons.cli2.resource.ResourceHelper; 027 028/** 029 * Builds Switch instance. 030 */ 031public class SwitchBuilder { 032 private final String enabledPrefix; 033 private final String disabledPrefix; 034 private String description; 035 private String preferredName; 036 private Set aliases; 037 private boolean required; 038 private Argument argument; 039 private Group children; 040 private int id; 041 private Boolean switchDefault; 042 043 /** 044 * Creates a new SwitchBuilder using defaults. 045 * @see Switch#DEFAULT_ENABLED_PREFIX 046 * @see Switch#DEFAULT_DISABLED_PREFIX 047 */ 048 public SwitchBuilder() { 049 this(Switch.DEFAULT_ENABLED_PREFIX, Switch.DEFAULT_DISABLED_PREFIX); 050 } 051 052 /** 053 * Creates a new SwitchBuilder 054 * @param enabledPrefix the prefix to use for enabling the option 055 * @param disabledPrefix the prefix to use for disabling the option 056 * @throws IllegalArgumentException if either prefix is less than 1 057 * character long or the prefixes match 058 */ 059 public SwitchBuilder(final String enabledPrefix, 060 final String disabledPrefix) 061 throws IllegalArgumentException { 062 if ((enabledPrefix == null) || (enabledPrefix.length() < 1)) { 063 throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_ENABLED_PREFIX)); 064 } 065 066 if ((disabledPrefix == null) || (disabledPrefix.length() < 1)) { 067 throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_DISABLED_PREFIX)); 068 } 069 070 if (enabledPrefix.equals(disabledPrefix)) { 071 throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_IDENTICAL_PREFIXES)); 072 } 073 074 this.enabledPrefix = enabledPrefix; 075 this.disabledPrefix = disabledPrefix; 076 reset(); 077 } 078 079 /** 080 * Creates a new Switch instance 081 * @return a new Switch instance 082 */ 083 public Switch create() { 084 final Switch option = 085 new Switch(enabledPrefix, disabledPrefix, preferredName, aliases, description, 086 required, argument, children, id, switchDefault); 087 088 reset(); 089 090 return option; 091 } 092 093 /** 094 * Resets the builder. 095 * @return this builder 096 */ 097 public SwitchBuilder reset() { 098 description = null; 099 preferredName = null; 100 required = false; 101 aliases = new HashSet(); 102 argument = null; 103 children = null; 104 id = 0; 105 switchDefault = null; 106 107 return this; 108 } 109 110 /** 111 * Use this option description 112 * @param newDescription the description to use 113 * @return this builder 114 */ 115 public SwitchBuilder withDescription(final String newDescription) { 116 this.description = newDescription; 117 118 return this; 119 } 120 121 /** 122 * Use this option name. The first name is used as the preferred 123 * display name for the Command and then later names are used as aliases. 124 * 125 * @param name the name to use 126 * @return this builder 127 */ 128 public SwitchBuilder withName(final String name) { 129 if (preferredName == null) { 130 preferredName = name; 131 } else { 132 aliases.add(name); 133 } 134 135 return this; 136 } 137 138 /** 139 * Use this optionality 140 * @param newRequired true iff the Option is required 141 * @return this builder 142 */ 143 public SwitchBuilder withRequired(final boolean newRequired) { 144 this.required = newRequired; 145 146 return this; 147 } 148 149 /** 150 * Use this Argument 151 * @param newArgument the argument to use 152 * @return this builder 153 */ 154 public SwitchBuilder withArgument(final Argument newArgument) { 155 this.argument = newArgument; 156 157 return this; 158 } 159 160 /** 161 * Use this child Group 162 * @param newChildren the child Group to use 163 * @return this builder 164 */ 165 public SwitchBuilder withChildren(final Group newChildren) { 166 this.children = newChildren; 167 168 return this; 169 } 170 171 /** 172 * Sets the id 173 * 174 * @param newId 175 * the id of the Switch 176 * @return this SwitchBuilder 177 */ 178 public final SwitchBuilder withId(final int newId) { 179 this.id = newId; 180 181 return this; 182 } 183 184 /** 185 * Sets the default state for this switch 186 * 187 * @param newSwitchDefault the default state 188 * @return this SwitchBuilder 189 */ 190 public final SwitchBuilder withSwitchDefault(final Boolean newSwitchDefault) { 191 this.switchDefault = newSwitchDefault; 192 193 return this; 194 } 195}