001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.api.notifications;
021    
022    import java.io.Serializable;
023    import java.util.HashMap;
024    
025    import org.apache.commons.lang.builder.ReflectionToStringBuilder;
026    import org.apache.commons.lang.builder.ToStringStyle;
027    
028    import com.google.common.collect.Maps;
029    
030    /**
031     * <p>
032     * This class represents a notification that will be delivered to users. This is a general concept and it has no
033     * knowledge of the possible ways to be delivered (see {@link NotificationChannel}) or of the users who should
034     * receive it (see {@link NotificationDispatcher}).
035     * </p> 
036     * 
037     * @since 2.10
038     */
039    public class Notification implements Serializable {
040    
041      private String type;
042    
043      private HashMap<String, String> fields = Maps.newHashMap(); // NOSONAR false-positive due to serialization : usage of HashMap instead of
044                                                                  // Map
045    
046      /**
047       * <p>
048       * Create a new {@link Notification} of the given type.
049       * </p>
050       * Example: type = "new-violations"
051       * 
052       * @param type the type of notification
053       */
054      public Notification(String type) {
055        this.type = type;
056      }
057    
058      /**
059       * Returns the type of the notification
060       * 
061       * @return the type
062       */
063      public String getType() {
064        return type;
065      }
066    
067      /**
068       * Adds a field (kind of property) to the notification
069       * 
070       * @param field the name of the field (= the key)
071       * @param value the value of the field
072       * @return the notification itself
073       */
074      public Notification setFieldValue(String field, String value) {
075        fields.put(field, value);
076        return this;
077      }
078    
079      /**
080       * Returns the value of a field.
081       * 
082       * @param field the field
083       * @return the value of the field
084       */
085      public String getFieldValue(String field) {
086        return fields.get(field);
087      }
088    
089      @Override
090      public boolean equals(Object obj) {
091        if (!(obj instanceof Notification)) {
092          return false;
093        }
094        if (this == obj) {
095          return true;
096        }
097        Notification other = (Notification) obj;
098        return this.type.equals(other.type) && this.fields.equals(other.fields);
099      }
100    
101      @Override
102      public int hashCode() {
103        return type.hashCode() * 31 + fields.hashCode();
104      }
105    
106      @Override
107      public String toString() {
108        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
109      }
110    
111    }