View Javadoc

1   package cz.cuni.amis.utils.flag.connective;
2   
3   import cz.cuni.amis.utils.flag.Flag;
4   import cz.cuni.amis.utils.flag.FlagListener;
5   
6   public class ConnectiveListener implements FlagListener<Boolean> {
7   	
8   	Flag<Boolean> myFlag;
9   	
10  	private Object mutex = new Object();
11  	
12  	private int intTrue;
13  	private int intFalse;
14  	
15  	private Connective connective;
16  	
17  	public ConnectiveListener(Connective connective, Flag<Boolean> flag, int myIndex) {
18  		this.myFlag = flag;
19  		this.connective = connective; 			
20  		intTrue = 1 << myIndex;
21  		intFalse = 0 ^ intTrue;
22  		synchronized(mutex) {
23  			this.myFlag.addListener(this);
24  			if (flag.getFlag()) {
25  				synchronized(connective.truthValue) {
26  					connective.truthValue[0] = connective.truthValue[0] | intTrue;
27  				}
28  			} else {
29  				synchronized(connective.truthValue) {
30  					connective.truthValue[0] = connective.truthValue[0] & intFalse;
31  				}
32  			}				
33  		}
34  	}
35  
36  	@Override
37  	public void flagChanged(Boolean changedValue) {
38  		if (changedValue == null || !changedValue) {
39  			synchronized(connective.truthValue) {
40  				connective.truthValue[0] = connective.truthValue[0] | intTrue;
41  				connective.truthValueChanged();
42  			}
43  		} else {
44  			synchronized(connective.truthValue) {
45  				connective.truthValue[0] = connective.truthValue[0] & intFalse;
46  				connective.truthValueChanged();
47  			}
48  		}
49  		
50  	}
51  	
52  }