Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CcpId to represent CCPs #2013

Merged
merged 1 commit into from Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,101 @@
/*
* Copyright (C) 2019 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.product.common;

import java.io.Serializable;

import org.joda.convert.FromString;
import org.joda.convert.ToString;

import com.opengamma.strata.collect.ArgChecker;
import com.opengamma.strata.collect.named.Named;

/**
* An identifier for a Central Counterparty Clearing House (CCP).
* <p>
* Identifiers for common CCPs are provided in {@link CcpIds}.
*/
public final class CcpId implements Named, Serializable {

/** Serialization version. */
private static final long serialVersionUID = 1L;

/**
* The code identifying the CCP.
*/
private final String name;

//-------------------------------------------------------------------------
/**
* Obtains an identifier for the CCP.
*
* @param name the code identifying the CCP
* @return an identifier for the CCP
*/
@FromString
public static CcpId of(String name) {
return new CcpId(name);
}

// restricted constructor
private CcpId(String name) {
this.name = ArgChecker.notBlank(name, "name");
}

// resolve after deserialization
private Object readResolve() {
return of(name);
}

//-------------------------------------------------------------------------
/**
* Returns the code identifying the CCP.
*
* @return the code identifying the CCP
*/
@Override
public String getName() {
return name;
}

//-------------------------------------------------------------------------
/**
* Checks if this identifier equals another identifier.
* <p>
* The comparison checks the name.
*
* @param obj the other identifier, null returns false
* @return true if equal
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
CcpId that = (CcpId) obj;
return name.equals(that.name);
}

/**
* Returns a suitable hash code for the identifier.
*
* @return the hash code
*/
@Override
public int hashCode() {
return name.hashCode();
}

@ToString
@Override
public String toString() {
return name;
}

}
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2019 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.product.common;

/**
* Identifiers for common CCPs.
*/
public final class CcpIds {
// alphabetical

/** Australian Securities Exchange. */
public static final CcpId ASX = CcpId.of("ASX");

/** Bursa Malaysia Derivatives. */
public static final CcpId BMD = CcpId.of("BMD");

/** Canadian Derivatives Clearing Corporation. */
public static final CcpId CDCC = CcpId.of("CDCC");

/** Chicago Mercantile Exchange. */
public static final CcpId CME = CcpId.of("CME");

/** European Commodity Clearing. */
public static final CcpId ECC = CcpId.of("ECC");

/** Eurex. */
public static final CcpId EUREX = CcpId.of("EUREX");

/** Hong Kong Exchange. */
public static final CcpId HKEX = CcpId.of("HKEX");

/** Intercontinental Exchange (EU). */
public static final CcpId ICE_EU = CcpId.of("ICE-EU");

/** Intercontinental Exchange (US). */
public static final CcpId ICE_US = CcpId.of("ICE-US");

/** Japan Securities Clearing Corporation. */
public static final CcpId JSCC = CcpId.of("JSCC");

/** Japan Commodity Clearing House. */
public static final CcpId JCCH = CcpId.of("JCCH");

/** London Clearing House. */
public static final CcpId LCH = CcpId.of("LCH");

/** London Metal Exchange Clear. */
public static final CcpId LME = CcpId.of("LME");

/** Minneapolis Grain Exchange. */
public static final CcpId MGEX = CcpId.of("MGEX");

/** Options Clearing Corporation. */
public static final CcpId OCC = CcpId.of("OCC");

/** Singapore Exchange. */
public static final CcpId SGX = CcpId.of("SGX");

/** Tokyo Financial Exchange. */
public static final CcpId TFX = CcpId.of("TFX");

//-------------------------------------------------------------------------
/**
* Restricted constructor.
*/
private CcpIds() {
}

}
Expand Up @@ -28,10 +28,29 @@ public final class ExchangeId implements Named, Serializable {
*/
private final String name;

//-------------------------------------------------------------------------
/**
* Returns an identifier for the exchange.
*
* @param name the Market Identifier Code (MIC) identifying the exchange
* @return an identifier for the exchange
*/
@FromString
public static ExchangeId of(String name) {
return new ExchangeId(name);
}

// restricted constructor
private ExchangeId(String name) {
this.name = ArgChecker.notBlank(name, "name");
}

// resolve after deserialization
private Object readResolve() {
return of(name);
}

//-------------------------------------------------------------------------
/**
* Returns the Market Identifier Code (MIC) identifying the exchange.
*
Expand All @@ -42,17 +61,6 @@ public String getName() {
return name;
}

/**
* Returns an identifier for an exchange.
*
* @param name the Market Identifier Code (MIC) identifying the exchange
* @return an identifier for an exchange
*/
@FromString
public static ExchangeId of(String name) {
return new ExchangeId(name);
}

//-------------------------------------------------------------------------
/**
* Checks if this identifier equals another identifier.
Expand Down
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2019 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.product.common;

import static com.opengamma.strata.collect.TestHelper.assertSerialization;
import static com.opengamma.strata.collect.TestHelper.coverPrivateConstructor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

import org.testng.annotations.Test;

/**
* Test {@link CcpId}.
*/
@Test
public class CcpIdTest {

public void test_of() {
CcpId test = CcpId.of("GB");
assertThat(test.getName()).isEqualTo("GB");
assertThat(test).hasToString("GB");
assertThatIllegalArgumentException().isThrownBy(() -> CcpId.of(""));
}

//-------------------------------------------------------------------------
public void test_equalsHashCode() {
CcpId a = CcpId.of("LCH");
CcpId a2 = CcpIds.LCH;
CcpId b = CcpId.of("CME");
assertThat(a)
.isEqualTo(a)
.isEqualTo(a2)
.isNotEqualTo(b)
.isNotEqualTo("")
.isNotEqualTo(null)
.hasSameHashCodeAs(a2);
}

//-------------------------------------------------------------------------
public void coverage() {
coverPrivateConstructor(CcpIds.class);
}

public void test_serialization() {
CcpId test = CcpId.of("LCH");
assertSerialization(test);
}

}
Expand Up @@ -7,7 +7,8 @@

import static com.opengamma.strata.collect.TestHelper.assertSerialization;
import static com.opengamma.strata.collect.TestHelper.coverPrivateConstructor;
import static org.testng.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

import org.testng.annotations.Test;

Expand All @@ -17,25 +18,25 @@
@Test
public class ExchangeIdTest {

private static final Object ANOTHER_TYPE = "";

public void test_of() {
ExchangeId test = ExchangeId.of("GB");
assertEquals(test.getName(), "GB");
assertEquals(test.toString(), "GB");
assertThat(test.getName()).isEqualTo("GB");
assertThat(test).hasToString("GB");
assertThatIllegalArgumentException().isThrownBy(() -> CcpId.of(""));
}

//-------------------------------------------------------------------------
public void test_equalsHashCode() {
ExchangeId a = ExchangeId.of("ECAG");
ExchangeId a2 = ExchangeId.of("ECAG");
ExchangeId a2 = ExchangeIds.ECAG;
ExchangeId b = ExchangeId.of("XLON");
assertEquals(a.hashCode(), a2.hashCode());
assertEquals(a.equals(a), true);
assertEquals(a.equals(a2), true);
assertEquals(a.equals(b), false);
assertEquals(a.equals(null), false);
assertEquals(a.equals(ANOTHER_TYPE), false);
assertThat(a)
.isEqualTo(a)
.isEqualTo(a2)
.isNotEqualTo(b)
.isNotEqualTo("")
.isNotEqualTo(null)
.hasSameHashCodeAs(a2);
}

//-------------------------------------------------------------------------
Expand Down