1 /*
2 * Copyright 2012 smartics, Kronseder & Reiner GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package de.smartics.maven.issue.command;
17
18 import java.io.Serializable;
19
20 import org.apache.commons.lang.StringUtils;
21
22 /**
23 * A concrete argument to a {@link Command}.
24 *
25 * @param <T> the type of command the argument belongs to.
26 */
27 public final class CommandArgument<T extends Command<T>> implements
28 Serializable
29 {
30 // ********************************* Fields *********************************
31
32 // --- constants ------------------------------------------------------------
33
34 /**
35 * The class version identifier.
36 * <p>
37 * The value of this constant is {@value}.
38 * </p>
39 */
40 private static final long serialVersionUID = 1L;
41
42 // --- members --------------------------------------------------------------
43
44 /**
45 * The name of the argument.
46 *
47 * @serial
48 */
49 private final CommandParameter<T> name;
50
51 /**
52 * The value of the argument.
53 *
54 * @serial
55 */
56 private final String value;
57
58 /**
59 * The flag to indicate that the value should not be printed.
60 */
61 private final boolean hide;
62
63 // ****************************** Initializer *******************************
64
65 // ****************************** Constructors ******************************
66
67 /**
68 * Default constructor.
69 *
70 * @param name the name of the argument.
71 * @param value the value of the argument.
72 * @param hide the flag to indicate that the value should not be printed.
73 */
74 private CommandArgument(final CommandParameter<T> name, final String value,
75 final boolean hide)
76 {
77 this.name = name;
78 this.value = value;
79 this.hide = hide;
80 }
81
82 // ****************************** Inner Classes *****************************
83
84 // ********************************* Methods ********************************
85
86 // --- init -----------------------------------------------------------------
87
88 // --- get&set --------------------------------------------------------------
89
90 /**
91 * Returns the name of the argument.
92 *
93 * @return the name of the argument.
94 */
95 public CommandParameter<T> getName()
96 {
97 return name;
98 }
99
100 /**
101 * Returns the value of the argument.
102 *
103 * @return the value of the argument.
104 */
105 public String getValue()
106 {
107 return value;
108 }
109
110 /**
111 * Checks if the argument has a non-blank value.
112 *
113 * @return <code>true</code> if the value is not blank, <code>false</code> if
114 * it is blank.
115 */
116 public boolean hasValue()
117 {
118 return StringUtils.isNotBlank(value);
119 }
120
121 // --- business -------------------------------------------------------------
122
123 /**
124 * Factory method to create an argument instance with non-hidden values.
125 *
126 * @param name the name of the argument.
127 * @param value the value of the argument.
128 * @return the new command argument instance.
129 * @param <T> the type of the command the created argument belongs to.
130 */
131 public static <T extends Command<T>> CommandArgument<T> create(
132 final CommandParameter<T> name, final String value)
133 {
134 return create(name, value, false);
135 }
136
137 /**
138 * Factory method to create an argument instance.
139 *
140 * @param name the name of the argument.
141 * @param value the value of the argument.
142 * @param hide the flag to indicate that the value should not be printed.
143 * @return the new command argument instance.
144 * @param <T> the type of the command the created argument belongs to.
145 */
146 public static <T extends Command<T>> CommandArgument<T> create(
147 final CommandParameter<T> name, final String value, final boolean hide)
148 {
149 return new CommandArgument<T>(name, value, hide);
150 }
151
152 // --- object basics --------------------------------------------------------
153
154 /**
155 * {@inheritDoc}
156 *
157 * @see java.lang.Object#toString()
158 */
159 @Override
160 public String toString()
161 {
162 final String pair = String.valueOf(name) + '=' + (hide ? "***" : value);
163 return pair;
164 }
165
166 }