public void convert( InstrumentService instrumentService, InstrumentType instrumentType, InstrumentRunValue targetInstrumentRunValue, InstrumentRunValue sourceInstrumentRunValue) { InstrumentParameter sourceParameter = instrumentType.getInstrumentParameter(sourceInstrumentRunValue.getInstrumentParameter()); InstrumentParameter targetParameter = instrumentType.getInstrumentParameter(targetInstrumentRunValue.getInstrumentParameter()); log.debug( "Converting parameters from source {} to target {}", sourceParameter, targetParameter); Unit sourceUnit = Unit.valueOf(sourceParameter.getMeasurementUnit()); Unit targetUnit = Unit.valueOf(targetParameter.getMeasurementUnit()); log.debug( "Converting units from source {} to target {}", sourceUnit.toString(), targetUnit.toString()); double sourceValue; // Extract the source value and convert it to a double try { sourceValue = Double.parseDouble( sourceInstrumentRunValue.getData(sourceParameter.getDataType()).getValueAsString()); } catch (NumberFormatException e) { Data sourceData = sourceInstrumentRunValue.getData(sourceParameter.getDataType()); log.error( "Error converting between measurement units. Original value {} of type {} cannot be converted to a double, which is required to convert between measurement units.", sourceData.getValueAsString(), sourceData.getType()); throw e; } double newValue = sourceUnit.getConverterTo(targetUnit).convert(sourceValue); switch (targetParameter.getDataType()) { case DECIMAL: targetInstrumentRunValue.setData(DataBuilder.buildDecimal(newValue)); break; case INTEGER: if (targetUnit.toString().equalsIgnoreCase("year")) newValue = Math.floor(newValue); targetInstrumentRunValue.setData(DataBuilder.buildInteger(Math.round(newValue))); break; } }
public void setInstrument(BDXInstrument instrument) { if (instrument == BDXInstrument.NONE) { _type = InstrumentType.NONE; } else if (instrument.isDrums() && _type != InstrumentType.DRUMS) { throw new IllegalArgumentException( instrument.getName() + " を " + _type.toString() + " に設定することはできません"); } _instrument = instrument; }
private InstrumentType matchInstrument(String instrumentData) throws ParseException { InstrumentType matchedType; StringBuilder instrumentPatternsBuilder = new StringBuilder(); for (InstrumentType instrumentType : InstrumentType.values()) { instrumentPatternsBuilder.append( String.format("|(?<%s>%s)", instrumentType.name(), instrumentType.pattern)); } Pattern instrumentPattern = Pattern.compile(instrumentPatternsBuilder.substring(1), Pattern.CASE_INSENSITIVE); Matcher matcher = instrumentPattern.matcher(instrumentData); if (matcher.find()) { if (matcher.group(InstrumentType.HIGHHAT.name()) != null) { matchedType = InstrumentType.HIGHHAT; } else if (matcher.group(InstrumentType.BASSDRUM.name()) != null) { matchedType = InstrumentType.BASSDRUM; } else if (matcher.group(InstrumentType.CRASHCYMBAL.name()) != null) { matchedType = InstrumentType.CRASHCYMBAL; } else if (matcher.group(InstrumentType.RIDECYMBAL.name()) != null) { matchedType = InstrumentType.RIDECYMBAL; } else if (matcher.group(InstrumentType.SNAREDRUM.name()) != null) { matchedType = InstrumentType.SNAREDRUM; } else if (matcher.group(InstrumentType.HIGHTOM.name()) != null) { matchedType = InstrumentType.HIGHTOM; } else if (matcher.group(InstrumentType.LOWTOM.name()) != null) { matchedType = InstrumentType.LOWTOM; } else if (matcher.group(InstrumentType.FLOORTOM.name()) != null) { matchedType = InstrumentType.FLOORTOM; } else { throw new ParseException(instrumentData, 0); } } else { throw new ParseException(instrumentData, 0); } return matchedType; }
public String getPartName() { if (_instrument == BDXInstrument.NONE) { return _instrument.getName(); } StringBuilder sb = new StringBuilder(); sb.append(_instrument.getName()); if (_cloneNum > 0) { String st = BinaryUtil.to2ByteString(String.valueOf(_cloneNum)); sb.append(st); } switch (_type) { case GUITAR: case PIANO: sb.append("("); sb.append(_type.getTypeName()); sb.append(")"); break; default: break; } return sb.toString(); }