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 net.jcip.annotations.Immutable;
025    import org.jboss.dna.common.i18n.I18n;
026    import org.jboss.dna.common.util.CheckArg;
027    import org.jboss.dna.common.util.HashCode;
028    
029    /**
030     * @author Randall Hauch
031     */
032    @Immutable
033    public class Problem {
034    
035        public static final int DEFAULT_CODE = 0;
036    
037        public enum Status {
038            ERROR,
039            WARNING,
040            INFO;
041        }
042    
043        private final Status status;
044        private final I18n message;
045        private final Object[] parameters;
046        private final Throwable throwable;
047        private final int code;
048        private final String resource;
049        private final String location;
050    
051        public Problem( Status status,
052                        int code,
053                        I18n message,
054                        Object[] params,
055                        String resource,
056                        String location,
057                        Throwable throwable ) {
058            CheckArg.isNotNull(status, "status");
059            CheckArg.isNotNull(message, "message");
060            this.status = status;
061            this.code = code;
062            this.message = message;
063            this.parameters = params;
064            this.resource = resource != null ? resource.trim() : null;
065            this.location = location != null ? location.trim() : null;
066            this.throwable = throwable;
067        }
068    
069        /**
070         * @return code
071         */
072        public int getCode() {
073            return this.code;
074        }
075    
076        /**
077         * @return location
078         */
079        public String getLocation() {
080            return this.location;
081        }
082    
083        /**
084         * Get the message written in the current locale.
085         * 
086         * @return the message
087         */
088        public String getMessageString() {
089            return this.message.text(this.parameters);
090        }
091    
092        /**
093         * @return message
094         */
095        public I18n getMessage() {
096            return this.message;
097        }
098    
099        public Object[] getParameters() {
100            return this.parameters;
101        }
102    
103        /**
104         * @return resource
105         */
106        public String getResource() {
107            return this.resource;
108        }
109    
110        /**
111         * @return status
112         */
113        public Status getStatus() {
114            return this.status;
115        }
116    
117        /**
118         * @return throwable
119         */
120        public Throwable getThrowable() {
121            return this.throwable;
122        }
123    
124        /**
125         * {@inheritDoc}
126         * 
127         * @see java.lang.Object#hashCode()
128         */
129        @Override
130        public int hashCode() {
131            return HashCode.compute(status, code, message, resource, location);
132        }
133    
134        /**
135         * {@inheritDoc}
136         * 
137         * @see java.lang.Object#equals(java.lang.Object)
138         */
139        @Override
140        public boolean equals( Object obj ) {
141            if (obj == this) return true;
142            if (obj instanceof Problem) {
143                Problem that = (Problem)obj;
144                if (this.getStatus() != that.getStatus()) return false;
145                if (this.getCode() != that.getCode()) return false;
146                if (!this.getMessage().equals(that.getMessage())) return false;
147                if (!this.getParameters().equals(that.getParameters())) return false;
148    
149                String thisResource = this.getResource();
150                String thatResource = that.getResource();
151                if (thisResource != thatResource) {
152                    if (thisResource == null || !thisResource.equals(thatResource)) return false;
153                }
154    
155                String thisLocation = this.getLocation();
156                String thatLocation = that.getLocation();
157                if (thisLocation != thatLocation) {
158                    if (thisLocation == null || !thisLocation.equals(thatLocation)) return false;
159                }
160    
161                Throwable thisThrowable = this.getThrowable();
162                Throwable thatThrowable = that.getThrowable();
163                if (thisThrowable != thatThrowable) {
164                    if (thisThrowable == null || !thisThrowable.equals(thatThrowable)) return false;
165                }
166                return true;
167            }
168            return false;
169        }
170    
171        /**
172         * {@inheritDoc}
173         * 
174         * @see java.lang.Object#toString()
175         */
176        @Override
177        public String toString() {
178            StringBuilder sb = new StringBuilder();
179            sb.append(this.getStatus()).append(": ");
180            if (this.getCode() != DEFAULT_CODE) {
181                sb.append("(").append(this.getCode()).append(") ");
182            }
183            sb.append(this.getMessageString());
184            if (this.getResource() != null) {
185                sb.append(" Resource=\"").append(this.getResource()).append("\"");
186            }
187            if (this.getLocation() != null) {
188                sb.append(" At \"").append(this.getLocation()).append("\"");
189            }
190            if (this.getThrowable() != null) {
191                sb.append(" (threw ").append(this.getThrowable().getLocalizedMessage()).append(")");
192            }
193            return sb.toString();
194        }
195    
196    }