Java tutorial
/* * 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.text.StrBuilder; import java.util.Collections; import java.util.LinkedList; import java.util.List; /** * The results of a {@link Parser} parse operation. */ public class ParseResult<T> { /** * The class instance that was passed into the parse operation. */ private T bean; /** * The modifiable list of possible errors resulting from the parse operation. */ private List<ParsedOption<T>> errors; /** * An immutable list backed by {@link #errors} that is exposed outside the class. */ private List<ParsedOption<T>> immutableErrors; /** * The creation of the parse result guarantees the bean and errors list members are not null. * * @param bean The class instance passed into the parse operation. * @throws java.lang.IllegalArgumentException if the bean instance is {@code null}. */ public ParseResult(final T bean) { if (null == bean) { throw new IllegalArgumentException("The bean parameter cannot be null"); } this.bean = bean; errors = new LinkedList<ParsedOption<T>>(); immutableErrors = Collections.unmodifiableList(errors); } /** * Get the class instance that was passed into the parse operation. * * @return the class instance that was passed into the parser operation. */ public T getBean() { return bean; } /** * A list of errors encountered while parsing the parameters. The list is read-only. * * @return a list of parser errors or an empty list if there are no errors. */ public List<ParsedOption<T>> getErrors() { return immutableErrors; } /** * The number of errors encountered parsing the parameters. * * @return the number of errors encountered parsing the parameters. */ public int getErrorCount() { return errors.size(); } /** * Indicates if there were errors encountered while parsing the parameters. * * @return {@code true} if there were errors parsing parameters, {@code false} otherwise. */ public boolean isParseError() { return getErrorCount() > 0; } /** * Adds an error to the parse results. * * @param error The error that will be added to the parse results. */ protected void addError(final ParsedOption<T> error) { errors.add(error); } @Override public String toString() { final StrBuilder builder = new StrBuilder(); if (!isParseError()) { builder.append("Ok"); } else { for (final ParsedOption<T> parsedOption : errors) { if (builder.length() > 0) { builder.appendNewLine(); } final ParserOption<T> parserOption = parsedOption.getParserOption(); if (null != parserOption) { builder.append(parserOption).append(": "); } builder.append(parsedOption.getError()); } } return builder.toString(); } }