001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube 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 * SonarQube 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 License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonar.api.notifications; 021 022import com.google.common.collect.Maps; 023 024import javax.annotation.CheckForNull; 025import javax.annotation.Nullable; 026 027import java.io.Serializable; 028import java.util.Map; 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 * <p> 037 * When creating a new notification, it is strongly advised to give a default message that can be used by channels 038 * that don't want to specifically format messages for different notification types. You can use 039 * {@link Notification#setDefaultMessage(String)} for that purpose. 040 * </p> 041 * 042 * @since 2.10 043 */ 044public class Notification implements Serializable { 045 046 private static final String DEFAULT_MESSAGE_KEY = "default_message"; 047 048 private final String type; 049 private final Map<String, String> fields = Maps.newHashMap(); 050 051 /** 052 * <p> 053 * Create a new {@link Notification} of the given type. 054 * </p> 055 * Example: type = "new-violations" 056 * 057 * @param type the type of notification 058 */ 059 public Notification(String type) { 060 this.type = type; 061 } 062 063 /** 064 * Returns the type of the notification 065 * 066 * @return the type 067 */ 068 public String getType() { 069 return type; 070 } 071 072 /** 073 * <p> 074 * When creating a new notification, it is strongly advised to give a default message that can be 075 * used by channels that don't want to specifically format messages for different notification types. 076 * </p> 077 * <p> 078 * This method is equivalent to setting a value for the field {@link #DEFAULT_MESSAGE_KEY} with 079 * {@link #setFieldValue(String, String)}. 080 * </p> 081 * 082 * @since 3.5 083 */ 084 public Notification setDefaultMessage(String value) { 085 setFieldValue(DEFAULT_MESSAGE_KEY, value); 086 return this; 087 } 088 089 /** 090 * Returns the default message to display for this notification. 091 */ 092 public String getDefaultMessage() { 093 String defaultMessage = getFieldValue(DEFAULT_MESSAGE_KEY); 094 if (defaultMessage == null) { 095 defaultMessage = this.toString(); 096 } 097 return defaultMessage; 098 } 099 100 /** 101 * Adds a field (kind of property) to the notification 102 * 103 * @param field the name of the field (= the key) 104 * @param value the value of the field 105 * @return the notification itself 106 */ 107 public Notification setFieldValue(String field, @Nullable String value) { 108 fields.put(field, value); 109 return this; 110 } 111 112 /** 113 * Returns the value of a field. 114 * 115 * @param field the field 116 * @return the value of the field 117 */ 118 @CheckForNull 119 public String getFieldValue(String field) { 120 return fields.get(field); 121 } 122 123 @Override 124 public boolean equals(Object obj) { 125 if (!(obj instanceof Notification)) { 126 return false; 127 } 128 if (this == obj) { 129 return true; 130 } 131 Notification other = (Notification) obj; 132 return this.type.equals(other.type) && this.fields.equals(other.fields); 133 } 134 135 @Override 136 public int hashCode() { 137 return type.hashCode() * 31 + fields.hashCode(); 138 } 139 140 @Override 141 public String toString() { 142 StringBuilder sb = new StringBuilder("Notification{"); 143 sb.append("type='").append(type).append('\''); 144 sb.append(", fields=").append(fields); 145 sb.append('}'); 146 return sb.toString(); 147 } 148}