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