View Javadoc

1   package cz.cuni.amis.utils.flag.connective;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   import java.util.HashSet;
6   import java.util.List;
7   import java.util.Set;
8   
9   import cz.cuni.amis.utils.HashCode;
10  import cz.cuni.amis.utils.flag.Flag;
11  
12  /**
13   * Used to create logical expression out of different flags. (Warning: spagetthi code ahead!)
14   * @author Jimmy
15   */
16  public abstract class Connective extends Flag<Boolean> {
17  	
18  	protected List<ConnectiveListener> listeners = new ArrayList<ConnectiveListener>();
19  	
20  	protected int[] truthValue = new int[]{0};
21  	
22  	protected Set<Flag<Boolean>> flags = new HashSet<Flag<Boolean>>();
23  	
24  	private int hashCode = 0;
25  	
26  	public Connective(Flag<Boolean> flag1, Flag<Boolean> flag2) {
27  		this(new Flag[]{flag1, flag2});
28  	}
29  
30  	public Connective(Flag<Boolean>[] flags) {
31  		if (flags.length > Integer.SIZE) throw new IllegalArgumentException("Can't have connective with more then " + Integer.SIZE + " arguments (int bit count).");
32  		synchronized(truthValue) {
33  			int i = 0;
34  			Arrays.sort(flags);
35  			HashCode hc = new HashCode();
36  			for (Flag<Boolean> flag : flags) {
37  				this.flags.add(flag);
38  				hc.add(flag);
39  				ConnectiveListener listener = new ConnectiveListener(this, flag, i);
40  				listeners.add(listener);
41  				++i;			
42  			}
43  			hashCode = hc.getHash();
44  		}
45  	}
46  	
47  	public int hashCode() {
48  		return hashCode;
49  	}
50  	
51  	protected abstract void truthValueChanged();
52  
53  }