View Javadoc

1   package com.atlassian.webtest.ui.keys;
2   
3   import java.util.Collection;
4   import java.util.EnumSet;
5   import java.util.Set;
6   
7   import static com.google.common.base.Preconditions.checkNotNull;
8   
9   /**
10   * Abstract implementation of the {@link com.atlassian.webtest.ui.keys.KeySequence} interface and its accompanying
11   * interfaces.
12   *
13   * @since v1.21
14   */
15  public abstract class AbstractKeySequence implements KeySequence, TypeModeAware, KeyEventAware
16  {
17  
18      private final EnumSet<ModifierKey> toPress;
19      private final EnumSet<KeyEventType> keyEvents;
20      private final TypeMode typeMode;
21  
22  
23      public AbstractKeySequence(TypeMode typeMode, Collection<ModifierKey> toPress, Collection<KeyEventType> events)
24      {
25          this.toPress = toEnumSet(toPress, ModifierKey.class);
26          this.keyEvents = toEnumSet(events, KeyEventType.class);
27          this.typeMode = checkNotNull(typeMode, "typeMode");
28      }
29  
30      private <E extends Enum<E>> EnumSet<E> toEnumSet(Collection<E> toPress, Class<E> enumType)
31      {
32          if (toPress instanceof EnumSet<?>)
33          {
34              return EnumSet.copyOf((EnumSet<E>) toPress);
35          }
36          return toPress.size() == 0 ? EnumSet.noneOf(enumType) : EnumSet.copyOf(toPress);
37      }
38  
39  
40      public final Set<ModifierKey> withPressed()
41      {
42          return toPress;
43      }
44  
45      public final Set<KeyEventType> keyEvents()
46      {
47          return keyEvents;
48      }
49  
50      public final TypeMode typeMode()
51      {
52          return typeMode;
53      }
54  }