001    
002    /*
003     * Copyright (C) 2012 Archie L. Cobbs. All rights reserved.
004     *
005     * $Id: PersistentObjectEvent.java 230 2012-01-18 17:08:03Z archie.cobbs $
006     */
007    
008    package org.dellroad.stuff.pobj;
009    
010    import java.util.EventObject;
011    
012    /**
013     * Notification event emitted by a {@link PersistentObject} to listeners whenever there is an update to the root object.
014     *
015     * @param <T> type of the root persistent object
016     */
017    @SuppressWarnings("serial")
018    public class PersistentObjectEvent<T> extends EventObject {
019    
020        private final long version;
021        private final T oldRoot;
022        private final T newRoot;
023    
024        public PersistentObjectEvent(PersistentObject<T> persistentObject, long version, T oldRoot, T newRoot) {
025            super(persistentObject);
026            this.version = version;
027            this.oldRoot = oldRoot;
028            this.newRoot = newRoot;
029        }
030    
031        /**
032         * Get the {@link PersistentObject} that originated this event.
033         */
034        @SuppressWarnings("unchecked")
035        public PersistentObject<T> getSource() {
036            return (PersistentObject<T>)super.getSource();
037        }
038    
039        /**
040         * Get the version that this event is associated with. This will be the version of the {@linkplain #getNewRoot new root}.
041         *
042         * <p>
043         * The {@link PersistentObject} class always delivers notifications in order, so this
044         * number should always increase over time.
045         */
046        public long getVersion() {
047            return this.version;
048        }
049    
050        /**
051         * Get the old root prior to the update.
052         *
053         * <p>
054         * The caller must not modify the returned object, as it is shared among all listeners.
055         */
056        public T getOldRoot() {
057            return this.oldRoot;
058        }
059    
060        /**
061         * Get the new root after to the update.
062         *
063         * <p>
064         * The caller must not modify the returned object, as it is shared among all listeners.
065         */
066        public T getNewRoot() {
067            return this.newRoot;
068        }
069    }
070