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 *
027 */
028public class BaseDNDescriptor implements Comparable<BaseDNDescriptor>
029{
030  /**
031   * An enumeration describing the type of base DN for a given backend.
032   */
033  public enum Type
034  {
035    /**
036     * The base DN is not replicated.
037     */
038    NOT_REPLICATED,
039    /**
040     * The base DN is replicated.
041     */
042    REPLICATED,
043    /**
044     * Replication is disabled.
045     */
046    DISABLED
047  }
048
049  private int nEntries;
050  private int missingChanges;
051  private BackendDescriptor backend;
052  private long ageOfOldestMissingChange;
053  private Type type;
054  private final DN baseDn;
055  private int replicaID = -1;
056
057  private int hashCode;
058
059  /**
060   * Constructor for this class.
061   * @param type the type of replication.
062   * @param baseDn the base DN associated with the Replication.
063   * @param backend the backend containing this base DN.
064   * @param nEntries the number of entries for the base DN.
065   * @param ageOfOldestMissingChange the number of missing changes.
066   * @param missingChanges the number of missing changes.
067   */
068  public BaseDNDescriptor(Type type, DN baseDn, BackendDescriptor backend,
069      int nEntries, long ageOfOldestMissingChange, int missingChanges)
070  {
071    this.baseDn = baseDn;
072    this.backend = backend;
073    this.type = type;
074    this.nEntries = nEntries;
075    this.ageOfOldestMissingChange = ageOfOldestMissingChange;
076    this.missingChanges = missingChanges;
077
078    if (backend != null)
079    {
080      recalculateHashCode();
081    }
082  }
083
084  /**
085   * Return the String DN associated with the base DN..
086   * @return the String DN associated with the base DN.
087   */
088  public DN getDn()
089  {
090    return baseDn;
091  }
092
093  /** {@inheritDoc} */
094  @Override
095  public boolean equals(Object v)
096  {
097    if (this == v)
098    {
099      return true;
100    }
101    if (!(v instanceof BaseDNDescriptor))
102    {
103      return false;
104    }
105
106    BaseDNDescriptor desc = (BaseDNDescriptor)v;
107    return getType() == desc.getType()
108        && getDn().equals(desc.getDn())
109        && getAgeOfOldestMissingChange() == desc.getAgeOfOldestMissingChange()
110        && getMissingChanges() == desc.getMissingChanges()
111        && getEntries() == desc.getEntries()
112        && backendIdEqual(desc);
113  }
114
115  private boolean backendIdEqual(BaseDNDescriptor desc)
116  {
117    // Only compare the backend IDs.  In this context is enough
118    return getBackend() != null
119        && desc.getBackend() != null
120        && getBackend().getBackendID().equals(desc.getBackend().getBackendID());
121  }
122
123  /** {@inheritDoc} */
124  @Override
125  public int hashCode()
126  {
127    return hashCode;
128  }
129
130  /** {@inheritDoc} */
131  public int compareTo(BaseDNDescriptor desc)
132  {
133    int returnValue = desc.getDn().compareTo(getDn());
134    if (returnValue == 0)
135    {
136      returnValue = getType().compareTo(desc.getType());
137    }
138    if (returnValue == 0 && getBackend() != null && desc.getBackend() != null)
139    {
140      // Only compare the backend IDs. In this context is enough
141      returnValue = getBackend().getBackendID().compareTo(
142          desc.getBackend().getBackendID());
143    }
144    if (returnValue == 0)
145    {
146      returnValue = compare(getEntries(), desc.getEntries());
147    }
148    if (returnValue == 0)
149    {
150      returnValue = compare(getAgeOfOldestMissingChange(),
151          desc.getAgeOfOldestMissingChange());
152    }
153    if (returnValue == 0)
154    {
155      returnValue = compare(getMissingChanges(), desc.getMissingChanges());
156    }
157    return returnValue;
158  }
159
160  /**
161   * Returns the number of entries in the backend for this base DN.
162   * @return the number of entries in the backend for this base DN.
163   */
164  public int getEntries()
165  {
166    return nEntries;
167  }
168
169  /**
170   * Returns the number of missing changes in the replication topology for
171   * this base DN.
172   * @return the number of missing changes in the replication topology for
173   * this base DN.
174   */
175  public int getMissingChanges()
176  {
177    return missingChanges;
178  }
179
180  /**
181   * Sets the number of missing changes in the replication topology for
182   * this base DN.
183   * @param missingChanges the missing changes.
184   */
185  public void setMissingChanges(int missingChanges)
186  {
187    this.missingChanges = missingChanges;
188    recalculateHashCode();
189  }
190
191  /**
192   * Returns the age of the oldest missing change in seconds in the
193   * replication topology for this base DN.
194   * @return the age of the oldest missing change in seconds in the
195   * replication topology for this base DN.
196   */
197  public long getAgeOfOldestMissingChange()
198  {
199    return ageOfOldestMissingChange;
200  }
201
202  /**
203   * Sets the age of the oldest missing change in seconds in the
204   * replication topology for this base DN.
205   * @param ageOfOldestMissingChange the age of the oldest missing change in
206   * seconds in the replication topology for this base DN.
207   */
208  public void setAgeOfOldestMissingChange(long ageOfOldestMissingChange)
209  {
210    this.ageOfOldestMissingChange = ageOfOldestMissingChange;
211    recalculateHashCode();
212  }
213
214  /**
215   * Returns the type for this base DN.
216   * @return the type for this base DN.
217   */
218  public Type getType()
219  {
220    return type;
221  }
222
223  /**
224   * Returns the backend where this base DN is defined.
225   * @return the backend where this base DN is defined.
226   */
227  public BackendDescriptor getBackend()
228  {
229    return backend;
230  }
231
232
233  /**
234   * Sets the backend of this base DN.
235   * @param backend the backend for this base DN.
236   */
237  public void setBackend(BackendDescriptor backend)
238  {
239    this.backend = backend;
240    recalculateHashCode();
241  }
242
243  /**
244   * Sets the type of this base DN.
245   * @param type the new type for this base DN.
246   */
247  public void setType(Type type)
248  {
249    this.type = type;
250    recalculateHashCode();
251  }
252
253  /**
254   * Sets the number of entries for this base DN in this database.
255   * @param nEntries the number of entries.
256   */
257  public void setEntries(int nEntries)
258  {
259    this.nEntries = nEntries;
260    recalculateHashCode();
261  }
262
263  /**
264   * Returns the ID of the replication domain associated with this base DN. -1
265   * if this base DN is not replicated.
266   * @return the ID of the replication domain associated with this base DN.
267   */
268  public int getReplicaID()
269  {
270    return replicaID;
271  }
272
273  /**
274   * Sets the ID of the replication domain associated with this base DN.
275   * @param replicaID the ID of the replication domain associated with this base
276   * DN.
277   */
278  public void setReplicaID(int replicaID)
279  {
280    this.replicaID = replicaID;
281    recalculateHashCode();
282  }
283
284  /**
285   * Method called when one of the elements that affect the value of the
286   * hashcode is modified.  It is used to minimize the time spent calculating
287   * hashCode.
288   *
289   */
290  private void recalculateHashCode()
291  {
292    hashCode = (getType().toString() + getAgeOfOldestMissingChange() +
293          getDn() +
294        getBackend().getBackendID() + getMissingChanges()).hashCode();
295  }
296
297  private int compare(int i1, int i2)
298  {
299    if (i1 == i2)
300    {
301      return 0;
302    }
303    else if (i1 > i2)
304    {
305      return 1;
306    }
307    else
308    {
309      return -1;
310    }
311  }
312
313  private int compare(long i1, long i2)
314  {
315    if (i1 == i2)
316    {
317      return 0;
318    }
319    else if (i1 > i2)
320    {
321      return 1;
322    }
323    else
324    {
325      return -1;
326    }
327  }
328}