问题描述:

I've an existing JFreeChart plugin-class that won't convert to Scala v2.11, its trickier than it seems. The Java is:

package x;

import org.apache.commons.lang.NotImplementedException;

import org.jfree.data.time.RegularTimePeriod;

import java.io.Serializable;

import java.math.BigDecimal;

import java.math.MathContext;

import java.math.RoundingMode;

import java.util.Calendar;

import java.util.Comparator;

import java.util.Date;

public final class IntervalJFreeChart extends RegularTimePeriod implements Comparator, Comparable, Serializable {

final int width;

final long seqNo;

final long firstMilli;

final long lastMilli;

public IntervalJFreeChart(Date time, int w) {

MathContext mc = new MathContext(16, RoundingMode.FLOOR);

width = w;

BigDecimal wbd = new java.math.BigDecimal(""+w);

wbd.setScale(120, RoundingMode.FLOOR);

BigDecimal bd = new java.math.BigDecimal(time.getTime());

bd.setScale(120, RoundingMode.FLOOR);

seqNo = bd.divide(wbd, mc).longValue();

BigDecimal seqBd = new java.math.BigDecimal(seqNo);

seqBd.setScale(120, RoundingMode.FLOOR);

firstMilli = seqBd.multiply(wbd).longValue();

lastMilli = firstMilli + w -1;

}

@Override

public IntervalJFreeChart previous() {

return new IntervalJFreeChart(new Date(firstMilli - width), width);

}

@Override

public IntervalJFreeChart next() {

return new IntervalJFreeChart(new Date(lastMilli + width), width);

}

@Override

public long getSerialIndex() {

return this.seqNo;

}

@Override

public void peg(Calendar calendar) {

throw new NotImplementedException("peg()");

}

@Override

public long getFirstMillisecond() {

return firstMilli;

}

@Override

public long getFirstMillisecond(Calendar calendar) {

calendar.setTimeInMillis(firstMilli);

return firstMilli;

}

@Override

public long getLastMillisecond() {

return lastMilli;

}

@Override

public long getLastMillisecond(Calendar calendar) {

calendar.setTimeInMillis(lastMilli);

return lastMilli;

}

@Override

public int compareTo(Object o) {

return compare(this, o);

}

@Override

public int compare(Object o1, Object o2) {

if (o2 instanceof IntervalJFreeChart) {

final IntervalJFreeChart m1 = (IntervalJFreeChart)o1;

final IntervalJFreeChart m2 = (IntervalJFreeChart)o2;

final int diff = Long.valueOf(m1.seqNo).compareTo(m2.seqNo);

if (diff > 0) return 1;

if (diff < 0) return -1;

return 0;

}

throw new RuntimeException("MilliInterval: compare error : ");

}

}

The issue I am seeing is: abstract class RegularTimePeriod implements java's Comparable interface without specifying a type parameter for it.

The Scala implementation therefore needs both: concrete Comparator and Comparable.

Advice greatly appreciated.

网友答案:

Since the class can actually only compare to IntervalJFreeChart, just use Comparable[IntervalJFreeChart].

相关阅读:
Top