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