[英]Interface's implementations MUST implement its inner class builder interface. How to do it?
I have an interesting question. 我有一个有趣的问题。
I have created an interface as so; 我已经创建了一个界面。 the idea is that a class which implements this interface will have to implement the TimeRangeBuilder too
这个想法是实现该接口的类也必须实现TimeRangeBuilder
import java.util.Date;
public interface TimeRange<T> extends Comparable<T> {
public static interface TimeRangeBuilder<T> {
public TimeRangeBuilder<T> startDate(Date startDate);
public TimeRangeBuilder<T> endDate(Date endDate);
public T build();
}
public Date getStartDate();
public Date getEndDate();
}
But unfortunately this isn't how it works; 不幸的是,这不是它的工作方式。 when I implement this interface in a concrete class;
当我在具体的类中实现此接口时; the implementation of the TimeRangeBuilder is completely optional.
TimeRangeBuilder的实现是完全可选的。
To give you an idea of where I'm heading, here is my implementation class 为了让您大致了解我要去的地方,这是我的实现类
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import ru.crystals.videoutil.utils.TimeRange;
public class VidArchive implements TimeRange<VidArchive> {
/** date format used to convert Date objects to human-readable form for the toString() function */
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss");
private final byte[] bytes;
private final String cam;
private final Date startDate;
private final Date endDate;
private final String fileName;
private final String directory;
public static class Builder implements TimeRangeBuilder<VidArchive> {
private byte[] bytes;
private String cam;
private Date startDate;
private Date endDate;
private String fileName;
private String directory;
public Builder() {}
public Builder bytes(byte[] bytes) { this.bytes = bytes; return this; }
public Builder cam(String cam) { this.cam = cam; return this; }
@Override
public Builder startDate(Date startDate) { this.startDate = startDate; return this; }
@Override
public Builder endDate(Date endDate) { this.endDate = endDate; return this; }
public Builder fileName(String fileName) { this.fileName = fileName; return this; }
public Builder directory(String directory) { this.directory = directory; return this; }
@Override
public VidArchive build() { return new VidArchive(this); }
}
private VidArchive(Builder builder) {
bytes = builder.bytes;
cam = builder.cam;
startDate = builder.startDate;
endDate = builder.endDate;
fileName = builder.fileName;
directory = builder.directory;
}
public byte[] getBytes() { return bytes; }
public String getCam() { return cam; }
@Override
public Date getStartDate() { return startDate; }
@Override
public Date getEndDate() { return endDate; }
public String getFileName() { return fileName; }
public String getDirectory() { return directory; }
@Override
public int compareTo(VidArchive vidArchive) {
return startDate.compareTo(vidArchive.getStartDate());
}
@Override
public String toString() {
return DATE_FORMAT.format(startDate) + " - " + DATE_FORMAT.format(endDate);
}
}
And it all comes down to this; 一切归结于此; I have another class that should compatible not only with the VidArchive class, but all implementations of the TimeRange interface.
我还有另一个类,该类不仅应与VidArchive类兼容,而且应与TimeRange接口的所有实现兼容。 Here is the code that I want to get working
这是我要开始工作的代码
public class TimeBar<E extends TimeRange<E>> extends JProgressBar {
private TreeSet<E> timeRanges;
...
...
public void seekToTime() {
/** create a dummy object (an implementation of the TimeRange interface) with the most recent date set in TimeBarUI, so that it can be compared
* to all the other objects in the set in order to find the closest one by time*/
E archive = new E.Builder().
startDate(((TimeBarUI)ui).getMostRecentDate()).
endDate(((TimeBarUI)ui).getMostRecentDate()).
build();
TimeRange<E> closest = timeRanges.headSet(archive, true).last();
...
}
}
As you gentlemen can imagine; 各位先生可以想象; that whole
E.Builder()
thing.. yep, it doesn't work. 整个
E.Builder()
事情。是的,它不起作用。 Help me to get it to work, or find an alternative solution to my conundrum (I would be open to an alternative Builder implementation, but I would very much like to keep my Builder. 帮助我使其正常运行,或为我的难题找到替代解决方案(我愿意接受替代的Builder实施,但是我非常想保留我的Builder。
PS Any other suggestions in regards to my code would be welcome too. PS关于我的代码的任何其他建议也将受到欢迎。 I'm always eager to improve.
我一直渴望改善。
I am not sure to understand but is it what you try to achieve ? 我不确定是否了解,但这是您想要实现的目标吗?
import java.util.Date;
public interface TimeRange<T,K> extends Comparable<T> {
public K Builder();
public static interface TimeRangeBuilder<T> {
public TimeRangeBuilder<T> startDate(Date startDate);
public TimeRangeBuilder<T> endDate(Date endDate);
public T build();
}
public Date getStartDate();
public Date getEndDate();
}
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class VidArchive implements TimeRange<VidArchive, VidArchive.Builder> {
/** date format used to convert Date objects to human-readable form for the toString() function */
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss");
private final byte[] bytes;
private final String cam;
private final Date startDate;
private final Date endDate;
private final String fileName;
private final String directory;
private Builder builder;
@Override
public Builder Builder() {
return builder;
}
public static class Builder implements TimeRangeBuilder<VidArchive> {
private byte[] bytes;
private String cam;
private Date startDate;
private Date endDate;
private String fileName;
private String directory;
public Builder() {}
public Builder bytes(byte[] bytes) { this.bytes = bytes; return this; }
public Builder cam(String cam) { this.cam = cam; return this; }
@Override
public Builder startDate(Date startDate) { this.startDate = startDate; return this; }
@Override
public Builder endDate(Date endDate) { this.endDate = endDate; return this; }
public Builder fileName(String fileName) { this.fileName = fileName; return this; }
public Builder directory(String directory) { this.directory = directory; return this; }
@Override
public VidArchive build() { return new VidArchive(this); }
}
private VidArchive(Builder builder) {
this.builder = builder;
bytes = builder.bytes;
cam = builder.cam;
startDate = builder.startDate;
endDate = builder.endDate;
fileName = builder.fileName;
directory = builder.directory;
}
public byte[] getBytes() { return bytes; }
public String getCam() { return cam; }
@Override
public Date getStartDate() { return startDate; }
@Override
public Date getEndDate() { return endDate; }
public String getFileName() { return fileName; }
public String getDirectory() { return directory; }
@Override
public int compareTo(VidArchive vidArchive) {
return startDate.compareTo(vidArchive.getStartDate());
}
@Override
public String toString() {
return DATE_FORMAT.format(startDate) + " - " + DATE_FORMAT.format(endDate);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.