001    /*
002     * Copyright (c) 2011, Cloudera, Inc. All Rights Reserved.
003     *
004     * Cloudera, Inc. licenses this file to you under the Apache License,
005     * Version 2.0 (the "License"). You may not use this file except in
006     * compliance with the License. You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
011     * CONDITIONS OF ANY KIND, either express or implied. See the License for
012     * the specific language governing permissions and limitations under the
013     * License.
014     */
015    package com.cloudera.lib.wsrs;
016    
017    import com.cloudera.lib.util.Check;
018    
019    import java.text.MessageFormat;
020    
021    public abstract class Param<T> {
022      protected T value;
023    
024      public T parseParam(String name, String str) {
025        Check.notNull(name, "name");
026        try {
027          return (str != null && str.trim().length() > 0) ? parse(str) : null;
028        }
029        catch (Exception ex) {
030          throw new IllegalArgumentException(
031            MessageFormat.format("Parameter [{0}], invalid value [{1}], value must be [{2}]",
032                                 name, str, getDomain()));
033        }
034      }
035    
036      public T value() {
037        return value;
038      }
039    
040      protected void setValue(T value) {
041        this.value = value;
042      }
043    
044      protected abstract String getDomain();
045    
046      protected abstract T parse(String str) throws Exception;
047    
048      public String toString() {
049        return value.toString();
050      }
051    }