小编hen*_*lst的帖子

强制类字段为相同的泛型类型,而不指定类类型参数

考虑一个对象,该对象生成另一个对象使用的数据以生成结果.该过程封装在一个类中,中间数据不相关.

在下面的示例中,该过程在构造时进行,并且没有问题.构造函数上的type参数确保兼容的使用者/生产者.

public class ProduceAndConsume {
    public interface Producer<T> {
        T produce();
    }
    public interface Consumer<V> {
        void consume(V data);
    }

    public <IntermediateType> ProduceAndConsume(Producer<? extends IntermediateType> producer, Consumer<IntermediateType> consumer) {
        consumer.consume(producer.produce());
    }

    ...

}
Run Code Online (Sandbox Code Playgroud)

如果我希望存储对生产者/消费者的引用并稍后进行处理,那么代码将变为:

public class ProduceAndConsume<IntermediateType> {
    public interface Producer<T> {
        T produce();
    }
    public interface Consumer<V> {
        void consume(V data);
    }

    private Producer<? extends IntermediateType> producer;
    private Consumer<IntermediateType> consumer;

    public ProduceAndConsume(Producer<? extends IntermediateType> producer, Consumer<IntermediateType> consumer) {
        this.producer = producer;
        this.consumer = consumer;
    }

    ...

    private void …
Run Code Online (Sandbox Code Playgroud)

java generics

8
推荐指数
1
解决办法
95
查看次数

如何等待(固定利率)ScheduledFuture 在取消时完成

是否有一种内置方法可以取消Runnable已通过固定速率安排的任务ScheduledExecutorService.scheduleAtFixedRate并等待它完成(如果它在调用取消时碰巧正在运行)?

考虑以下示例:

public static void main(String[] args) throws InterruptedException, ExecutionException  {

    Runnable fiveSecondTask = new Runnable() {
        @Override
        public void run() {
            System.out.println("5 second task started");
            long finishTime = System.currentTimeMillis() + 5_000;
            while (System.currentTimeMillis() < finishTime);
            System.out.println("5 second task finished");
        }
    };

    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    ScheduledFuture<?> fut = exec.scheduleAtFixedRate(fiveSecondTask, 0, 1, TimeUnit.SECONDS);

    Thread.sleep(1_000);
    System.out.print("Cancelling task..");
    fut.cancel(true);

    System.out.println("done");
    System.out.println("isCancelled : " + fut.isCancelled());
    System.out.println("isDone      : " + fut.isDone());
    try {
        fut.get();
        System.out.println("get         : didn't throw exception"); …
Run Code Online (Sandbox Code Playgroud)

java scheduledexecutorservice

6
推荐指数
1
解决办法
3040
查看次数

@ElementCollection with Map &lt;Entity,Embeddable&gt;,其中Entity是Embeddable的字段

在搜索了JPA文档和各种帖子之后,我对于JPA2.0是否可以进行以下操作感到困惑。我刚开始使用JPA,所以请原谅我做蠢事,

我的域模型有一个“投资组合”,其中包含零个或多个“未结头寸”。头寸由一个“工具”(它是一个JPA实体)和一个价格(两倍)组成。产品组合如下:

@Entity (name = "portfolio")
public class Portfolio {
    @Id
    @Column (name = "id")
    @GeneratedValue
    private long id;

    @ElementCollection (fetch = FetchType.EAGER)
    @CollectionTable (name = "portfolio_entry", joinColumns = @JoinColumn (name = "portfolio_id"))
    private final Map<Instrument, OpenPosition> positions = new HashMap<Instrument, OpenPosition>();
....
Run Code Online (Sandbox Code Playgroud)

可嵌入的OpenPosition如下:

@Embeddable
public class OpenPosition extends Position {
    @ManyToOne (targetEntity = InstrumentImpl.class, optional = false)
    @JoinColumn (name = "instrument_id", nullable = false)
    protected Instrument instrument;

    @Column (name = "price", nullable = false)
    protected double price;
....
Run Code Online (Sandbox Code Playgroud)

工具实体为: …

java hibernate jpa map

5
推荐指数
1
解决办法
2583
查看次数

布局和模板之间的液体变量范围

是否可以在布局文件中定义在模板文件中可见的液体变量?

布局中定义的变量(例如theme.liquid)对于通过包含的任何代码段都是可见的<% include %>(反之亦然)。

模板中定义的变量(例如index.liquid)在通过<% include %>(例如product-grid-item.liquid)包含的任何代码片段中都是可见的,并且在布局文件中也是可见的

但是,布局中定义的变量似乎对模板不可见。大概在评估布局之前先评估模板。有什么方法可以覆盖此行为?

liquid shopify

5
推荐指数
1
解决办法
466
查看次数