package 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. /** * This class defines the standard message object for the * {@linkplain HarvestCache}. */ import java.util.Date; import java.util.List; import votorola.a.diff.harvest.cache.HarvestCache; import votorola.g.lang.ThreadSafe; /** * Message structure for any communication medium. Load your data in a message * object and pass it to {@linkplain HarvestCache}. */ public @ThreadSafe final class Message { public final MessageContext mc; public MessageContext mc() { return mc; } /** * A short message summary computed according to * {@link HarvestCache#CHARLIMIT} by {@link HarvestUtil#summarize(String)}. * * @return summary */ public String content() { return content; } private final String content; /** * The path from {@link MessageContext#archiveUrl()} to the message. * Together they form the absolute path to the message. * * @return path */ public String path() { return path; } private final String path; /** * Create a message object for some message you found including the url to * the data on the web-archive. All fields may not be null. Content and path * may not be empty. * * @param content * calculate this with {@linkplain HarvestUtil#summarize(String)} */ private Message(final MessageContext mc, final String content, final String path) { if (mc == null) { throw new IllegalArgumentException("messageContext is null."); } if (content == null) { throw new IllegalArgumentException(mc.archiveUrl() + ": content is null."); } if (path == null) { throw new IllegalArgumentException(mc.archiveUrl() + ": path is null."); } if (content.isEmpty()) { throw new IllegalArgumentException(mc.archiveUrl() + ": content is empty."); } if (path.isEmpty()) { throw new IllegalArgumentException(mc.archiveUrl() + ": path is empty."); } this.mc = mc; this.content = content; this.path = path; } /** * Create a new message including {@linkplain MessageContext}. All fields * are expected to be not null. Content and path may not be empty. * * @param content * calculate this with {@linkplain HarvestUtil#summarize(String)} * @param sender * identity in the medium * @param archiveUrl * @param path * @param diffUrls * calculate this with * {@linkplain HarvestCache#findDiffUrls(String)} * @param sentDate */ public static Message create(final String content, final String sender, final String archiveUrl, final String path, final List diffUrls, final Date sentDate) { return new Message(MessageContext.create(sender, archiveUrl, diffUrls, sentDate), content, path); } @Override public boolean equals(Object o) { if (o == this) { return true; } return (o instanceof Message) && mc.equals(o) && ((Message) o).content.equals(content) && ((Message) o).path.equals(path); } @Override public int hashCode() { int hash = mc.hashCode(); hash = hash * 37 + content.hashCode(); hash = hash * 37 + path.hashCode(); return hash; } /** * Return context with content and url appended, separated by |. This is not * guaranteed to be stable. */ @Override public String toString() { return mc.toString() + "|" + content + "|" + path; } }