/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.financial.fudgemsg;

import org.fudgemsg.types.SecondaryFieldType;
import org.fudgemsg.types.StringFieldType;

import com.opengamma.financial.Currency;

/**
 * Converts Currency instances to/from a Fudge string type.
 */
public final class CurrencySecondaryType extends SecondaryFieldType<Currency, String> {

  /**
   * Singleton instance of the type.
   */
  public static final CurrencySecondaryType INSTANCE = new CurrencySecondaryType();

  private CurrencySecondaryType() {
    super(StringFieldType.INSTANCE, Currency.class);
  }

  @Override
  public String secondaryToPrimary(Currency object) {
    return object.getISOCode();
  }

  @Override
  public Currency primaryToSecondary(final String isoCode) {
    return Currency.getInstance(isoCode);
  }

}

@@ -264,16 +264,58 @@
 urrency;
+%0Aimport com.opengamma.id.UniqueIdentifier;
 %0A%0A/**%0A *
@@ -869,53 +869,600 @@
 Code
-) %7B%0A    return Currency.getInstance(isoCode);
+OrUniqueIdentifier) %7B%0A    if (isoCodeOrUniqueIdentifier.length() == 3) %7B%0A      // 3 letters means ISO code%0A      return Currency.getInstance(isoCodeOrUniqueIdentifier);%0A    %7D else %7B%0A      // Otherwise, try as a UID%0A      final UniqueIdentifier uid = UniqueIdentifier.parse(isoCodeOrUniqueIdentifier);%0A      if (Currency.IDENTIFICATION_DOMAIN.equals(uid.getScheme())) %7B%0A        return Currency.getInstance(uid.getValue());%0A      %7D else %7B%0A        throw new IllegalArgumentException(%22Not a unique identifier or currency ISO code - '%22%0A            + isoCodeOrUniqueIdentifier + %22'%22);%0A      %7D%0A    %7D
 %0A  %7D

allow a currency to be converted from a string ISO code or a string UniqueIdentifier