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 Crossforum.
009 * @see DraftPair
010 * @see Cache
011 * @see Message
012 * @see HarvestTable
013 */
014
015import votorola.a.diff.harvest.Message;
016import votorola.a.diff.harvest.auth.Authenticator;
017import votorola.g.lang.ThreadSafe;
018import votorola.g.lang.Warning;
019
020/**
021 * Authenticated difference messages contain both the difference and authorship
022 * information gained when {@link Authenticator} matches a {@link DiffMessage}.
023 * 
024 * @see HarvestCache
025 * @see DiffMessage
026 * 
027 */
028@ThreadSafe
029public class AuthDiffMessage {
030
031    final private DiffMessage dmsg;
032
033    public DiffMessage diffMessage() {
034        return dmsg;
035    }
036
037    /**
038     * Sender of the message as verified email.
039     * 
040     * @return author
041     */
042    public String author() {
043        return author;
044    }
045
046    private final String author;
047
048    /**
049     * Addressee of the message as email.
050     * 
051     * @return addressee
052     */
053    public String addressee() {
054        return addressee;
055    }
056
057    /**
058     * This is the selectand of a difference.
059     * 
060     * @return selectand string, e.g. "a" or "b"
061     */
062    public String selectand() {
063        return selectand;
064    }
065
066    private final String addressee;
067    private final String selectand;
068
069    private AuthDiffMessage(final DiffMessage dmsg, final String author,
070            final String addressee, final String selectand) {
071        this.dmsg = dmsg;
072        this.author = author;
073        this.addressee = addressee;
074        this.selectand = selectand;
075    }
076
077    /**
078     * Create a DiffMessage from a {@linkplain Message} and decorate it with
079     * difference information.
080     * 
081     * @param dmsg
082     * @param author
083     * @param addressee
084     * @param selectand
085     */
086    public static AuthDiffMessage create(final DiffMessage dmsg,
087            final String author, final String addressee, final String selectand) {
088        return new AuthDiffMessage(dmsg, author, addressee, selectand);
089    }
090
091    @Override
092    public boolean equals(Object o) {
093        if (o == this) {
094            return true;
095        }
096        return (o instanceof AuthDiffMessage)
097                && ((AuthDiffMessage) o).dmsg.equals(dmsg)
098                && ((AuthDiffMessage) o).author.equals(author)
099                && ((AuthDiffMessage) o).addressee.equals(addressee)
100                && ((AuthDiffMessage) o).selectand.equals(selectand);
101    }
102
103    @Override
104    public int hashCode() {
105        int hash = dmsg.hashCode();
106        hash = hash * 37 + author.hashCode();
107        hash = hash * 37 + addressee.hashCode();
108        hash = hash * 37 + selectand.hashCode();
109        return hash;
110    }
111
112    /**
113     * Return {@linkplain DiffMessage} contents as well as author, addressee and
114     * selectand. The format is not guaranteed to be stable.
115     */
116    @Override
117    public String toString() {
118        return dmsg.toString() + "|" + author + "|" + addressee + "|"
119                + selectand;
120    }
121}