001package votorola.a.diff.harvest;// Copyright 2011-2012. Christian Weilbach. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. 002 003/** 004 * This class defines the standard message object for the 005 * {@linkplain HarvestCache}. 006 */ 007 008import java.util.Date; 009import java.util.List; 010 011import votorola.a.diff.harvest.cache.HarvestCache; 012import votorola.g.lang.ThreadSafe; 013 014/** 015 * Message structure for any communication medium. Load your data in a message 016 * object and pass it to {@linkplain HarvestCache}. 017 */ 018public @ThreadSafe 019final class Message { 020 021 public final MessageContext mc; 022 023 public MessageContext mc() { 024 return mc; 025 } 026 027 /** 028 * A short message summary computed according to 029 * {@link HarvestCache#CHARLIMIT} by {@link HarvestUtil#summarize(String)}. 030 * 031 * @return summary 032 */ 033 public String content() { 034 return content; 035 } 036 037 private final String content; 038 039 /** 040 * The path from {@link MessageContext#archiveUrl()} to the message. 041 * Together they form the absolute path to the message. 042 * 043 * @return path 044 */ 045 public String path() { 046 return path; 047 } 048 049 private final String path; 050 051 /** 052 * Create a message object for some message you found including the url to 053 * the data on the web-archive. All fields may not be null. Content and path 054 * may not be empty. 055 * 056 * @param content 057 * calculate this with {@linkplain HarvestUtil#summarize(String)} 058 */ 059 private Message(final MessageContext mc, final String content, 060 final String path) { 061 if (mc == null) { 062 throw new IllegalArgumentException("messageContext is null."); 063 } 064 if (content == null) { 065 throw new IllegalArgumentException(mc.archiveUrl() 066 + ": content is null."); 067 } 068 if (path == null) { 069 throw new IllegalArgumentException(mc.archiveUrl() 070 + ": path is null."); 071 } 072 073 if (content.isEmpty()) { 074 throw new IllegalArgumentException(mc.archiveUrl() 075 + ": content is empty."); 076 } 077 if (path.isEmpty()) { 078 throw new IllegalArgumentException(mc.archiveUrl() 079 + ": path is empty."); 080 } 081 082 this.mc = mc; 083 this.content = content; 084 this.path = path; 085 } 086 087 /** 088 * Create a new message including {@linkplain MessageContext}. All fields 089 * are expected to be not null. Content and path may not be empty. 090 * 091 * @param content 092 * calculate this with {@linkplain HarvestUtil#summarize(String)} 093 * @param sender 094 * identity in the medium 095 * @param archiveUrl 096 * @param path 097 * @param diffUrls 098 * calculate this with 099 * {@linkplain HarvestCache#findDiffUrls(String)} 100 * @param sentDate 101 */ 102 public static Message create(final String content, final String sender, 103 final String archiveUrl, final String path, 104 final List<String> diffUrls, final Date sentDate) { 105 return new Message(MessageContext.create(sender, archiveUrl, diffUrls, 106 sentDate), content, path); 107 } 108 109 @Override 110 public boolean equals(Object o) { 111 if (o == this) { 112 return true; 113 } 114 return (o instanceof Message) && mc.equals(o) 115 && ((Message) o).content.equals(content) 116 && ((Message) o).path.equals(path); 117 } 118 119 @Override 120 public int hashCode() { 121 int hash = mc.hashCode(); 122 hash = hash * 37 + content.hashCode(); 123 hash = hash * 37 + path.hashCode(); 124 return hash; 125 } 126 127 /** 128 * Return context with content and url appended, separated by |. This is not 129 * guaranteed to be stable. 130 */ 131 @Override 132 public String toString() { 133 return mc.toString() + "|" + content + "|" + path; 134 } 135 136}