📄 destinationset.java
字号:
package broker;import java.util.*;import com.Message;import com.Unsubscription;import util.*;public class DestinationSet { /** * An entry maps a message (or filter) to a destination. * @author parzy * */ public static class Entry{ /** * The entry's destination. */ public final Destination destination; /** * The entry's message. */ public final Message message; /** * The entry's filter. */ protected final Filter filter; /** * Creates an entry mapping a message or filter to a destination. * @param entry the original entry from the hashtable. */ protected Entry(Map.Entry entry){ Object value; this.destination = (Destination)entry.getKey(); // either a message or a filter is mapped value = entry.getValue(); if(value instanceof Message){ this.message = (Message)value; this.filter = null; }else{ this.message = null; this.filter = (Filter)entry.getValue(); } } /** * Returns the entry's destination. * @return the entry's destination. */ public Destination getDestintion(){ return destination; } /** * Returns the entry's message. * @return the entry's message. */ public Message getMessage(){ return message; } /** * Returns the entry's filter. * @return the entry's filter. */ protected Filter getFilter(){ return filter; } } /** * Provides an iterator of the destination set. * @author parzy */ protected class SetIterator implements Iterator{ /** * The iterator provided by the hashtable class. */ Iterator iterator; /** * Creates a new set iterator. */ protected SetIterator(){ iterator = DestinationSet.this.destinations.entryIterator; //entrySet().iterator(); } /** * Returns true if the iteration has more elements. * @return true if the iteration has more elements. */ public boolean hasNext(){ return iterator.hasNext(); } /** * Returns the next element in the iteration. * @return the next element in the iteration. */ public Object next(){ return new DestinationSet.Entry((Map.Entry)iterator.next()); } /** * Removes from the underlying collection the last element * returned by the iterator. */ public void remove(){ throw new UnsupportedOperationException(); //iterator.remove(); } } /** * The underlying hashtable the destination set is based on. */ protected SimpleHashtable destinations = new SimpleHashtable(); /** * The message or filter to be mapped to added destinations. */ protected Message message = null; protected Filter filter = null; /** * Flag, if adding destinations is allowed. */ protected boolean restricted = false; /** * Creates an empty destination set. */ public DestinationSet(){} /** * Creates an empty destination set. * @param message message to be mapped to added destinations. */ public DestinationSet(Message message){ this.message = message; } /** * Creates an empty destination set. * @param filter filter to be mapped to added destinations. */ public DestinationSet(Filter filter){ this.filter = filter; } /** * Sets the message, which is mapped to all added destinations. * Overrides a filter previously set. * @param message messge which is mapped to all added destinations. */ public void setMessage(Message message){ this.message = message; this.filter = null; } /** * Returns the last message, which was set. * @return the last message, which was set. */ public Message getMessage(){ return message; } /** * Sets the filter, which is mapped to all added destinations. * Overrides a message previously set. * @param filter filter which is mapped to all added destinations. */ public void setFilter(Filter filter){ this.filter = filter; this.message = null; } /** * Returns the last filter, which was set. * @return the last filter, which was set. */ public Filter getFilter(){ return this.filter; } /** * Returns true, if the destination set maps no destination to a message. * @return true, if the destination set maps no destination to a message, * otherwise false. */ public boolean isEmpty(){ return destinations.isEmpty(); } /** * Clears all destinations. */ public void clear(){ destinations.clear(); } /** * Returns a new Iterator to access the elements in the set. * @return a set iterator. */ public Iterator iterator(){ return new SetIterator(); } /** * Adds a destination to the set, if it is not restricted. * A previously specified message or filter is mapped to the added * destination. * @param destination the destination to add. */ public void add(Destination destination){ if(!restricted){ if(filter == null){ destinations.put(destination, message.clone()); }else{ destinations.put(destination, filter); } } } /** * Adds a all destinations to the set, if it is not restricted. * A previously specified message or filter is mapped to the added * destinations. * @param destinations the destinations to add. */ public void addAll(Collection destinations){ Destination destination; if(!restricted){ for(Iterator it = destinations.iterator(); it.hasNext(); ) { destination = (Destination)it.next(); this.add(destination); } } } /** * Removes a destination from the set. * @param destination the destination to remove. */ public void remove(Destination destination){ destinations.remove(destination); } /** * Returns true if the destination set contains the specified destination. * @param destination the destination. * @return true if the destination set contains the specified destination. */ public boolean contains(Destination destination){ return destinations.containsKey(destination); } /** * Restricts the set to the specified destination. * If the specified destination is not a member of the set, * the result is an empty set. * @param destination the specified destination. */ public void restrictTo(Destination destination){ Object message; message = destinations.get(destination); destinations.clear(); if(message!=null){ destinations.put(destination, message); } restricted=true; } /** * Adds all mapped filters to the uncovered subscriptions. * @param uncoveredDestinations a destination set containing the uncovered * filters with its destinations. */ public void addFilters(DestinationSet uncoveredDestinations){ Entry entry; // entry of the uncovered destinations Unsubscription unsubscription; // current unsubscription // for each uncovered filter and destination for(Iterator it = uncoveredDestinations.iterator(); it.hasNext(); ){ entry = (Entry)it.next(); unsubscription = (Unsubscription)destinations.get(entry.destination); unsubscription.getUncoveredFilters().add(entry.filter); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -