001package votorola.a.diff.harvest.cache;// Copyright 2010-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 contains a derived Message object, which
005 * contains all the original message data and additionally
006 * information to the single diffKey URL it maps to in this
007 * message. This is what is stored in the database.
008 * This is losely connected to the Bite type of {@link votorola.s.wap.HarvestWAP}.
009 * @see DraftPair
010 * @see Cache
011 * @see Message
012 * @see HarvestTable
013 */
014
015import votorola.a.diff.DraftPair;
016import votorola.a.diff.harvest.Message;
017import votorola.a.diff.harvest.auth.Authenticator;
018import votorola.g.lang.ThreadSafe;
019
020/**
021 * DiffMessages are created by HarvestCache from {@linkplain Message}
022 * objects by {@linkplain HarvestCache#store(Message, Authenticator)}.
023 * 
024 * @see HarvestCache
025 * @see Message
026 * 
027 */
028public @ThreadSafe
029class DiffMessage {
030
031    final private Message msg;
032
033    /**
034     * The message containing this difference.
035     * 
036     * @return wrapped message
037     */
038    public Message message() {
039        return msg;
040    }
041
042    /**
043     * Representing a valid {@link votorola.a.diff.DraftPair}.
044     * 
045     * @return draft-pair
046     */
047    public DraftPair draftPair() {
048        return draftPair;
049    }
050
051    private final DraftPair draftPair;
052
053    /**
054     * Constructor to create a DiffMessage from a {@linkplain Message} and
055     * decorate it with difference information.
056     * 
057     * @param msg
058     * @param draftPair
059     *            used internally to compute, but is only stored as
060     *            {@link votorola.a.diff.DiffKey}
061     */
062    DiffMessage(final Message msg, final DraftPair draftPair) {
063        this.msg = msg;
064        this.draftPair = draftPair;
065    }
066    
067    @Override 
068    public boolean equals(Object o) {
069        if( o == this ) {
070            return true;
071        }
072        return (o instanceof DiffMessage) 
073                && ((DiffMessage)o).msg.equals(msg)
074                && ((DiffMessage)o).draftPair.equals(draftPair);
075    }
076    
077    @Override
078    public int hashCode() {
079        int hash = msg.hashCode();
080        hash = msg.hashCode()*37 + draftPair.hashCode();
081        return hash;
082    }
083    
084    /**
085     * Returns message content and draft-pair. This is not 
086     * guaranteed to stable.
087     */
088    @Override
089    public String toString() {
090        return msg.toString() + "|" + draftPair.toString();
091    }
092}