Arg.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.validator;

  18. import java.io.Serializable;

  19. /**
  20.  * <p>
  21.  * A default argument or an argument for a
  22.  * specific validator definition (ex: required)
  23.  * can be stored to pass into a message as parameters.  This can be used in a
  24.  * pluggable validator for constructing locale
  25.  * sensitive messages by using <code>java.text.MessageFormat</code>
  26.  * or an equivalent class.  The resource field can be
  27.  * used to determine if the value stored in the argument
  28.  * is a value to be retrieved from a locale sensitive
  29.  * message retrieval system like <code>java.util.PropertyResourceBundle</code>.
  30.  * The resource field defaults to 'true'.
  31.  * </p>
  32.  * <p>Instances of this class are configured with an &lt;arg&gt; xml element.</p>
  33.  */
  34. //TODO mutable non-private fields
  35. public class Arg implements Cloneable, Serializable {

  36.     private static final long serialVersionUID = -8922606779669839294L;

  37.     /**
  38.      * The resource bundle name that this Arg's <code>key</code> should be
  39.      * resolved in (optional).
  40.      * @since 1.1
  41.      */
  42.     protected String bundle;

  43.     /**
  44.      * The key or value of the argument.
  45.      */
  46.     protected String key;

  47.     /**
  48.      * The name dependency that this argument goes with (optional).
  49.      */
  50.     protected String name;

  51.     /**
  52.      * This argument's position in the message. Set postion=0 to
  53.      * make a replacement in this string: "some msg {0}".
  54.      * @since 1.1
  55.      */
  56.     protected int position = -1;

  57.     /**
  58.      * Whether or not the key is a message resource (optional).  Defaults to
  59.      * true.  If it is 'true', the value will try to be resolved as a message
  60.      * resource.
  61.      */
  62.     protected boolean resource = true;

  63.     /**
  64.      * Creates and returns a copy of this object.
  65.      * @return A copy of this object.
  66.      */
  67.     @Override
  68.     public Object clone() {
  69.         try {
  70.             return super.clone();
  71.         } catch (final CloneNotSupportedException e) {
  72.             throw new UnsupportedOperationException(e.toString(), e);
  73.         }
  74.     }

  75.     /**
  76.      * Returns the resource bundle name.
  77.      * @return the bundle name.
  78.      * @since 1.1
  79.      */
  80.     public String getBundle() {
  81.         return this.bundle;
  82.     }

  83.     /**
  84.      * Gets the key/value.
  85.      * @return the key value.
  86.      */
  87.     public String getKey() {
  88.         return this.key;
  89.     }

  90.     /**
  91.      * Gets the name of the dependency.
  92.      * @return the name of the dependency.
  93.      */
  94.     public String getName() {
  95.         return this.name;
  96.     }

  97.     /**
  98.      * Argument's replacement position.
  99.      * @return This argument's replacement position.
  100.      */
  101.     public int getPosition() {
  102.         return this.position;
  103.     }

  104.     /**
  105.      * Tests whether or not the key is a resource key or literal value.
  106.      * @return {@code true} if key is a resource key.
  107.      */
  108.     public boolean isResource() {
  109.         return this.resource;
  110.     }

  111.     /**
  112.      * Sets the resource bundle name.
  113.      * @param bundle The new bundle name.
  114.      * @since 1.1
  115.      */
  116.     public void setBundle(final String bundle) {
  117.         this.bundle = bundle;
  118.     }

  119.     /**
  120.      * Sets the key/value.
  121.      * @param key They to access the argument.
  122.      */
  123.     public void setKey(final String key) {
  124.         this.key = key;
  125.     }

  126.     /**
  127.      * Sets the name of the dependency.
  128.      * @param name the name of the dependency.
  129.      */
  130.     public void setName(final String name) {
  131.         this.name = name;
  132.     }

  133.     /**
  134.      * Sets this argument's replacement position.
  135.      * @param position set this argument's replacement position.
  136.      */
  137.     public void setPosition(final int position) {
  138.         this.position = position;
  139.     }

  140.     /**
  141.      * Sets whether or not the key is a resource.
  142.      * @param resource If true indicates the key is a resource.
  143.      */
  144.     public void setResource(final boolean resource) {
  145.         this.resource = resource;
  146.     }

  147.     /**
  148.      * Returns a string representation of the object.
  149.      * @return a string representation of the object.
  150.      */
  151.     @Override
  152.     public String toString() {
  153.         // @formatter:off
  154.         return new StringBuilder()
  155.             .append("Arg: name=")
  156.             .append(name)
  157.             .append("  key=")
  158.             .append(key)
  159.             .append("  position=")
  160.             .append(position)
  161.             .append("  bundle=")
  162.             .append(bundle)
  163.             .append("  resource=")
  164.             .append(resource)
  165.             .append("\n")
  166.             .toString();
  167.         // @formatter:on
  168.     }

  169. }