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.  *      https://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. import java.text.MessageFormat;

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

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

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

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

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

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

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

  64.     /**
  65.      * Constructs a new instance.
  66.      */
  67.     public Arg() {
  68.         // empty
  69.     }

  70.     /**
  71.      * Creates and returns a copy of this object.
  72.      * @return A copy of this object.
  73.      */
  74.     @Override
  75.     public Object clone() {
  76.         try {
  77.             return super.clone();
  78.         } catch (final CloneNotSupportedException e) {
  79.             throw new UnsupportedOperationException(e.toString(), e);
  80.         }
  81.     }

  82.     /**
  83.      * Gets the resource bundle name.
  84.      *
  85.      * @return the bundle name.
  86.      * @since 1.1
  87.      */
  88.     public String getBundle() {
  89.         return bundle;
  90.     }

  91.     /**
  92.      * Gets the key/value.
  93.      *
  94.      * @return the key value.
  95.      */
  96.     public String getKey() {
  97.         return key;
  98.     }

  99.     /**
  100.      * Gets the name of the dependency.
  101.      *
  102.      * @return the name of the dependency.
  103.      */
  104.     public String getName() {
  105.         return name;
  106.     }

  107.     /**
  108.      * Gets the replacement position.
  109.      *
  110.      * @return This replacement position.
  111.      */
  112.     public int getPosition() {
  113.         return position;
  114.     }

  115.     /**
  116.      * Tests whether or not the key is a resource key or literal value.
  117.      *
  118.      * @return {@code true} if key is a resource key.
  119.      */
  120.     public boolean isResource() {
  121.         return resource;
  122.     }

  123.     /**
  124.      * Sets the resource bundle name.
  125.      *
  126.      * @param bundle The new bundle name.
  127.      * @since 1.1
  128.      */
  129.     public void setBundle(final String bundle) {
  130.         this.bundle = bundle;
  131.     }

  132.     /**
  133.      * Sets the key/value.
  134.      *
  135.      * @param key They to access the argument.
  136.      */
  137.     public void setKey(final String key) {
  138.         this.key = key;
  139.     }

  140.     /**
  141.      * Sets the name of the dependency.
  142.      *
  143.      * @param name the name of the dependency.
  144.      */
  145.     public void setName(final String name) {
  146.         this.name = name;
  147.     }

  148.     /**
  149.      * Sets this argument's replacement position.
  150.      *
  151.      * @param position set this argument's replacement position.
  152.      */
  153.     public void setPosition(final int position) {
  154.         this.position = position;
  155.     }

  156.     /**
  157.      * Sets whether or not the key is a resource.
  158.      *
  159.      * @param resource If true indicates the key is a resource.
  160.      */
  161.     public void setResource(final boolean resource) {
  162.         this.resource = resource;
  163.     }

  164.     /**
  165.      * Returns a string representation of the object.
  166.      *
  167.      * @return a string representation of the object.
  168.      */
  169.     @Override
  170.     public String toString() {
  171.         // @formatter:off
  172.         return new StringBuilder()
  173.             .append("Arg: name=")
  174.             .append(name)
  175.             .append("  key=")
  176.             .append(key)
  177.             .append("  position=")
  178.             .append(position)
  179.             .append("  bundle=")
  180.             .append(bundle)
  181.             .append("  resource=")
  182.             .append(resource)
  183.             .append("\n")
  184.             .toString();
  185.         // @formatter:on
  186.     }

  187. }