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 2006-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2015 ForgeRock AS.
016 */
017package org.opends.server.replication.plugin;
018
019import org.forgerock.opendj.ldap.ByteString;
020import org.opends.server.replication.common.CSN;
021
022/** AttrValueHistorical is the historical information of the modification of one attribute value. */
023public class AttrValueHistorical
024{
025  private ByteString value;
026  private CSN valueDeleteTime;
027  private CSN valueUpdateTime;
028
029  /**
030   * Build an AttrValueHistorical for a provided attribute value, providing
031   * the last time the provided value is either updated or deleted.
032   * @param value    the provided attributeValue
033   * @param csnUpdate last time when this value was updated
034   * @param csnDelete last time when this value for deleted
035   */
036  public AttrValueHistorical(ByteString value, CSN csnUpdate, CSN csnDelete)
037  {
038    this.value = value;
039    this.valueUpdateTime = csnUpdate;
040    this.valueDeleteTime = csnDelete;
041  }
042
043  @Override
044  public boolean equals(Object obj)
045  {
046    if (obj instanceof AttrValueHistorical)
047    {
048      AttrValueHistorical objVal = (AttrValueHistorical) obj;
049      return value.equals(objVal.getAttributeValue());
050    }
051    return false;
052  }
053
054  @Override
055  public int hashCode()
056  {
057    return value.hashCode();
058  }
059
060  /**
061   * Get the last time when the value was deleted.
062   * @return the last time when the value was deleted
063   */
064  public CSN getValueDeleteTime()
065  {
066    return valueDeleteTime;
067  }
068
069  /**
070   * Get the last time when the value was updated.
071   * @return the last time when the value was updated
072   */
073  public CSN getValueUpdateTime()
074  {
075    return valueUpdateTime;
076  }
077
078  /**
079   * Get the attributeValue for which this object was generated.
080   * @return the value for which this object was generated
081   */
082  public ByteString getAttributeValue()
083  {
084    return value;
085  }
086
087  /**
088   * Check if the value associated with this object was updated.
089   * @return true if the value associated with this object was updated
090   */
091  public boolean isUpdate()
092  {
093    return valueUpdateTime != null;
094  }
095
096  @Override
097  public String toString()
098  {
099    if (valueUpdateTime != null)
100    {
101      return valueDeleteTime != null
102          // valueUpdateTime and valueDeleteTime should have the same value
103          ? valueUpdateTime + ":replace:" + value
104          : valueUpdateTime + ":add:" + value;
105    }
106    else
107    {
108      return valueDeleteTime != null
109          ? valueDeleteTime + ":delete:" + value
110          : "????:" + value;
111    }
112  }
113}