001    /*
002     * JBoss, Home of Professional Open Source.
003     * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004     * as indicated by the @author tags. See the copyright.txt file in the
005     * distribution for a full listing of individual contributors. 
006     *
007     * This is free software; you can redistribute it and/or modify it
008     * under the terms of the GNU Lesser General Public License as
009     * published by the Free Software Foundation; either version 2.1 of
010     * the License, or (at your option) any later version.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this software; if not, write to the Free
019     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021     */
022    package org.jboss.dna.common.collection;
023    
024    import java.util.Collections;
025    import java.util.Iterator;
026    import java.util.List;
027    import org.jboss.dna.common.i18n.I18n;
028    
029    /**
030     * A list of problems for some execution context. The problems will be {@link #iterator() returned} in the order in which they
031     * were encountered (although this cannot be guaranteed in contexts involving multiple threads or processes).
032     * 
033     * @author Randall Hauch
034     * @author John Verhaeg
035     */
036    public abstract class AbstractProblems implements Problems {
037    
038        protected static final List<Problem> EMPTY_PROBLEMS = Collections.emptyList();
039    
040        public void addError( I18n message,
041                              Object... params ) {
042            addProblem(new Problem(Problem.Status.ERROR, Problem.DEFAULT_CODE, message, params, null, null, null));
043        }
044    
045        public void addError( Throwable throwable,
046                              I18n message,
047                              Object... params ) {
048            addProblem(new Problem(Problem.Status.ERROR, Problem.DEFAULT_CODE, message, params, null, null, throwable));
049        }
050    
051        public void addError( I18n message,
052                              String resource,
053                              String location,
054                              Object... params ) {
055            addProblem(new Problem(Problem.Status.ERROR, Problem.DEFAULT_CODE, message, params, resource, location, null));
056        }
057    
058        public void addError( Throwable throwable,
059                              I18n message,
060                              String resource,
061                              String location,
062                              Object... params ) {
063            addProblem(new Problem(Problem.Status.ERROR, Problem.DEFAULT_CODE, message, params, resource, location, throwable));
064        }
065    
066        public void addError( int code,
067                              I18n message,
068                              Object... params ) {
069            addProblem(new Problem(Problem.Status.ERROR, code, message, params, null, null, null));
070        }
071    
072        public void addError( Throwable throwable,
073                              int code,
074                              I18n message,
075                              Object... params ) {
076            addProblem(new Problem(Problem.Status.ERROR, code, message, params, null, null, throwable));
077        }
078    
079        public void addError( int code,
080                              I18n message,
081                              String resource,
082                              String location,
083                              Object... params ) {
084            addProblem(new Problem(Problem.Status.ERROR, code, message, params, resource, location, null));
085        }
086    
087        public void addError( Throwable throwable,
088                              int code,
089                              I18n message,
090                              String resource,
091                              String location,
092                              Object... params ) {
093            addProblem(new Problem(Problem.Status.ERROR, code, message, params, resource, location, throwable));
094        }
095    
096        public void addWarning( I18n message,
097                                Object... params ) {
098            addProblem(new Problem(Problem.Status.WARNING, Problem.DEFAULT_CODE, message, params, null, null, null));
099        }
100    
101        public void addWarning( Throwable throwable,
102                                I18n message,
103                                Object... params ) {
104            addProblem(new Problem(Problem.Status.WARNING, Problem.DEFAULT_CODE, message, params, null, null, throwable));
105        }
106    
107        public void addWarning( I18n message,
108                                String resource,
109                                String location,
110                                Object... params ) {
111            addProblem(new Problem(Problem.Status.WARNING, Problem.DEFAULT_CODE, message, params, resource, location, null));
112        }
113    
114        public void addWarning( Throwable throwable,
115                                I18n message,
116                                String resource,
117                                String location,
118                                Object... params ) {
119            addProblem(new Problem(Problem.Status.WARNING, Problem.DEFAULT_CODE, message, params, resource, location, throwable));
120        }
121    
122        public void addWarning( int code,
123                                I18n message,
124                                Object... params ) {
125            addProblem(new Problem(Problem.Status.WARNING, code, message, params, null, null, null));
126        }
127    
128        public void addWarning( Throwable throwable,
129                                int code,
130                                I18n message,
131                                Object... params ) {
132            addProblem(new Problem(Problem.Status.WARNING, code, message, params, null, null, throwable));
133        }
134    
135        public void addWarning( int code,
136                                I18n message,
137                                String resource,
138                                String location,
139                                Object... params ) {
140            addProblem(new Problem(Problem.Status.WARNING, code, message, params, resource, location, null));
141        }
142    
143        public void addWarning( Throwable throwable,
144                                int code,
145                                I18n message,
146                                String resource,
147                                String location,
148                                Object... params ) {
149            addProblem(new Problem(Problem.Status.WARNING, code, message, params, resource, location, throwable));
150        }
151    
152        public void addInfo( I18n message,
153                             Object... params ) {
154            addProblem(new Problem(Problem.Status.INFO, Problem.DEFAULT_CODE, message, params, null, null, null));
155        }
156    
157        public void addInfo( Throwable throwable,
158                             I18n message,
159                             Object... params ) {
160            addProblem(new Problem(Problem.Status.INFO, Problem.DEFAULT_CODE, message, params, null, null, throwable));
161        }
162    
163        public void addInfo( I18n message,
164                             String resource,
165                             String location,
166                             Object... params ) {
167            addProblem(new Problem(Problem.Status.INFO, Problem.DEFAULT_CODE, message, params, resource, location, null));
168        }
169    
170        public void addInfo( Throwable throwable,
171                             I18n message,
172                             String resource,
173                             String location,
174                             Object... params ) {
175            addProblem(new Problem(Problem.Status.INFO, Problem.DEFAULT_CODE, message, params, resource, location, throwable));
176        }
177    
178        public void addInfo( int code,
179                             I18n message,
180                             Object... params ) {
181            addProblem(new Problem(Problem.Status.INFO, code, message, params, null, null, null));
182        }
183    
184        public void addInfo( Throwable throwable,
185                             int code,
186                             I18n message,
187                             Object... params ) {
188            addProblem(new Problem(Problem.Status.INFO, code, message, params, null, null, throwable));
189        }
190    
191        public void addInfo( int code,
192                             I18n message,
193                             String resource,
194                             String location,
195                             Object... params ) {
196            addProblem(new Problem(Problem.Status.INFO, code, message, params, resource, location, null));
197        }
198    
199        public void addInfo( Throwable throwable,
200                             int code,
201                             I18n message,
202                             String resource,
203                             String location,
204                             Object... params ) {
205            addProblem(new Problem(Problem.Status.INFO, code, message, params, resource, location, throwable));
206        }
207    
208        public boolean hasProblems() {
209            return getProblems().size() > 0;
210        }
211    
212        public boolean hasErrors() {
213            for (Problem problem : this.getProblems()) {
214                if (problem.getStatus() == Problem.Status.ERROR) return true;
215            }
216            return false;
217        }
218    
219        public boolean hasWarnings() {
220            for (Problem problem : this.getProblems()) {
221                if (problem.getStatus() == Problem.Status.WARNING) return true;
222            }
223            return false;
224        }
225    
226        public boolean hasInfo() {
227            for (Problem problem : this.getProblems()) {
228                if (problem.getStatus() == Problem.Status.INFO) return true;
229            }
230            return false;
231        }
232    
233        public boolean isEmpty() {
234            return getProblems().isEmpty();
235        }
236    
237        public int size() {
238            return getProblems().size();
239        }
240    
241        /**
242         * {@inheritDoc}
243         * 
244         * @see org.jboss.dna.common.collection.Problems#iterator()
245         */
246        public Iterator<Problem> iterator() {
247            return getProblems().iterator();
248        }
249    
250        protected abstract void addProblem( Problem problem );
251    
252        protected abstract List<Problem> getProblems();
253    }