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 2006-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS.
026 */
027package org.opends.server.replication.plugin;
028
029import org.forgerock.opendj.ldap.ByteString;
030import org.opends.server.replication.common.CSN;
031
032/** Store historical information for an attribute value. */
033public class AttrValueHistorical
034{
035  private ByteString value;
036  private CSN valueDeleteTime;
037  private CSN valueUpdateTime;
038
039  /**
040   * Build an AttrValueHistorical for a provided attribute value, providing
041   * the last time the provided value is either updated or deleted.
042   * @param value    the provided attributeValue
043   * @param csnUpdate last time when this value was updated
044   * @param csnDelete last time when this value for deleted
045   */
046  public AttrValueHistorical(ByteString value, CSN csnUpdate, CSN csnDelete)
047  {
048    this.value = value;
049    this.valueUpdateTime = csnUpdate;
050    this.valueDeleteTime = csnDelete;
051  }
052
053  /**
054   * Compares this object with another ValueInfo object.
055   * Object are said equals when their values matches.
056   * @param obj object to be compared with this object
057   * @return true if equal, false otherwise
058   */
059  @Override
060  public boolean equals(Object obj)
061  {
062    if (obj instanceof AttrValueHistorical)
063    {
064      AttrValueHistorical objVal = (AttrValueHistorical) obj;
065      return value.equals(objVal.getAttributeValue());
066    }
067    return false;
068  }
069
070  /**
071   * Calculates the hasCode for this object.
072   * Only value is used when calculating the hashCode
073   * @return the hashcode
074   */
075  @Override
076  public int hashCode()
077  {
078    return value.hashCode();
079  }
080
081  /**
082   * Get the last time when the value was deleted.
083   * @return the last time when the value was deleted
084   */
085  public CSN getValueDeleteTime()
086  {
087    return valueDeleteTime;
088  }
089
090  /**
091   * Get the last time when the value was updated.
092   * @return the last time when the value was updated
093   */
094  public CSN getValueUpdateTime()
095  {
096    return valueUpdateTime;
097  }
098
099  /**
100   * Get the attributeValue for which this object was generated.
101   * @return the value for which this object was generated
102   */
103  public ByteString getAttributeValue()
104  {
105    return value;
106  }
107
108  /**
109   * Check if the value associated with this object was updated.
110   * @return true if the value associated with this object was updated
111   */
112  public boolean isUpdate()
113  {
114    return valueUpdateTime != null;
115  }
116
117  @Override
118  public String toString()
119  {
120    if (valueUpdateTime != null)
121    {
122      return valueDeleteTime != null
123          // valueUpdateTime and valueDeleteTime should have the same value
124          ? valueUpdateTime + ":replace:" + value
125          : valueUpdateTime + ":add:" + value;
126    }
127    else
128    {
129      return valueDeleteTime != null
130          ? valueDeleteTime + ":delete:" + value
131          : "????:" + value;
132    }
133  }
134}