Merge remote-tracking branch 'origin/feature/channel_impl' into feature/channel_impl
This commit is contained in:
commit
8b47edd07b
@ -3,16 +3,20 @@ package cz.cvut.fel.sit.omo.foodchain.channel;
|
||||
import cz.cvut.fel.sit.omo.foodchain.operation.Transaction;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
import cz.cvut.fel.sit.omo.foodchain.requirement.Requirement;
|
||||
import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver;
|
||||
import cz.cvut.fel.sit.omo.foodchain.util.Observable;
|
||||
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class ChannelBase implements IChannel
|
||||
{
|
||||
public abstract class ChannelBase implements IChannel, Observable {
|
||||
protected ProductType productType;
|
||||
protected Queue<Requirement> requirements;
|
||||
protected Queue<Transaction> transactions;
|
||||
|
||||
private Set<ChannelObserver> subscribers;
|
||||
|
||||
public ChannelBase(ProductType type)
|
||||
{
|
||||
this.requirements = new PriorityQueue<>();
|
||||
@ -47,6 +51,22 @@ public abstract class ChannelBase implements IChannel
|
||||
public void addRequirement(Requirement requirement)
|
||||
{
|
||||
this.requirements.add(requirement);
|
||||
notifyAllObservers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyAllObservers(){
|
||||
subscribers.forEach(subscriber -> subscriber.update(requirements.peek()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attach(ChannelObserver observer){
|
||||
subscribers.add(observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void detach(ChannelObserver observer){
|
||||
subscribers.remove(observer);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -0,0 +1,7 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.util;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.requirement.Requirement;
|
||||
|
||||
public interface ChannelObserver {
|
||||
void update(Requirement requirement);
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.util;
|
||||
|
||||
public interface Observable {
|
||||
void attach(ChannelObserver channelObserver);
|
||||
void detach(ChannelObserver channelObserver);
|
||||
void notifyAllObservers();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user