@Override public YieldCurveBundle buildObject( final FudgeDeserializer deserializer, final FudgeMsg message) { // note name in curve & name in bundle may be different! final List<FudgeField> curveFields = message.getAllByName(CURVES_FIELD_NAME); final List<FudgeField> curveNameFields = message.getAllByName(CURVES_NAME_FIELD_NAME); final List<FudgeField> ccyFields = message.getAllByName(CURRENCY_FIELD_NAME); final List<FudgeField> ccyCurveFields = message.getAllByName(CURRENCY_CURVE_FIELD_NAME); final Map<String, YieldAndDiscountCurve> curves = new LinkedHashMap<>(curveFields.size()); final Map<String, Currency> curveCurrencys = Maps.newHashMapWithExpectedSize(ccyFields.size()); final FXMatrix fxMatrix = deserializer.fieldValueToObject(FXMatrix.class, message.getByName(FX_MATRIX_FIELD_NAME)); for (int i = 0; i < curveFields.size(); i++) { final YieldAndDiscountCurve curve = deserializer.fieldValueToObject(YieldAndDiscountCurve.class, curveFields.get(i)); final String name = deserializer.fieldValueToObject(String.class, curveNameFields.get(i)); curves.put(name, curve); } for (int i = 0; i < ccyFields.size(); i++) { final String name = deserializer.fieldValueToObject(String.class, ccyCurveFields.get(i)); final Currency ccy = deserializer.fieldValueToObject(Currency.class, ccyFields.get(i)); curveCurrencys.put(name, ccy); } return new YieldCurveBundle(fxMatrix, curveCurrencys, curves); }
@Override public CurveBuildingBlockBundle buildObject( final FudgeDeserializer deserializer, final FudgeMsg message) { final List<FudgeField> curveNames = message.getAllByName(CURVE_NAME_FIELD); final List<FudgeField> blocks = message.getAllByName(BLOCK_FIELD); final List<FudgeField> matrices = message.getAllByName(MATRIX_FIELD); final int n = curveNames.size(); if (blocks.size() != n) { throw new IllegalStateException( "Should have one block for each curve name; have " + curveNames + " and " + blocks); } if (matrices.size() != n) { throw new IllegalStateException( "Should have one matrix for each curve name; have " + curveNames + " and " + matrices); } final LinkedHashMap<String, Pair<CurveBuildingBlock, DoubleMatrix2D>> data = new LinkedHashMap<>(); for (int i = 0; i < n; i++) { final String curveName = (String) curveNames.get(i).getValue(); final CurveBuildingBlock block = deserializer.fieldValueToObject(CurveBuildingBlock.class, blocks.get(i)); final DoubleMatrix2D m = new DoubleMatrix2D(deserializer.fieldValueToObject(double[][].class, matrices.get(i))); data.put(curveName, Pair.of(block, m)); } return new CurveBuildingBlockBundle(data); }
public static Entry fromFudgeMsg(final FudgeDeserializer context, final FudgeMsg msg) { final Entry e = new Entry(); final Iterator<FudgeField> keys = msg.getAllByOrdinal(1).iterator(); final Iterator<FudgeField> values = msg.getAllByOrdinal(2).iterator(); while (keys.hasNext() && values.hasNext()) { final FudgeField key = keys.next(); final FudgeField value = values.next(); e.add( context.fieldValueToObject(ExternalIdBundle.class, key), context.fieldValueToObject(HistoricalTimeSeries.class, value)); } return e; }
@Override public StandardSmileSurfaceDataBundle buildObject( final FudgeDeserializer deserializer, final FudgeMsg message) { final ForwardCurve forwardCurve = deserializer.fieldValueToObject( ForwardCurve.class, message.getByName(FORWARD_CURVE_FIELD_NAME)); final double[] expiries = deserializer.fieldValueToObject(double[].class, message.getByName(EXPIRIES_FIELD_NAME)); final double[][] strikes = deserializer.fieldValueToObject(double[][].class, message.getByName(STRIKES_FIELD_NAME)); final double[][] vols = deserializer.fieldValueToObject(double[][].class, message.getByName(VOLS_FIELD_NAME)); final boolean isCallData = message.getBoolean(IS_CALL_FIELD_NAME); return new StandardSmileSurfaceDataBundle(forwardCurve, expiries, strikes, vols, isCallData); }
@Override public DoubleMatrix2D buildObject( final FudgeDeserializer deserializer, final FudgeMsg message) { final double[][] data = deserializer.fieldValueToObject(double[][].class, message.getByName(DATA_FIELD_NAME)); return new DoubleMatrix2D(data); }
protected Map<ValueSpecification, Collection<ValueSpecification>> decodeMarketDataAliases( final FudgeDeserializer deserializer, final FudgeMsg msg) { final FudgeMsg msgRequirements = msg.getMessage(MARKET_DATA_REQUIREMENTS_FIELD); final FudgeMsg msgAliases = msg.getMessage(MARKET_DATA_ALIASES_FIELD); if ((msgRequirements == null) || (msgAliases == null) || msgRequirements.isEmpty()) { return Collections.emptyMap(); } final Map<ValueSpecification, Collection<ValueSpecification>> result = Maps.newHashMapWithExpectedSize(msgRequirements.getNumFields()); final Iterator<FudgeField> itrRequirements = msgRequirements.iterator(); final Iterator<FudgeField> itrAliases = msgAliases.iterator(); while (itrRequirements.hasNext() && itrAliases.hasNext()) { final FudgeField requirement = itrRequirements.next(); final FudgeField alias = itrAliases.next(); final ValueSpecification spec = deserializer.fieldValueToObject(ValueSpecification.class, requirement); if (alias.getValue() == IndicatorType.INSTANCE) { result.put(spec, Collections.singleton(spec)); } else { final FudgeMsg msgAlias = (FudgeMsg) alias.getValue(); final String clazz = msgAlias.getString(0); if ("list".equals(clazz)) { final Collection<ValueSpecification> aliases = new ArrayList<ValueSpecification>(msgAlias.getNumFields() - 1); for (FudgeField aliasField : msgAlias) { if (aliasField.getValue() == IndicatorType.INSTANCE) { aliases.add(spec); } else if (aliasField.getValue() instanceof FudgeMsg) { aliases.add(deserializer.fieldValueToObject(ValueSpecification.class, aliasField)); } } result.put(spec, aliases); } else { result.put( spec, Collections.singleton( deserializer.fieldValueToObject(ValueSpecification.class, alias))); } } } return result; }
@SuppressWarnings("synthetic-access") @Override public CurrencyLabelledMatrix1D buildObject( final FudgeDeserializer deserializer, final FudgeMsg message) { final FudgeMsg msg = message.getMessage(MATRIX_FIELD_NAME); final Queue<String> labelTypes = new LinkedList<String>(); final Queue<FudgeField> labelValues = new LinkedList<FudgeField>(); final List<Currency> keys = new LinkedList<Currency>(); final List<Object> labels = new LinkedList<Object>(); final List<Double> values = new LinkedList<Double>(); for (final FudgeField field : msg) { switch (field.getOrdinal()) { case LABEL_TYPE_ORDINAL: labelTypes.add((String) field.getValue()); break; case KEY_ORDINAL: keys.add(Currency.of((String) field.getValue())); break; case LABEL_ORDINAL: labelValues.add(field); break; case VALUE_ORDINAL: values.add((Double) field.getValue()); break; } if (!labelTypes.isEmpty() && !labelValues.isEmpty()) { // Have a type and a value, which can be consumed final String labelType = labelTypes.remove(); Class<?> labelClass; try { labelClass = LabelledMatrix1DBuilder.getLabelClass(labelType, _loadedClasses); } catch (final ClassNotFoundException ex) { throw new OpenGammaRuntimeException( "Could not deserialize label of type " + labelType, ex); } final FudgeField labelValue = labelValues.remove(); final Object label = deserializer.fieldValueToObject(labelClass, labelValue); // labels.add(Currency.of((String) label)); labels.add(label); } } final int matrixSize = keys.size(); final Currency[] keysArray = new Currency[matrixSize]; keys.toArray(keysArray); final Object[] labelsArray = new Object[matrixSize]; labels.toArray(labelsArray); final double[] valuesArray = Doubles.toArray(values); return new CurrencyLabelledMatrix1D(keysArray, labelsArray, valuesArray); }
@Override public Position buildObject(final FudgeDeserializer deserializer, final FudgeMsg message) { final SimplePosition position = buildObjectImpl(deserializer, message); final FudgeField parentField = message.getByName(FIELD_PARENT); final UniqueId parentId = (parentField != null) ? deserializer.fieldValueToObject(UniqueId.class, parentField) : null; if (parentId != null) { position.setParentNodeId(parentId); } return position; }
@Override public SimpleHistoricalTimeSeries buildObject(FudgeDeserializer deserializer, FudgeMsg message) { UniqueId uniqueId = null; LocalDateDoubleTimeSeries timeseries = null; if (message.getByName(UNIQUE_ID_FIELD_NAME) != null) { uniqueId = deserializer.fieldValueToObject(UniqueId.class, message.getByName(UNIQUE_ID_FIELD_NAME)); } if (message.hasField(TIMESERIES_FIELD_NAME)) { Object fieldValue = deserializer.fieldValueToObject(message.getByName(TIMESERIES_FIELD_NAME)); if (fieldValue instanceof LocalDateDoubleTimeSeries) { timeseries = (LocalDateDoubleTimeSeries) fieldValue; } } if (uniqueId != null && timeseries != null) { return new SimpleHistoricalTimeSeries(uniqueId, timeseries); } else { throw new OpenGammaRuntimeException( "Cannot deserialize " + message + " to SimpleHistoricalTimeSeries"); } }
// ------------------------------------------------------------------------- @SuppressWarnings("unchecked") @Override public T buildObject(FudgeDeserializer deserializer, FudgeMsg outerMsg) { FudgeField fudgeField = outerMsg.getByName("inner"); FudgeMsg msg = (FudgeMsg) fudgeField.getValue(); final FudgeField classNameField = msg.getByOrdinal(FudgeSerializer.TYPES_HEADER_ORDINAL); final String className = (String) classNameField.getValue(); try { final List<Object> parameters = newArrayList(); parameters.add(null); // the omitted enclosing object for (FudgeField parameterField : msg.getAllByOrdinal(1)) { parameters.add(deserializer.fieldValueToObject(parameterField)); } return (T) AccessController.doPrivileged( new PrivilegedAction<Object>() { @Override public Object run() { try { final Class<?> innerClass = Class.forName(className); final Constructor<?>[] ctors = innerClass.getDeclaredConstructors(); // We require that inner classes got only one ctor (anonymous inner classes will // do) // in order to avoid disambiguity if (ctors.length == 1) { final Constructor<?> ctor = ctors[0]; ctor.setAccessible(true); // solution Object inner = ctor.newInstance(parameters.toArray()); return new AutoFudgable<Object>(inner); } } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } return null; } }); } catch (RuntimeException ex) { throw new FudgeRuntimeException("Unable to deserialize: " + className, ex); } }
protected static SimplePosition buildObjectImpl( final FudgeDeserializer deserializer, final FudgeMsg message) { SimpleSecurityLink secLink = new SimpleSecurityLink(); if (message.hasField(FIELD_SECURITYKEY)) { FudgeField secKeyField = message.getByName(FIELD_SECURITYKEY); if (secKeyField != null) { secLink.setExternalId(deserializer.fieldValueToObject(ExternalIdBundle.class, secKeyField)); } } if (message.hasField(FIELD_SECURITYID)) { FudgeField secIdField = message.getByName(FIELD_SECURITYID); if (secIdField != null) { secLink.setObjectId(deserializer.fieldValueToObject(ObjectId.class, secIdField)); } } SimplePosition position = new SimplePosition(); position.setSecurityLink(secLink); if (message.hasField(FIELD_UNIQUE_ID)) { FudgeField uniqueIdField = message.getByName(FIELD_UNIQUE_ID); if (uniqueIdField != null) { position.setUniqueId(deserializer.fieldValueToObject(UniqueId.class, uniqueIdField)); } } if (message.hasField(FIELD_QUANTITY)) { FudgeField quantityField = message.getByName(FIELD_QUANTITY); if (quantityField != null) { position.setQuantity(message.getFieldValue(BigDecimal.class, quantityField)); } } readTrades( deserializer, message.getFieldValue(FudgeMsg.class, message.getByName(FIELD_TRADES)), position); return position; }
protected Set<ComputationTargetSpecification> decodeComputationTargets( final FudgeDeserializer deserializer, final FudgeMsg msg) { final FudgeMsg submsg = msg.getMessage(COMPUTATION_TARGETS_FIELD); if (submsg == null) { return Collections.emptySet(); } final Set<ComputationTargetSpecification> result = Sets.newHashSetWithExpectedSize(submsg.getNumFields()); for (final FudgeField field : submsg) { result.add( deserializer .fieldValueToObject(ComputationTargetReference.class, field) .getSpecification()); } return result; }
@Override public LocalDateLabelledMatrix1D buildObject( final FudgeDeserializer deserializer, final FudgeMsg message) { final FudgeMsg msg = message.getMessage(MATRIX_FIELD_NAME); final Queue<String> labelTypes = new LinkedList<String>(); final Queue<FudgeField> labelValues = new LinkedList<FudgeField>(); final List<LocalDate> keys = new LinkedList<LocalDate>(); final List<Object> labels = new LinkedList<Object>(); final List<Double> values = new LinkedList<Double>(); for (final FudgeField field : msg) { switch (field.getOrdinal()) { case LABEL_TYPE_ORDINAL: labelTypes.add((String) field.getValue()); break; case KEY_ORDINAL: keys.add(((FudgeDate) field.getValue()).toLocalDate()); break; case LABEL_ORDINAL: labelValues.add(field); break; case VALUE_ORDINAL: values.add((Double) field.getValue()); break; } if (!labelTypes.isEmpty() && !labelValues.isEmpty()) { // Have a type and a value, which can be consumed final String labelType = labelTypes.remove(); Class<?> labelClass = getClass(labelType); final FudgeField labelValue = labelValues.remove(); final Object label = deserializer.fieldValueToObject(labelClass, labelValue); labels.add(label); } } final int matrixSize = keys.size(); final LocalDate[] keysArray = new LocalDate[matrixSize]; keys.toArray(keysArray); final Object[] labelsArray = new Object[matrixSize]; labels.toArray(labelsArray); final double[] valuesArray = Doubles.toArray(values); return new LocalDateLabelledMatrix1D(keysArray, labelsArray, valuesArray); }
@Override public ViewCalculationResultModel buildObject(FudgeDeserializer deserializer, FudgeMsg message) { final Map<ComputationTargetSpecification, Map<Pair<String, ValueProperties>, ComputedValue>> mapNames = new HashMap< ComputationTargetSpecification, Map<Pair<String, ValueProperties>, ComputedValue>>(); for (FudgeField field : message) { final ComputedValue value = deserializer.fieldValueToObject(ComputedValue.class, field); final ComputationTargetSpecification target = value.getSpecification().getTargetSpecification(); if (!mapNames.containsKey(target)) { mapNames.put(target, new HashMap<Pair<String, ValueProperties>, ComputedValue>()); } mapNames .get(target) .put( Pair.of( value.getSpecification().getValueName(), value.getSpecification().getProperties()), value); } return new ViewCalculationResultModel() { @Override public Collection<ComputationTargetSpecification> getAllTargets() { return mapNames.keySet(); } @Override public Map<Pair<String, ValueProperties>, ComputedValue> getValues( ComputationTargetSpecification target) { return mapNames.get(target); } @Override public Collection<ComputedValue> getAllValues(ComputationTargetSpecification target) { Map<Pair<String, ValueProperties>, ComputedValue> targetValuesMap = mapNames.get(target); return targetValuesMap != null ? Collections.unmodifiableCollection(targetValuesMap.values()) : null; } }; }
@Override public MulticurveProviderDiscount buildObject( final FudgeDeserializer deserializer, final FudgeMsg message) { final Map<Currency, YieldAndDiscountCurve> discountingCurves = new LinkedHashMap<>(); final List<FudgeField> currencyFields = message.getAllByName(CURRENCY_FIELD); final List<FudgeField> discountingCurveFields = message.getAllByName(DISCOUNTING_CURVE_FIELD); for (int i = 0; i < currencyFields.size(); i++) { final Currency currency = Currency.of((String) currencyFields.get(i).getValue()); final YieldAndDiscountCurve curve = deserializer.fudgeMsgToObject( YieldAndDiscountCurve.class, (FudgeMsg) discountingCurveFields.get(i).getValue()); discountingCurves.put(currency, curve); } final Map<IborIndex, YieldAndDiscountCurve> forwardIborCurves = new LinkedHashMap<>(); final List<FudgeField> indexIborFields = message.getAllByName(INDEX_IBOR_FIELD); final List<FudgeField> forwardIborCurveFields = message.getAllByName(INDEX_IBOR_CURVE); for (int i = 0; i < currencyFields.size(); i++) { final IborIndex index = deserializer.fudgeMsgToObject( IborIndex.class, (FudgeMsg) indexIborFields.get(i).getValue()); final YieldAndDiscountCurve curve = deserializer.fudgeMsgToObject( YieldAndDiscountCurve.class, (FudgeMsg) forwardIborCurveFields.get(i).getValue()); forwardIborCurves.put(index, curve); } final Map<IndexON, YieldAndDiscountCurve> forwardONCurves = new LinkedHashMap<>(); final List<FudgeField> indexONFields = message.getAllByName(INDEX_ON_FIELD); final List<FudgeField> forwardONCurveFields = message.getAllByName(OVERNIGHT_CURVE_FIELD); for (int i = 0; i < currencyFields.size(); i++) { final IndexON index = deserializer.fudgeMsgToObject( IndexON.class, (FudgeMsg) indexONFields.get(i).getValue()); final YieldAndDiscountCurve curve = deserializer.fudgeMsgToObject( YieldAndDiscountCurve.class, (FudgeMsg) forwardONCurveFields.get(i).getValue()); forwardONCurves.put(index, curve); } final FXMatrix fxMatrix = deserializer.fieldValueToObject(FXMatrix.class, message.getByName(FX_MATRIX_FIELD)); return new MulticurveProviderDiscount( discountingCurves, forwardIborCurves, forwardONCurves, fxMatrix); }
@Override public FXMatrix buildObject(final FudgeDeserializer deserializer, final FudgeMsg message) { final List<FudgeField> currencies = message.getAllByName(CURRENCY_FIELD); final List<FudgeField> orders = message.getAllByName(ORDER_FIELD); final Map<Currency, Integer> map = new HashMap<>(); for (int i = 0; i < currencies.size(); i++) { final Currency currency = Currency.of((String) currencies.get(i).getValue()); final Integer order = ((Number) orders.get(i).getValue()).intValue(); map.put(currency, order); } final List<FudgeField> entries = message.getAllByName(ENTRIES_FIELD); final List<FudgeField> arrays = message.getAllByName(FX_RATES_FIELD); final double[][] fxRates = new double[entries.size()][]; for (int i = 0; i < entries.size(); i++) { final FudgeMsg msg = (FudgeMsg) arrays.get(i).getValue(); final double[] row = deserializer.fieldValueToObject(double[].class, msg.getByName(ROW_FIELD)); fxRates[i] = row; } return new FXMatrix(map, fxRates); }
@Override public InflationProviderDiscount buildObject( final FudgeDeserializer deserializer, final FudgeMsg message) { final MulticurveProviderDiscount yieldCurves = deserializer.fieldValueToObject( MulticurveProviderDiscount.class, message.getByName(YIELD_CURVES_FIELD)); final List<FudgeField> indexFields = message.getAllByName(PRICE_INDEX_FIELD); final List<FudgeField> indexCurveFields = message.getAllByName(PRICE_INDEX_CURVE_FIELD); final Map<IndexPrice, PriceIndexCurve> priceIndexCurves = new LinkedHashMap<>(); final int n = indexFields.size(); for (int i = 0; i < n; i++) { final IndexPrice index = deserializer.fudgeMsgToObject( IndexPrice.class, (FudgeMsg) indexFields.get(i).getValue()); final PriceIndexCurve curve = deserializer.fudgeMsgToObject( PriceIndexCurve.class, (FudgeMsg) indexCurveFields.get(i).getValue()); priceIndexCurves.put(index, curve); } return new InflationProviderDiscount(yieldCurves, priceIndexCurves); }
@Override public CalculationJobItem buildObject(FudgeDeserializer deserializer, FudgeMsg message) { ComputationTargetSpecification computationTargetSpecification = deserializer.fudgeMsgToObject(ComputationTargetSpecification.class, message); Validate.notNull( computationTargetSpecification, "Fudge message is not a CalculationJobItem - field 'computationTargetSpecification' is not present"); String functionUniqueId = message.getString(FUNCTION_UNIQUE_ID_FIELD_NAME); Validate.notNull( functionUniqueId, "Fudge message is not a CalculationJobItem - field 'functionUniqueIdentifier' is not present"); FudgeField fudgeField = message.getByName(FUNCTION_PARAMETERS_FIELD_NAME); Validate.notNull( fudgeField, "Fudge message is not a CalculationJobItem - field 'functionParameters' is not present"); FunctionParameters functionParameters = deserializer.fieldValueToObject(FunctionParameters.class, fudgeField); final long[] inputIdentifiers = (long[]) message.getByName(INPUT_FIELD_NAME).getValue(); List<ValueRequirement> desiredValues = new ArrayList<ValueRequirement>(); for (FudgeField field : message.getAllByName(DESIRED_VALUE_FIELD_NAME)) { FudgeMsg valueMsg = (FudgeMsg) field.getValue(); ValueRequirement desiredValue = deserializer.fudgeMsgToObject(ValueRequirement.class, valueMsg); desiredValues.add(desiredValue); } return CalculationJobItem.create( functionUniqueId, functionParameters, computationTargetSpecification, inputIdentifiers, desiredValues); }
@SuppressWarnings("unchecked") protected Map<ValueSpecification, Set<ValueRequirement>> decodeTerminalOutputSpecifications( final FudgeDeserializer deserializer, final FudgeMsg msg) { final FudgeMsg submsg = msg.getMessage(TERMINAL_OUTPUT_SPECIFICATIONS_FIELD); if (submsg == null) { return Collections.emptyMap(); } final Map<ValueSpecification, Set<ValueRequirement>> result = Maps.newHashMapWithExpectedSize(submsg.getNumFields() / 2); LinkedList<Object> overflow = null; ValueSpecification key = null; Set<ValueRequirement> value = null; for (final FudgeField field : submsg) { if (MAP_KEY.equals(field.getOrdinal())) { final ValueSpecification fieldValue = deserializer.fieldValueToObject(ValueSpecification.class, field); if (key == null) { if (value == null) { key = fieldValue; } else { result.put(fieldValue, value); if (overflow != null) { value = overflow.isEmpty() ? null : (Set<ValueRequirement>) overflow.removeFirst(); } else { value = null; } } } else { if (overflow == null) { overflow = new LinkedList<Object>(); } overflow.add(fieldValue); } } else if (MAP_VALUE.equals(field.getOrdinal())) { final FudgeMsg submsg2 = (FudgeMsg) field.getValue(); final Set<ValueRequirement> fieldValue = Sets.newHashSetWithExpectedSize(submsg2.getNumFields()); for (final FudgeField field2 : submsg2) { fieldValue.add(deserializer.fieldValueToObject(ValueRequirement.class, field2)); } if (value == null) { if (key == null) { value = fieldValue; } else { result.put(key, fieldValue); if (overflow != null) { key = overflow.isEmpty() ? null : (ValueSpecification) overflow.removeFirst(); } else { key = null; } } } else { if (overflow == null) { overflow = new LinkedList<Object>(); } overflow.add(fieldValue); } } } return result; }
public static Scaling fromFudgeMsg(final FudgeDeserializer deserializer, final FudgeMsg msg) { Double scalingFactor = deserializer.fieldValueToObject(Double.class, msg.getByName(SCALING_FACTOR)); return new Scaling(scalingFactor); }