/**
 * Copyright (C) 2013 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */

package com.opengamma.financial.credit;

import org.threeten.bp.Period;

import com.opengamma.analytics.financial.credit.StubType;
import com.opengamma.financial.convention.businessday.BusinessDayConvention;
import com.opengamma.financial.convention.businessday.BusinessDayConventionFactory;
import com.opengamma.financial.convention.daycount.DayCount;
import com.opengamma.financial.convention.daycount.DayCountFactory;
import com.opengamma.id.ExternalId;
import com.opengamma.id.ObjectId;
import com.opengamma.id.ObjectIdentifiable;
import com.opengamma.id.UniqueId;
import com.opengamma.id.UniqueIdentifiable;
import com.opengamma.util.ArgumentChecker;

/**
 * Class holding properties of a single name.
 */
public class SingleNameIdentifiable implements UniqueIdentifiable, ObjectIdentifiable {

  public static String SCHEME = "SingleName";
  private static String SEPERATOR = "-";

  private String name;
  private UniqueId id;
  private ExternalId referenceEntity;
  private BusinessDayConvention badDayConvention;
  private DayCount daycount;
  private Period couponFrequency;
  private double recoveryRate;
  private StubType stubType;

  public SingleNameIdentifiable(String name,
                                ExternalId referenceEntity,
                                BusinessDayConvention badDayConvention,
                                DayCount daycount,
                                Period couponFrequency,
                                double recoveryRate,
                                StubType stubType) {
    this.name = name;
    this.referenceEntity = referenceEntity;
    this.badDayConvention = badDayConvention;
    this.daycount = daycount;
    this.couponFrequency = couponFrequency;
    this.recoveryRate = recoveryRate;
    this.stubType = stubType;
    this.id = UniqueId.of(SCHEME, name + SEPERATOR + referenceEntity.getScheme() + SEPERATOR + badDayConvention.getConventionName()
        + SEPERATOR + daycount.getConventionName() + SEPERATOR + couponFrequency + SEPERATOR + recoveryRate + SEPERATOR + stubType);
  }

  public static SingleNameIdentifiable of(final UniqueId id) {
    String[] tokens = id.getValue().split(SEPERATOR);
    ArgumentChecker.isTrue(tokens.length == 7, "Incorrect number of params for SingleNameIdentifiable");
    final String name = tokens[0];
    final ExternalId reference = ExternalId.of(tokens[1], name);
    final BusinessDayConvention badDayConvention = BusinessDayConventionFactory.of(tokens[2]);
    final DayCount dayCount = DayCountFactory.of(tokens[3]);
    final Period couponFrequency = Period.parse(tokens[4]);
    final double recoveryRate = Double.valueOf(tokens[5]);
    final StubType stubType = StubType.valueOf(tokens[6]);
    return new SingleNameIdentifiable(name, reference, badDayConvention, dayCount, couponFrequency, recoveryRate, stubType);
  }

  @Override
  public UniqueId getUniqueId() {
    return id;
  }

  @Override
  public ObjectId getObjectId() {
    return getUniqueId().getObjectId();
  }

  public String getName() {
    return name;
  }

  public ExternalId getReferenceEntity() {
    return referenceEntity;
  }

  public BusinessDayConvention getBadDayConvention() {
    return badDayConvention;
  }

  public DayCount getDaycount() {
    return daycount;
  }

  public Period getCouponFrequency() {
    return couponFrequency;
  }

  public double getRecoveryRate() {
    return recoveryRate;
  }

  public StubType getStubType() {
    return stubType;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    SingleNameIdentifiable that = (SingleNameIdentifiable) o;

    if (Double.compare(that.recoveryRate, recoveryRate) != 0) {
      return false;
    }
    if (badDayConvention != null ? !badDayConvention.equals(that.badDayConvention) : that.badDayConvention != null) {
      return false;
    }
    if (couponFrequency != null ? !couponFrequency.equals(that.couponFrequency) : that.couponFrequency != null) {
      return false;
    }
    if (daycount != null ? !daycount.equals(that.daycount) : that.daycount != null) {
      return false;
    }
    if (id != null ? !id.equals(that.id) : that.id != null) {
      return false;
    }
    if (name != null ? !name.equals(that.name) : that.name != null) {
      return false;
    }
    if (referenceEntity != null ? !referenceEntity.equals(that.referenceEntity) : that.referenceEntity != null) {
      return false;
    }
    if (stubType != that.stubType) {
      return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    int result;
    long temp;
    result = name != null ? name.hashCode() : 0;
    result = 31 * result + (id != null ? id.hashCode() : 0);
    result = 31 * result + (referenceEntity != null ? referenceEntity.hashCode() : 0);
    result = 31 * result + (badDayConvention != null ? badDayConvention.hashCode() : 0);
    result = 31 * result + (daycount != null ? daycount.hashCode() : 0);
    result = 31 * result + (couponFrequency != null ? couponFrequency.hashCode() : 0);
    temp = Double.doubleToLongBits(recoveryRate);
    result = 31 * result + (int) (temp ^ (temp >>> 32));
    result = 31 * result + (stubType != null ? stubType.hashCode() : 0);
    return result;
  }
}

@@ -1221,39 +1221,8 @@
 cy;%0A
-  private double recoveryRate;%0A
   pr
@@ -1246,16 +1246,16 @@
 ubType;%0A
+
 %0A  publi
@@ -1535,61 +1535,8 @@
 cy,%0A
-                                double recoveryRate,%0A
     
@@ -1774,46 +1774,8 @@
 cy;%0A
-    this.recoveryRate = recoveryRate;%0A
     
@@ -2030,35 +2030,8 @@
 OR +
- recoveryRate + SEPERATOR +
  stu
@@ -2208,9 +2208,9 @@
  == 
-7
+6
 , %22I
@@ -2585,67 +2585,8 @@
 %5D);%0A
-    final double recoveryRate = Double.valueOf(tokens%5B5%5D);%0A
     
@@ -2635,17 +2635,17 @@
 (tokens%5B
-6
+5
 %5D);%0A    
@@ -2743,22 +2743,8 @@
 ncy,
- recoveryRate,
  stu
@@ -3264,74 +3264,8 @@
  %7D%0A%0A
-  public double getRecoveryRate() %7B%0A    return recoveryRate;%0A  %7D%0A%0A
   pu
@@ -3559,98 +3559,8 @@
 o;%0A%0A
-    if (Double.compare(that.recoveryRate, recoveryRate) != 0) %7B%0A      return false;%0A    %7D%0A
     
@@ -4419,23 +4419,8 @@
 lt;%0A
-    long temp;%0A
     
@@ -4766,32 +4766,32 @@
 ashCode() : 0);%0A
+
     result = 31 
@@ -4865,115 +4865,8 @@
 0);%0A
-    temp = Double.doubleToLongBits(recoveryRate);%0A    result = 31 * result + (int) (temp %5E (temp %3E%3E%3E 32));%0A
     

Remove recovery rate from SingleNameIdentifiable