candr.yoclip.ParsedOption.java Source code

Java tutorial

Introduction

Here is the source code for candr.yoclip.ParsedOption.java

Source

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

package candr.yoclip;

import org.apache.commons.lang3.StringUtils;

/**
 * A bean that holds parsed parser parameters or parsed parser parameters that have an error.
 */
public class ParsedOption<T> {

    /**
     * The parser parameter that was parsed.
     */
    private ParserOption<T> parserOption;

    /**
     * The value associated with the parsed parameter.
     */
    private String value;

    /**
     * An error, if any, associated with the parser parameter.
     */
    private String error;

    /**
     * Creates an immutable error for a parsed parameter that does not have an {@link ParserOption} associated with it.
     *
     * @param error A description of the error.
     */
    public ParsedOption(final String error) {
        if (StringUtils.isEmpty(error)) {
            throw new IllegalArgumentException("The parsed parameter error cannot be empty.");
        }
        this.error = error;
    }

    /**
     * Creates the bean for parser parameters the have been parsed or have an error.
     *
     * @param parserOption The parser parameter.
     * @param value        The value associated with the parsed parameter.
     * @throws IllegalArgumentException if the parserOption is null.
     */
    public ParsedOption(final ParserOption<T> parserOption, final String value) {
        if (null == parserOption) {
            throw new IllegalArgumentException("The ParserOption cannot be null.");
        }
        this.parserOption = parserOption;
        this.value = value;
    }

    /**
     * Get the parser parameter.
     *
     * @return the parser parameter.
     */
    public ParserOption<T> getParserOption() {
        return parserOption;
    }

    /**
     * The parsed value associated with the parser parameter.
     *
     * @return the parsed value or {@code null} is there is none.
     */
    public String getValue() {
        return value;
    }

    /**
     * A description of any errors associated with the parser parameter.
     *
     * @return any errors associated with the parser parameter or {@code null} if there were none.
     */
    public String getError() {
        return error;
    }

    /**
     * Sets a description of any errors associated with the parser parameter.
     *
     * @param error A description of the error.
     * @throws java.lang.UnsupportedOperationException if the instance is created via {@link #ParsedOption(String)}.
     */
    public void setError(final String error) {
        if (null == getParserOption()) {
            throw new UnsupportedOperationException("A parsed parameter error is immutable.");
        }
        this.error = error;
    }

    /**
     * Indicates if the parsed parameter has an error.
     *
     * @return {@code true} if an error exists, {@code false} otherwise.
     */
    public boolean isError() {
        return !StringUtils.isEmpty(error);
    }
}