View Javadoc

1   /**
2    * 
3    */
4   package org.ximtec.igesture.core.composite;
5   
6   import java.util.HashSet;
7   import java.util.Iterator;
8   import java.util.List;
9   import java.util.Set;
10  import java.util.logging.Logger;
11  
12  import org.ximtec.igesture.core.Gesture;
13  import org.ximtec.igesture.io.IDeviceManager;
14  import org.ximtec.igesture.io.IUser;
15  
16  
17  /**
18   * This class represents a cardinality constraint. A certain gesture has to be performed between a mimimum and a maximum number 
19   * of times within a certain time interval. The main example for this kind of constraint is defining a majority. 
20   * Each user has only one vote.
21   * 
22   * @author Bjorn Puype, bpuype@gmail.com
23   *
24   */
25  public class CardinalityUserConstraint extends CardinalityDeviceConstraint {
26  
27  	private static final Logger LOGGER = Logger.getLogger(CardinalityUserConstraint.class.getName());
28  
29  	public CardinalityUserConstraint() {
30  		super();
31  	}
32  	
33  	/* (non-Javadoc)
34  	 * @see org.ximtec.igesture.core.composite.DefaultConstraint#validateConditions(java.util.List)
35  	 */
36  	@Override
37  	public boolean validateConditions(List<Gesture<?>> gestures, IDeviceManager manager) {
38  		boolean conditionsValid = super.validateConditions(gestures, manager);
39  		
40  		if(conditionsValid) // if previous conditions hold
41  		{
42  			
43  			Set<IUser> users = new HashSet<IUser>();
44  			
45  			//check if all gestures were created by different users
46  			for (Iterator iterator = gestures.iterator(); iterator.hasNext();) {
47  				Gesture<?> gesture = (Gesture<?>) iterator.next();
48  				IUser user = manager.getAssociatedUser(gesture.getSource());
49  				users.add(user);
50  			}
51  			
52  			if(users.size() != gestures.size())
53  				conditionsValid = false;
54  		}
55  		
56  		return conditionsValid;
57  	}
58  
59  	public String toString()
60  	{
61  		return CardinalityUserConstraint.class.getSimpleName();
62  	}
63  	
64  }