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 interface is for possible message objects. */ import java.util.Collections; import java.util.Date; import java.util.List; import votorola.a.diff.harvest.cache.HarvestCache; import votorola.g.lang.ThreadSafe; /** * Data structure for message-contexts as long as the Url is unknown and the * message is therefore only known (including diff-urls) to determine harvesting * {@linkplain votorola.a.diff.harvest.kick.UpdateKick}s. If you already know * the url of the posting on the web-archive and all the other data, then you * can pass a {@linkplain Message} object to {@linkplain HarvestCache} to store * it directly without web harvesting. Look at the documentation of * {@linkplain HarvestCache} to get an overview over the caching process. * * @see Message * @see HarvestCache */ @ThreadSafe final public class MessageContext { /** * All fields may not be null or empty. * @param sender * @param archiveUrl * @param diffUrls * @param sentDate may not have 'getTime()==0' */ private MessageContext(final String sender, final String archiveUrl, final List diffUrls, final Date sentDate) { if (archiveUrl == null) { throw new IllegalArgumentException("archiveUrl is null."); } if (sender == null) { throw new IllegalArgumentException(archiveUrl + ": sender is null."); } if (diffUrls == null) { throw new IllegalArgumentException(archiveUrl + ": diffUrls is null."); } if (sentDate == null) { throw new IllegalArgumentException(archiveUrl + ": sentDate is null."); } if (archiveUrl.isEmpty()) { throw new IllegalArgumentException("archiveUrl is empty."); } if (sender.isEmpty()) { throw new IllegalArgumentException(archiveUrl + ": sender is empty."); } if (diffUrls.isEmpty()) { throw new IllegalArgumentException(archiveUrl + ": diffUrls is empty."); } if (sentDate.getTime()==0) { throw new IllegalArgumentException(archiveUrl + ": sentDate is zero seconds unix time."); } this.sender = sender; this.archiveUrl = archiveUrl; this.diffUrls = Collections.unmodifiableList(diffUrls); this.sentDate = sentDate; } /** * Create a context. * * @param sender * , may not be null * @param archiveUrl * , may not be null * @param diffUrls * , may not be null, set through * {@link HarvestCache#findDiffUrls(String)} * @param sentDate * , may not be null */ public static MessageContext create(final String sender, final String archiveUrl, final List diffUrls, final Date sentDate) { return new MessageContext(sender, archiveUrl, diffUrls, sentDate); } final private String sender; /** * Sender of the message as known to the medium. This is later resolved * through {@linkplain HarvestCache#store(Message, Authenticator)} by a * {@linkplain votorola.a.diff.harvest.auth.Authenticator} to * mailish-usernames of Votorola. * * @return sender */ public String sender() { return sender; } final private String archiveUrl; /** * The base-url which acts as a system wide identifier for archives. * * @return base-url */ public String archiveUrl() { return archiveUrl; } final private List diffUrls; /** * Diff-urls detected in this message. This can be done through * {@linkplain HarvestCache#findDiffUrls(String)} * * @return diff-urls */ public List diffUrls() { return diffUrls; } final private Date sentDate; /** * Date the message has been sent due to the archive information. This * information is crucial for proper archive access, both through harvesting * and later for browsing. * * @return sent-date */ public Date sentDate() { return sentDate; } /** * Compares all constructor variables. * * @return whether the contexts equal each other */ @Override public boolean equals(final Object o) { return (o instanceof MessageContext) && sender.equals(((MessageContext) o).sender) && archiveUrl.equals(((MessageContext) o).archiveUrl) && diffUrls.equals(((MessageContext) o).diffUrls) && sentDate.equals(((MessageContext) o).sentDate); } /** * Obey equals contract. */ @Override public int hashCode() { int hash = sender.hashCode(); hash = hash * 37 + archiveUrl.hashCode(); hash = hash * 37 + diffUrls.hashCode(); hash = hash * 37 + sentDate.hashCode(); return hash; } /** * Prints all constructor variables separated by ":". This format is not * guaranteed. */ @Override public String toString() { return archiveUrl + "|" + sender + "|" + sentDate.toString() + "|" + diffUrls.toString(); } }