001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008-2011 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.datamodel;
029
030import org.opends.server.types.DN;
031
032
033/**
034 * This class is used to represent a Base DN / Replica and is aimed to be
035 * used by the classes in the BackendTableModel class.
036 *
037 */
038public class BaseDNDescriptor implements Comparable<BaseDNDescriptor>
039{
040  /**
041   * An enumeration describing the type of base DN for a given backend.
042   */
043  public enum Type
044  {
045    /**
046     * The base DN is not replicated.
047     */
048    NOT_REPLICATED,
049    /**
050     * The base DN is replicated.
051     */
052    REPLICATED,
053    /**
054     * Replication is disabled.
055     */
056    DISABLED
057  }
058
059  private int nEntries;
060  private int missingChanges;
061  private BackendDescriptor backend;
062  private long ageOfOldestMissingChange;
063  private Type type;
064  private final DN baseDn;
065  private int replicaID = -1;
066
067  private int hashCode;
068
069  /**
070   * Constructor for this class.
071   * @param type the type of replication.
072   * @param baseDn the base DN associated with the Replication.
073   * @param backend the backend containing this base DN.
074   * @param nEntries the number of entries for the base DN.
075   * @param ageOfOldestMissingChange the number of missing changes.
076   * @param missingChanges the number of missing changes.
077   */
078  public BaseDNDescriptor(Type type, DN baseDn, BackendDescriptor backend,
079      int nEntries, long ageOfOldestMissingChange, int missingChanges)
080  {
081    this.baseDn = baseDn;
082    this.backend = backend;
083    this.type = type;
084    this.nEntries = nEntries;
085    this.ageOfOldestMissingChange = ageOfOldestMissingChange;
086    this.missingChanges = missingChanges;
087
088    if (backend != null)
089    {
090      recalculateHashCode();
091    }
092  }
093
094  /**
095   * Return the String DN associated with the base DN..
096   * @return the String DN associated with the base DN.
097   */
098  public DN getDn()
099  {
100    return baseDn;
101  }
102
103  /** {@inheritDoc} */
104  @Override
105  public boolean equals(Object v)
106  {
107    if (this == v)
108    {
109      return true;
110    }
111    if (!(v instanceof BaseDNDescriptor))
112    {
113      return false;
114    }
115
116    BaseDNDescriptor desc = (BaseDNDescriptor)v;
117    return getType() == desc.getType()
118        && getDn().equals(desc.getDn())
119        && getAgeOfOldestMissingChange() == desc.getAgeOfOldestMissingChange()
120        && getMissingChanges() == desc.getMissingChanges()
121        && getEntries() == desc.getEntries()
122        && backendIdEqual(desc);
123  }
124
125  private boolean backendIdEqual(BaseDNDescriptor desc)
126  {
127    // Only compare the backend IDs.  In this context is enough
128    return getBackend() != null
129        && desc.getBackend() != null
130        && getBackend().getBackendID().equals(desc.getBackend().getBackendID());
131  }
132
133  /** {@inheritDoc} */
134  @Override
135  public int hashCode()
136  {
137    return hashCode;
138  }
139
140  /** {@inheritDoc} */
141  public int compareTo(BaseDNDescriptor desc)
142  {
143    int returnValue = desc.getDn().compareTo(getDn());
144    if (returnValue == 0)
145    {
146      returnValue = getType().compareTo(desc.getType());
147    }
148    if (returnValue == 0 && getBackend() != null && desc.getBackend() != null)
149    {
150      // Only compare the backend IDs. In this context is enough
151      returnValue = getBackend().getBackendID().compareTo(
152          desc.getBackend().getBackendID());
153    }
154    if (returnValue == 0)
155    {
156      returnValue = compare(getEntries(), desc.getEntries());
157    }
158    if (returnValue == 0)
159    {
160      returnValue = compare(getAgeOfOldestMissingChange(),
161          desc.getAgeOfOldestMissingChange());
162    }
163    if (returnValue == 0)
164    {
165      returnValue = compare(getMissingChanges(), desc.getMissingChanges());
166    }
167    return returnValue;
168  }
169
170  /**
171   * Returns the number of entries in the backend for this base DN.
172   * @return the number of entries in the backend for this base DN.
173   */
174  public int getEntries()
175  {
176    return nEntries;
177  }
178
179  /**
180   * Returns the number of missing changes in the replication topology for
181   * this base DN.
182   * @return the number of missing changes in the replication topology for
183   * this base DN.
184   */
185  public int getMissingChanges()
186  {
187    return missingChanges;
188  }
189
190  /**
191   * Sets the number of missing changes in the replication topology for
192   * this base DN.
193   * @param missingChanges the missing changes.
194   */
195  public void setMissingChanges(int missingChanges)
196  {
197    this.missingChanges = missingChanges;
198    recalculateHashCode();
199  }
200
201  /**
202   * Returns the age of the oldest missing change in seconds in the
203   * replication topology for this base DN.
204   * @return the age of the oldest missing change in seconds in the
205   * replication topology for this base DN.
206   */
207  public long getAgeOfOldestMissingChange()
208  {
209    return ageOfOldestMissingChange;
210  }
211
212  /**
213   * Sets the age of the oldest missing change in seconds in the
214   * replication topology for this base DN.
215   * @param ageOfOldestMissingChange the age of the oldest missing change in
216   * seconds in the replication topology for this base DN.
217   */
218  public void setAgeOfOldestMissingChange(long ageOfOldestMissingChange)
219  {
220    this.ageOfOldestMissingChange = ageOfOldestMissingChange;
221    recalculateHashCode();
222  }
223
224  /**
225   * Returns the type for this base DN.
226   * @return the type for this base DN.
227   */
228  public Type getType()
229  {
230    return type;
231  }
232
233  /**
234   * Returns the backend where this base DN is defined.
235   * @return the backend where this base DN is defined.
236   */
237  public BackendDescriptor getBackend()
238  {
239    return backend;
240  }
241
242
243  /**
244   * Sets the backend of this base DN.
245   * @param backend the backend for this base DN.
246   */
247  public void setBackend(BackendDescriptor backend)
248  {
249    this.backend = backend;
250    recalculateHashCode();
251  }
252
253  /**
254   * Sets the type of this base DN.
255   * @param type the new type for this base DN.
256   */
257  public void setType(Type type)
258  {
259    this.type = type;
260    recalculateHashCode();
261  }
262
263  /**
264   * Sets the number of entries for this base DN in this database.
265   * @param nEntries the number of entries.
266   */
267  public void setEntries(int nEntries)
268  {
269    this.nEntries = nEntries;
270    recalculateHashCode();
271  }
272
273  /**
274   * Returns the ID of the replication domain associated with this base DN. -1
275   * if this base DN is not replicated.
276   * @return the ID of the replication domain associated with this base DN.
277   */
278  public int getReplicaID()
279  {
280    return replicaID;
281  }
282
283  /**
284   * Sets the ID of the replication domain associated with this base DN.
285   * @param replicaID the ID of the replication domain associated with this base
286   * DN.
287   */
288  public void setReplicaID(int replicaID)
289  {
290    this.replicaID = replicaID;
291    recalculateHashCode();
292  }
293
294  /**
295   * Method called when one of the elements that affect the value of the
296   * hashcode is modified.  It is used to minimize the time spent calculating
297   * hashCode.
298   *
299   */
300  private void recalculateHashCode()
301  {
302    hashCode = (getType().toString() + getAgeOfOldestMissingChange() +
303          getDn() +
304        getBackend().getBackendID() + getMissingChanges()).hashCode();
305  }
306
307  private int compare(int i1, int i2)
308  {
309    if (i1 == i2)
310    {
311      return 0;
312    }
313    else if (i1 > i2)
314    {
315      return 1;
316    }
317    else
318    {
319      return -1;
320    }
321  }
322
323  private int compare(long i1, long i2)
324  {
325    if (i1 == i2)
326    {
327      return 0;
328    }
329    else if (i1 > i2)
330    {
331      return 1;
332    }
333    else
334    {
335      return -1;
336    }
337  }
338}