001 /*
002 * JBoss DNA (http://www.jboss.org/dna)
003 * See the COPYRIGHT.txt file distributed with this work for information
004 * regarding copyright ownership. Some portions may be licensed
005 * to Red Hat, Inc. under one or more contributor license agreements.
006 * See the AUTHORS.txt file in the distribution for a full listing of
007 * individual contributors.
008 *
009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010 * is licensed to you under the terms of the GNU Lesser General Public License as
011 * published by the Free Software Foundation; either version 2.1 of
012 * the License, or (at your option) any later version.
013 *
014 * JBoss DNA is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017 * Lesser General Public License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this software; if not, write to the Free
021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023 */
024 package org.jboss.dna.common.collection;
025
026 import net.jcip.annotations.Immutable;
027 import org.jboss.dna.common.i18n.I18n;
028 import org.jboss.dna.common.util.CheckArg;
029 import org.jboss.dna.common.util.HashCode;
030
031 /**
032 * @author Randall Hauch
033 */
034 @Immutable
035 public class Problem {
036
037 public static final int DEFAULT_CODE = 0;
038
039 public enum Status {
040 ERROR,
041 WARNING,
042 INFO;
043 }
044
045 private final Status status;
046 private final I18n message;
047 private final Object[] parameters;
048 private final Throwable throwable;
049 private final int code;
050 private final String resource;
051 private final String location;
052
053 public Problem( Status status,
054 int code,
055 I18n message,
056 Object[] params,
057 String resource,
058 String location,
059 Throwable throwable ) {
060 CheckArg.isNotNull(status, "status");
061 CheckArg.isNotNull(message, "message");
062 this.status = status;
063 this.code = code;
064 this.message = message;
065 this.parameters = params;
066 this.resource = resource != null ? resource.trim() : null;
067 this.location = location != null ? location.trim() : null;
068 this.throwable = throwable;
069 }
070
071 /**
072 * @return code
073 */
074 public int getCode() {
075 return this.code;
076 }
077
078 /**
079 * @return location
080 */
081 public String getLocation() {
082 return this.location;
083 }
084
085 /**
086 * Get the message written in the current locale.
087 *
088 * @return the message
089 */
090 public String getMessageString() {
091 return this.message.text(this.parameters);
092 }
093
094 /**
095 * @return message
096 */
097 public I18n getMessage() {
098 return this.message;
099 }
100
101 public Object[] getParameters() {
102 return this.parameters;
103 }
104
105 /**
106 * @return resource
107 */
108 public String getResource() {
109 return this.resource;
110 }
111
112 /**
113 * @return status
114 */
115 public Status getStatus() {
116 return this.status;
117 }
118
119 /**
120 * @return throwable
121 */
122 public Throwable getThrowable() {
123 return this.throwable;
124 }
125
126 /**
127 * {@inheritDoc}
128 *
129 * @see java.lang.Object#hashCode()
130 */
131 @Override
132 public int hashCode() {
133 return HashCode.compute(status, code, message, resource, location);
134 }
135
136 /**
137 * {@inheritDoc}
138 *
139 * @see java.lang.Object#equals(java.lang.Object)
140 */
141 @Override
142 public boolean equals( Object obj ) {
143 if (obj == this) return true;
144 if (obj instanceof Problem) {
145 Problem that = (Problem)obj;
146 if (this.getStatus() != that.getStatus()) return false;
147 if (this.getCode() != that.getCode()) return false;
148 if (!this.getMessage().equals(that.getMessage())) return false;
149 if (!this.getParameters().equals(that.getParameters())) return false;
150
151 String thisResource = this.getResource();
152 String thatResource = that.getResource();
153 if (thisResource != thatResource) {
154 if (thisResource == null || !thisResource.equals(thatResource)) return false;
155 }
156
157 String thisLocation = this.getLocation();
158 String thatLocation = that.getLocation();
159 if (thisLocation != thatLocation) {
160 if (thisLocation == null || !thisLocation.equals(thatLocation)) return false;
161 }
162
163 Throwable thisThrowable = this.getThrowable();
164 Throwable thatThrowable = that.getThrowable();
165 if (thisThrowable != thatThrowable) {
166 if (thisThrowable == null || !thisThrowable.equals(thatThrowable)) return false;
167 }
168 return true;
169 }
170 return false;
171 }
172
173 /**
174 * {@inheritDoc}
175 *
176 * @see java.lang.Object#toString()
177 */
178 @Override
179 public String toString() {
180 StringBuilder sb = new StringBuilder();
181 sb.append(this.getStatus()).append(": ");
182 if (this.getCode() != DEFAULT_CODE) {
183 sb.append("(").append(this.getCode()).append(") ");
184 }
185 sb.append(this.getMessageString());
186 if (this.getResource() != null) {
187 sb.append(" Resource=\"").append(this.getResource()).append("\"");
188 }
189 if (this.getLocation() != null) {
190 sb.append(" At \"").append(this.getLocation()).append("\"");
191 }
192 if (this.getThrowable() != null) {
193 sb.append(" (threw ").append(this.getThrowable().getLocalizedMessage()).append(")");
194 }
195 return sb.toString();
196 }
197
198 }