001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors.
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     * 
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.graph.observe;
025    
026    import java.io.Serializable;
027    import java.util.Iterator;
028    import java.util.List;
029    import net.jcip.annotations.Immutable;
030    import org.jboss.dna.graph.SecurityContext;
031    import org.jboss.dna.graph.property.DateTime;
032    import org.jboss.dna.graph.request.ChangeRequest;
033    
034    /**
035     * A set of changes that were made atomically. Each change is in the form of a frozen {@link ChangeRequest}.
036     */
037    @Immutable
038    public final class Changes implements Iterable<ChangeRequest>, Comparable<Changes>, Serializable {
039    
040        private static final long serialVersionUID = 1L;
041    
042        private final String processId;
043        private final String userName;
044        private final String sourceName;
045        private final DateTime timestamp;
046        private final List<ChangeRequest> changeRequests;
047    
048        public Changes( String userName,
049                        String sourceName,
050                        DateTime timestamp,
051                        List<ChangeRequest> requests ) {
052            this("", userName, sourceName, timestamp, requests);
053        }
054    
055        public Changes( String processId,
056                        String userName,
057                        String sourceName,
058                        DateTime timestamp,
059                        List<ChangeRequest> requests ) {
060            this.userName = userName;
061            this.sourceName = sourceName;
062            this.timestamp = timestamp;
063            this.changeRequests = requests;
064            this.processId = processId != null ? processId : "";
065        }
066    
067        /**
068         * Get the user that made these changes.
069         * 
070         * @return the user; never null
071         * @see SecurityContext#getUserName()
072         */
073        public String getUserName() {
074            return this.userName;
075        }
076    
077        /**
078         * Get the name of the source that was changed.
079         * 
080         * @return the source name; never null
081         */
082        public String getSourceName() {
083            return this.sourceName;
084        }
085    
086        /**
087         * Get the timestamp that the changes were made. All changes within the change set were all made at this instant in time.
088         * 
089         * @return the timestamp of the changes; never null
090         */
091        public DateTime getTimestamp() {
092            return this.timestamp;
093        }
094    
095        /**
096         * Get the identifier of the process where these changes originated. This identifier may be useful in preventing feedbacks.
097         * 
098         * @return the process identifier; never null
099         */
100        public String getProcessId() {
101            return processId;
102        }
103    
104        /**
105         * {@inheritDoc}
106         * 
107         * @see java.lang.Iterable#iterator()
108         */
109        public Iterator<ChangeRequest> iterator() {
110            return this.changeRequests.iterator();
111        }
112    
113        /**
114         * {@inheritDoc}
115         * 
116         * @see java.lang.Object#hashCode()
117         */
118        @Override
119        public int hashCode() {
120            return getTimestamp().hashCode();
121        }
122    
123        /**
124         * {@inheritDoc}
125         * 
126         * @see java.lang.Comparable#compareTo(java.lang.Object)
127         */
128        public int compareTo( Changes that ) {
129            if (this == that) return 0;
130            return this.getTimestamp().compareTo(that.getTimestamp());
131        }
132    
133        /**
134         * {@inheritDoc}
135         * 
136         * @see java.lang.Object#equals(java.lang.Object)
137         */
138        @Override
139        public boolean equals( Object obj ) {
140            if (obj == this) return true;
141            if (obj instanceof Changes) {
142                Changes that = (Changes)obj;
143                if (!this.getProcessId().equals(that.getProcessId())) return false;
144                if (!this.getSourceName().equals(that.getSourceName())) return false;
145                if (!this.getTimestamp().equals(that.getTimestamp())) return false;
146                if (!this.getUserName().equals(that.getUserName())) return false;
147                return true;
148            }
149            return false;
150        }
151    }