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.ArrayList; 020import java.util.List; 021 022import org.apache.commons.cli2.Group; 023import org.apache.commons.cli2.Option; 024import org.apache.commons.cli2.option.GroupImpl; 025 026/** 027 * Builds Group instances 028 */ 029public class GroupBuilder { 030 031 private String name; 032 private String description; 033 private List options; 034 private int minimum; 035 private int maximum; 036 private boolean required; 037 038 /** 039 * Creates a new GroupBuilder 040 */ 041 public GroupBuilder() { 042 reset(); 043 } 044 045 /** 046 * Creates a new Group instance 047 * @return the new Group instance 048 */ 049 public Group create() { 050 final GroupImpl group = 051 new GroupImpl(options, name, description, minimum, maximum, required); 052 053 reset(); 054 055 return group; 056 } 057 058 /** 059 * Resets the builder. 060 * @return this builder 061 */ 062 public GroupBuilder reset() { 063 name = null; 064 description = null; 065 options = new ArrayList(); 066 minimum = 0; 067 maximum = Integer.MAX_VALUE; 068 required = true; 069 return this; 070 } 071 072 /** 073 * Use this option description. 074 * @param newDescription the description to use 075 * @return this builder 076 */ 077 public GroupBuilder withDescription(final String newDescription) { 078 this.description = newDescription; 079 return this; 080 } 081 082 /** 083 * Use this option name 084 * @param newName the name to use 085 * @return this builder 086 */ 087 public GroupBuilder withName(final String newName) { 088 this.name = newName; 089 return this; 090 } 091 092 /** 093 * A valid group requires at least this many options present 094 * @param newMinimum the minimum Options required 095 * @return this builder 096 */ 097 public GroupBuilder withMinimum(final int newMinimum) { 098 this.minimum = newMinimum; 099 return this; 100 } 101 102 /** 103 * A valid group requires at most this many options present 104 * @param newMaximum the maximum Options allowed 105 * @return this builder 106 */ 107 public GroupBuilder withMaximum(final int newMaximum) { 108 this.maximum = newMaximum; 109 return this; 110 } 111 112 /** 113 * Add this option to the group 114 * @param option the Option to add 115 * @return this builder 116 */ 117 public GroupBuilder withOption(final Option option) { 118 this.options.add(option); 119 return this; 120 } 121 122 /** 123 * Sets the required flag. This flag is evaluated for groups that are 124 * added to other groups as child groups. If set to <b>true</b> the 125 * minimum and maximum constraints of the child group are always evaluated. 126 * @param newRequired the required flag 127 * @return this builder 128 */ 129 public GroupBuilder withRequired(final boolean newRequired) { 130 this.required = newRequired; 131 return this; 132 } 133}