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 *      https://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.validator;
018
019import java.io.Serializable;
020import java.text.MessageFormat;
021
022/**
023 * <p>
024 * A default argument or an argument for a
025 * specific validator definition (ex: required)
026 * can be stored to pass into a message as parameters.  This can be used in a
027 * pluggable validator for constructing locale
028 * sensitive messages by using {@link MessageFormat}
029 * or an equivalent class.  The resource field can be
030 * used to determine if the value stored in the argument
031 * is a value to be retrieved from a locale sensitive
032 * message retrieval system like {@code java.util.PropertyResourceBundle}.
033 * The resource field defaults to 'true'.
034 * </p>
035 * <p>Instances of this class are configured with an &lt;arg&gt; xml element.</p>
036 */
037//TODO mutable non-private fields
038public class Arg implements Cloneable, Serializable {
039
040    private static final long serialVersionUID = -8922606779669839294L;
041
042    /**
043     * The resource bundle name that this Arg's {@code key} should be
044     * resolved in (optional).
045     *
046     * @since 1.1
047     */
048    protected String bundle;
049
050    /**
051     * The key or value of the argument.
052     */
053    protected String key;
054
055    /**
056     * The name dependency that this argument goes with (optional).
057     */
058    protected String name;
059
060    /**
061     * This argument's position in the message. Set position=0 to
062     * make a replacement in this string: "some msg {0}".
063     *
064     * @since 1.1
065     */
066    protected int position = -1;
067
068    /**
069     * Whether or not the key is a message resource (optional).  Defaults to
070     * true.  If it is 'true', the value will try to be resolved as a message
071     * resource.
072     */
073    protected boolean resource = true;
074
075    /**
076     * Constructs a new instance.
077     */
078    public Arg() {
079        // empty
080    }
081
082    /**
083     * Creates and returns a copy of this object.
084     *
085     * @return A copy of this object.
086     */
087    @Override
088    public Object clone() {
089        try {
090            return super.clone();
091        } catch (final CloneNotSupportedException e) {
092            throw new UnsupportedOperationException(e.toString(), e);
093        }
094    }
095
096    /**
097     * Gets the resource bundle name.
098     *
099     * @return the bundle name.
100     * @since 1.1
101     */
102    public String getBundle() {
103        return bundle;
104    }
105
106    /**
107     * Gets the key/value.
108     *
109     * @return the key value.
110     */
111    public String getKey() {
112        return key;
113    }
114
115    /**
116     * Gets the name of the dependency.
117     *
118     * @return the name of the dependency.
119     */
120    public String getName() {
121        return name;
122    }
123
124    /**
125     * Gets the replacement position.
126     *
127     * @return This replacement position.
128     */
129    public int getPosition() {
130        return position;
131    }
132
133    /**
134     * Tests whether or not the key is a resource key or literal value.
135     *
136     * @return {@code true} if key is a resource key.
137     */
138    public boolean isResource() {
139        return resource;
140    }
141
142    /**
143     * Sets the resource bundle name.
144     *
145     * @param bundle The new bundle name.
146     * @since 1.1
147     */
148    public void setBundle(final String bundle) {
149        this.bundle = bundle;
150    }
151
152    /**
153     * Sets the key/value.
154     *
155     * @param key They to access the argument.
156     */
157    public void setKey(final String key) {
158        this.key = key;
159    }
160
161    /**
162     * Sets the name of the dependency.
163     *
164     * @param name the name of the dependency.
165     */
166    public void setName(final String name) {
167        this.name = name;
168    }
169
170    /**
171     * Sets this argument's replacement position.
172     *
173     * @param position set this argument's replacement position.
174     */
175    public void setPosition(final int position) {
176        this.position = position;
177    }
178
179    /**
180     * Sets whether or not the key is a resource.
181     *
182     * @param resource If true indicates the key is a resource.
183     */
184    public void setResource(final boolean resource) {
185        this.resource = resource;
186    }
187
188    /**
189     * Returns a string representation of the object.
190     *
191     * @return a string representation of the object.
192     */
193    @Override
194    public String toString() {
195        // @formatter:off
196        return new StringBuilder()
197            .append("Arg: name=")
198            .append(name)
199            .append("  key=")
200            .append(key)
201            .append("  position=")
202            .append(position)
203            .append("  bundle=")
204            .append(bundle)
205            .append("  resource=")
206            .append(resource)
207            .append("\n")
208            .toString();
209        // @formatter:on
210    }
211
212}