1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.mitosis.common;
21
22
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.apache.directory.mitosis.service.protocol.handler.ReplicationClientContextHandler;
29 import org.apache.directory.shared.ldap.util.EqualsBuilder;
30 import org.apache.directory.shared.ldap.util.HashCodeBuilder;
31
32
33 /**
34 * Creates a set of {@link CSN}s, which is defined in LDUP specification.
35 * Each {@link CSN} in the same {@link CSNVector} has different
36 * Replica Id from each other. Its data structure is
37 * similar to a {@link Map} whose key is the Replica Id.
38 * <p>
39 * {@link CSNVector} is usually used to represent 'Update Vector (UV)' and
40 * 'Purge Vector (PV)'. Please refer to the LDUP specification and other
41 * Mitosis classes such as {@link ReplicationClientContextHandler}.
42 *
43 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44 */
45 public class CSNVector
46 {
47 /**
48 * Declares the Serial Version Uid.
49 *
50 * @see <a
51 * href="http://c2.com/cgi/wiki?AlwaysDeclareSerialVersionUid">Always
52 * Declare Serial Version Uid</a>
53 */
54 private static final long serialVersionUID = 1L;
55
56 /** The internal structure holding the CSNs */
57 private final Map<String,CSN> csns = new HashMap<String,CSN>();
58
59 /**
60 * Creates a new empty instance.
61 */
62 public CSNVector()
63 {
64 }
65
66 /**
67 * Adds the specified <tt>csn</tt> to this vector. If there's a
68 * {@link CSN} with the same ReplicaId, it is replaced by
69 * the specified <tt>csn</tt>.
70 */
71 public void setCSN( CSN csn )
72 {
73 csns.put( csn.getReplicaId(), csn );
74 }
75
76
77 /**
78 * Adds all {@link CSN}s that the specified <tt>vector</tt> contains to
79 * this vector. If there's a {@link CSN} with the same ReplicaId
80 * in this vector, it is replaced by the {@link CSN} in the specified
81 * <tt>vector</tt>.
82 */
83 public void setAllCSN( CSNVector vector )
84 {
85 Iterator<CSN> i = vector.csns.values().iterator();
86 while ( i.hasNext() )
87 {
88 setCSN( i.next() );
89 }
90 }
91
92 /**
93 * Returns the {@link CSN} with the specified <tt>replicaId</tt> from
94 * this vector.
95 *
96 * @return <tt>null</tt> if there's no match
97 */
98 public CSN getCSN( String replicaId )
99 {
100 return csns.get( replicaId );
101 }
102
103
104 /**
105 * Removed the {@link CSN} with the specified <tt>replicaId</tt> from
106 * this vector and returns the removed {@link CSN}.
107 *
108 * @return <tt>null</tt> if there's no match
109 */
110 public CSN removeCSN( String replicaId )
111 {
112 return csns.remove( replicaId );
113 }
114
115
116 /**
117 * Returns the {@link Set} of the ReplicaIds extracted from
118 * the {@link CSN}s in this vector.
119 */
120 public Set<String> getReplicaIds()
121 {
122 return csns.keySet();
123 }
124
125 /**
126 * Returns the number of {@link CSN}s that this vector contains.
127 */
128 public int size()
129 {
130 return csns.size();
131 }
132
133 /**
134 * Returns <tt>true</tt> if and if only the specified <tt>object</tt> is
135 * a {@link CSNVector} and contains the {@link CSN}s with the same values.
136 */
137 public boolean equals( Object object )
138 {
139 if ( object == this )
140 {
141 return true;
142 }
143
144 if ( !( object instanceof CSNVector ) )
145 {
146 return false;
147 }
148
149 CSNVector rhs = ( CSNVector ) object;
150 return new EqualsBuilder().append( this.csns, rhs.csns ).isEquals();
151 }
152
153 /**
154 * Returns the hash code of this vector, calculated from each {@link CSN}
155 * element.
156 * @return the instance's hashcode
157 */
158 public int hashCode()
159 {
160 return new HashCodeBuilder( -33446267, -459427867 ).append( this.csns ).toHashCode();
161 }
162
163
164 /**
165 * Creates a deep copy of this vector and returns it.
166 */
167 public CSNVector clone()
168 {
169 CSNVector result = new CSNVector();
170 result.csns.putAll( this.csns );
171 return result;
172 }
173
174
175 public String toString()
176 {
177 return csns.toString();
178 }
179 }