下面列出了怎么用org.apache.commons.lang3.math.Fraction的API类实例代码及写法,或者点击链接到github查看源代码。
private PeriodicMarkerEventSource(String category, Reference reference, double period, long rollover, boolean foreground, RGBA color1, @Nullable RGBA color2) {
if (period <= 0) {
throw new IllegalArgumentException("period cannot be less than or equal to zero"); //$NON-NLS-1$
}
if (rollover < 0) {
throw new IllegalArgumentException("rollover cannot be less than zero"); //$NON-NLS-1$
}
fCategory = category;
fReference = reference;
fPeriod = period;
fPeriodInteger = (long) period;
try {
fPeriodFraction = Fraction.getFraction(fPeriod - fPeriodInteger);
} catch (ArithmeticException e) {
/* can't convert to fraction, use floating-point arithmetic */
fPeriodFraction = null;
}
fRollover = rollover;
fColor1 = color1;
fColor2 = color2;
fForeground = foreground;
}
private Reference adjustReference(Reference baseReference, long time) {
long offsetIndex = (long) ((time - baseReference.time) / fPeriod);
long offsetTime = 0;
Fraction fraction = fPeriodFraction;
if (fraction != null) {
/*
* If period = int num/den, find an offset index that is an exact
* multiple of den and calculate index * period = (index * int) +
* (index / den * num), all exact calculations.
*/
offsetIndex = offsetIndex - offsetIndex % fraction.getDenominator();
offsetTime = offsetIndex * fPeriodInteger + offsetIndex / fraction.getDenominator() * fraction.getNumerator();
} else {
/*
* Couldn't compute fractional part as fraction, use simple
* multiplication but with possible rounding error.
*/
offsetTime = Math.round(offsetIndex * fPeriod);
}
Reference reference = new Reference(baseReference.time + offsetTime, baseReference.index + offsetIndex);
return reference;
}
@Override
public void run()
{
progressMultiplier = Fraction.getFraction(1, tasks.size());
while (!tasks.isEmpty())
{
currentTask = tasks.poll();
setValues(currentTask.getMessage(), baseProgress.getNumerator(), baseProgress.getDenominator());
currentTask.addPCGenTaskListener(this);
currentTask.run();
currentTask.removePCGenTaskListener(this);
baseProgress = baseProgress.add(progressMultiplier);
}
}
@Override
public void progressChanged(PCGenTaskEvent event)
{
if (currentTask.getMaximum() == 0)
{
return;
}
Fraction progress = Fraction.getFraction(currentTask.getProgress(), currentTask.getMaximum());
progress = progress.multiplyBy(progressMultiplier);
progress = baseProgress.add(progress);
setValues(currentTask.getMessage(), progress.getNumerator(), progress.getDenominator());
}
@Override
public void run()
{
progressMultiplier = Fraction.getFraction(1, tasks.size());
while (!tasks.isEmpty())
{
currentTask = tasks.poll();
setValues(currentTask.getMessage(), baseProgress.getNumerator(), baseProgress.getDenominator());
currentTask.addPCGenTaskListener(this);
currentTask.run();
currentTask.removePCGenTaskListener(this);
baseProgress = baseProgress.add(progressMultiplier);
}
}
@Override
public void progressChanged(PCGenTaskEvent event)
{
if (currentTask.getMaximum() == 0)
{
return;
}
Fraction progress = Fraction.getFraction(currentTask.getProgress(), currentTask.getMaximum());
progress = progress.multiplyBy(progressMultiplier);
progress = baseProgress.add(progress);
setValues(currentTask.getMessage(), progress.getNumerator(), progress.getDenominator());
}
@Test
public void givenFractionClass_whenCalledgetFraction_thenCorrect() {
assertThat(Fraction.getFraction(5, 6)).isInstanceOf(Fraction.class);
}
@Test
public void givenTwoFractionInstances_whenCalledadd_thenCorrect() {
Fraction fraction1 = Fraction.getFraction(1, 4);
Fraction fraction2 = Fraction.getFraction(3, 4);
assertThat(fraction1.add(fraction2).toString()).isEqualTo("1/1");
}
@Test
public void givenTwoFractionInstances_whenCalledsubstract_thenCorrect() {
Fraction fraction1 = Fraction.getFraction(3, 4);
Fraction fraction2 = Fraction.getFraction(1, 4);
assertThat(fraction1.subtract(fraction2).toString()).isEqualTo("1/2");
}
@Test
public void givenTwoFractionInstances_whenCalledmultiply_thenCorrect() {
Fraction fraction1 = Fraction.getFraction(3, 4);
Fraction fraction2 = Fraction.getFraction(1, 4);
assertThat(fraction1.multiplyBy(fraction2).toString()).isEqualTo("3/16");
}
@Override
public Calculator<?, ExprType> newCalculator(final SenderHolder holder) {
final Calculator<Fraction, ExprType> calculator = FractionCalculatorFactory.createDefault();
calculator.environment.setGlobalSymbol("_x", () -> Fraction.getFraction(holder.getX(), 1));
calculator.environment.setGlobalSymbol("_y", () -> Fraction.getFraction(holder.getY(), 1));
calculator.environment.setGlobalSymbol("_z", () -> Fraction.getFraction(holder.getZ(), 1));
return calculator;
}