View Javadoc

1   package cz.cuni.amis.utils.flag.connective;
2   
3   import java.util.Map;
4   import java.util.WeakHashMap;
5   
6   import cz.cuni.amis.utils.flag.Flag;
7   
8   
9   public class And extends Connective {
10  	
11  	private static Map<And, And> ands = new WeakHashMap<And, And>();
12  	
13  	public static And get(Flag<Boolean> flag1, Flag<Boolean> flag2) {
14  		return get(new Flag[]{flag1, flag2});
15  	}
16  	
17  	public static And get(Flag<Boolean>[] flags) {
18  		synchronized(ands) {
19  			And and = new And(flags);
20  			And existingAnd = ands.get(and);
21  			if (existingAnd != null) return existingAnd;
22  			ands.put(and, and);
23  			return and;
24  		}		
25  	}
26  	
27  	protected int trueValue = 0;
28  	
29  	private And(Flag<Boolean> flag1, Flag<Boolean> flag2) {
30  		this(new Flag[]{flag1, flag2});
31  	}
32  
33  	private And(Flag<Boolean>[] flags) {
34  		super(flags);
35  		trueValue = (int) Math.pow(2, flags.length-1);
36  	}
37  
38  	@Override
39  	protected void truthValueChanged() {
40  		synchronized(truthValue) {
41  			setFlag(truthValue[0] == trueValue);
42  		}
43  	}
44  	
45  	public boolean equals(Object obj) {
46  		if (!(obj instanceof And)) return false;
47  		And and = (And)obj;
48  		for (Flag<Boolean> flag : this.flags) {
49  			if (!and.flags.contains(flag)) return false;
50  		}
51  		return true;
52  	}
53  	
54  }