001    package org.nakedobjects.applib.util;
002    
003    /**
004     * Helper class to create properly concatenated reason string for use in method that return {@link String}s
005     * with reasons.
006     * 
007     * <p>
008     * If no reasons are specified {@link #getReason()} will return <code>null</code> , otherwise it will return
009     * a {@link String} with all the valid reasons concatenated with a semi-colon separating each one.
010     */
011    public class ReasonBuffer {
012        StringBuffer reasonBuffer = new StringBuffer();
013    
014        /**
015         * Append a reason to the list of existing reasons.
016         */
017        public void append(final String reason) {
018            if (reason != null) {
019                if (reasonBuffer.length() > 0) {
020                    reasonBuffer.append("; ");
021                }
022                reasonBuffer.append(reason);
023            }
024        }
025    
026        /**
027         * Append a reason to the list of existing reasons if the condition flag is true.
028         */
029        public void appendOnCondition(final boolean condition, final String reason) {
030            if (condition) {
031                append(reason);
032            }
033        }
034    
035        /**
036         * Return the combined set of reasons, or <code>null</code> if there are none.
037         */
038        public String getReason() {
039            return reasonBuffer.length() == 0 ? null : reasonBuffer.toString();
040        }
041    
042    }
043    
044    // Copyright (c) Naked Objects Group Ltd.