简体   繁体   English

接口的实现必须实现其内部类生成器接口。 怎么做?

[英]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.

相关问题 处理事件:实现接口或使用内部类来处理接口。哪个更好 - Handle an event: implement an interface or using inner class to handle the interface. Which is better Java 中的抽象类不需要从其实现接口实现任何方法。 为什么? - An abstract class in Java need not implement any methods from its implementing interface. Why? 如何使用内部类正确实现Java接口? - How do I properly implement my Java interface with inner class? 抽象类实现接口和聚合同一接口的多个实现。 它有一个模式(名称)吗? - Abstract class implementing interface and aggregating mutliple implementations of same interface. Is there a pattern (name) for it? 如何为接口的所有实现实现compareTo? - How to implement compareTo for all implementations of an interface? javax.json对象具有相同的方法,但它们不是通用接口的实现。 如何施展? - javax.json objects have same methods, but they're not implementations of a common interface. How to cast? 接口中的内部类必须由“实现者”实现 - Inner class in interface that must be implemented by the “implementor” 如何实例化内部类并实现附加接口 - How can you instantiate an inner class AND implement an additional interface 抽象类或接口。 哪种方式是正确的? - Abstract class or interface. Which way is correct? 如何将实例传递给必须实现接口的类,然后在Java中使用它? - how to pass a instance to a class that must implement an interface and then use it in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM