Home | History | Annotate | Download | only in keyboard
      1 package org.unicode.cldr.draft.keyboard;
      2 
      3 import static com.google.common.base.Preconditions.checkNotNull;
      4 
      5 import com.google.common.base.MoreObjects;
      6 import com.google.common.base.Objects;
      7 import com.google.common.collect.ImmutableSet;
      8 import com.google.common.collect.ImmutableSortedMap;
      9 import com.google.common.collect.Maps;
     10 
     11 /**
     12  * Object that represents a full keyboard mapping for a given modifier key combination set.
     13  *
     14  * <p>
     15  * For example, the English-US keyboard with the Shift modifier activated outputs:
     16  * <ul>
     17  * <li>{@code ISO=E01 US-101 keyboard=[1] = '!'}
     18  * <li>{@code E02 [2] = '@'}
     19  * <li>{@code E03 [3] = '#'}
     20  * <li>{@code E04 [4] = '$'}
     21  * <li>{@code D01 [Q] = 'Q'}
     22  * <li>And so on...
     23  * </ul>
     24  */
     25 public final class KeyMap implements Comparable<KeyMap> {
     26     private final ModifierKeyCombinationSet modifierKeyCombinationSet;
     27     private final ImmutableSortedMap<IsoLayoutPosition, CharacterMap> isoLayoutToCharacterMap;
     28 
     29     private KeyMap(ModifierKeyCombinationSet modifierKeyCombinationSet,
     30         ImmutableSortedMap<IsoLayoutPosition, CharacterMap> isoLayoutToCharacterMap) {
     31         this.modifierKeyCombinationSet = checkNotNull(modifierKeyCombinationSet);
     32         this.isoLayoutToCharacterMap = checkNotNull(isoLayoutToCharacterMap);
     33     }
     34 
     35     /** Creates a key map from the given modifier key combination set and characer maps. */
     36     public static KeyMap of(ModifierKeyCombinationSet modifierKeyCombinationSet,
     37         ImmutableSet<CharacterMap> characterMaps) {
     38         return new KeyMap(modifierKeyCombinationSet, ImmutableSortedMap.copyOf(Maps.uniqueIndex(
     39             characterMaps, CharacterMap.isoLayoutPositionFunction())));
     40     }
     41 
     42     public ModifierKeyCombinationSet modifierKeyCombinationSet() {
     43         return modifierKeyCombinationSet;
     44     }
     45 
     46     public ImmutableSortedMap<IsoLayoutPosition, CharacterMap> isoLayoutToCharacterMap() {
     47         return isoLayoutToCharacterMap;
     48     }
     49 
     50     @Override
     51     public String toString() {
     52         return MoreObjects.toStringHelper(this)
     53             .add("modifierKeyCombinationSet", modifierKeyCombinationSet)
     54             .add("isoLayoutToCharacterMap", isoLayoutToCharacterMap)
     55             .toString();
     56     }
     57 
     58     @Override
     59     public boolean equals(Object o) {
     60         if (this == o) {
     61             return true;
     62         }
     63         if (o instanceof KeyMap) {
     64             KeyMap other = (KeyMap) o;
     65             return modifierKeyCombinationSet.equals(other.modifierKeyCombinationSet)
     66                 && isoLayoutToCharacterMap.equals(other.isoLayoutToCharacterMap);
     67         }
     68         return false;
     69     }
     70 
     71     @Override
     72     public int hashCode() {
     73         return Objects.hashCode(modifierKeyCombinationSet, isoLayoutToCharacterMap);
     74     }
     75 
     76     @Override
     77     public int compareTo(KeyMap o) {
     78         // Order the key maps by their modifier sets.
     79         return modifierKeyCombinationSet.compareTo(o.modifierKeyCombinationSet);
     80     }
     81 }
     82