View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.observe;
25  
26  import java.io.Serializable;
27  import java.util.Collections;
28  import java.util.List;
29  import net.jcip.annotations.Immutable;
30  import org.modeshape.graph.ExecutionContext;
31  import org.modeshape.graph.SecurityContext;
32  import org.modeshape.graph.property.DateTime;
33  import org.modeshape.graph.request.ChangeRequest;
34  
35  /**
36   * A set of changes that were made atomically. Each change is in the form of a frozen {@link ChangeRequest}.
37   */
38  @Immutable
39  public class Changes implements Comparable<Changes>, Serializable {
40  
41      private static final long serialVersionUID = 1L;
42  
43      protected final String processId;
44      protected final String contextId;
45      protected final String userName;
46      protected final String sourceName;
47      protected final DateTime timestamp;
48      protected final List<ChangeRequest> changeRequests;
49  
50      public Changes( String processId,
51                      String contextId,
52                      String userName,
53                      String sourceName,
54                      DateTime timestamp,
55                      List<ChangeRequest> requests ) {
56          assert requests != null;
57          assert !requests.isEmpty();
58          this.userName = userName;
59          this.sourceName = sourceName;
60          this.timestamp = timestamp;
61          this.changeRequests = Collections.unmodifiableList(requests);
62          this.processId = processId != null ? processId : "";
63          this.contextId = contextId != null ? contextId : "";
64          assert this.userName != null;
65          assert this.sourceName != null;
66          assert this.timestamp != null;
67          assert this.changeRequests != null;
68          assert this.processId != null;
69      }
70  
71      protected Changes( Changes changes ) {
72          this.userName = changes.userName;
73          this.sourceName = changes.sourceName;
74          this.timestamp = changes.timestamp;
75          this.changeRequests = changes.changeRequests;
76          this.processId = changes.processId;
77          this.contextId = changes.contextId;
78          assert this.userName != null;
79          assert this.sourceName != null;
80          assert this.timestamp != null;
81          assert this.changeRequests != null;
82          assert this.processId != null;
83          assert this.contextId != null;
84      }
85  
86      /**
87       * Get the user that made these changes.
88       * 
89       * @return the user; never null
90       * @see SecurityContext#getUserName()
91       */
92      public String getUserName() {
93          return this.userName;
94      }
95  
96      /**
97       * Get the name of the source that was changed.
98       * 
99       * @return the source name; never null
100      */
101     public String getSourceName() {
102         return this.sourceName;
103     }
104 
105     /**
106      * Get the timestamp that the changes were made. All changes within the change set were all made at this instant in time.
107      * 
108      * @return the timestamp of the changes; never null
109      */
110     public DateTime getTimestamp() {
111         return this.timestamp;
112     }
113 
114     /**
115      * Get the identifier of the process where these changes originated. This identifier may be useful in preventing feedbacks.
116      * 
117      * @return the process identifier; never null
118      */
119     public String getProcessId() {
120         return processId;
121     }
122 
123     /**
124      * Get the {@link ExecutionContext#getId() identifier} of the {@link ExecutionContext} where these changes originated. This
125      * identifier may be useful in preventing feedbacks.
126      * 
127      * @return the context identifier; never null
128      */
129     public String getContextId() {
130         return contextId;
131     }
132 
133     /**
134      * Get the list of changes.
135      * 
136      * @return the immutable list of change requests; never null and never empty
137      */
138     public List<ChangeRequest> getChangeRequests() {
139         return changeRequests;
140     }
141 
142     /**
143      * {@inheritDoc}
144      * 
145      * @see java.lang.Object#hashCode()
146      */
147     @Override
148     public int hashCode() {
149         return getTimestamp().hashCode();
150     }
151 
152     /**
153      * {@inheritDoc}
154      * 
155      * @see java.lang.Comparable#compareTo(java.lang.Object)
156      */
157     public int compareTo( Changes that ) {
158         if (this == that) return 0;
159         return this.getTimestamp().compareTo(that.getTimestamp());
160     }
161 
162     /**
163      * {@inheritDoc}
164      * 
165      * @see java.lang.Object#equals(java.lang.Object)
166      */
167     @Override
168     public boolean equals( Object obj ) {
169         if (obj == this) return true;
170         if (obj instanceof Changes) {
171             Changes that = (Changes)obj;
172             if (!this.getProcessId().equals(that.getProcessId())) return false;
173             if (!this.getContextId().equals(that.getContextId())) return false;
174             if (!this.getSourceName().equals(that.getSourceName())) return false;
175             if (!this.getTimestamp().equals(that.getTimestamp())) return false;
176             if (!this.getUserName().equals(that.getUserName())) return false;
177             return true;
178         }
179         return false;
180     }
181 
182     /**
183      * {@inheritDoc}
184      * 
185      * @see java.lang.Object#toString()
186      */
187     @Override
188     public String toString() {
189         if (processId.length() != 0) {
190             return getTimestamp() + " @" + getUserName() + " [" + getSourceName() + "] - " + changeRequests.size() + " events";
191         }
192         return getTimestamp() + " @" + getUserName() + " #" + getProcessId() + " [" + getSourceName() + "] - "
193                + changeRequests.size() + " events";
194     }
195 }