class IdentityMultimap {

  synchronized boolean addKeyValuePair(@NonNull final K key, @NonNull final V value) {
    int size = 0;
    int indexToAdd = -1;
    boolean hasValue = false;
    for (int index = 0; index < keysValues.length; index += 2) {
      final Object keysValue = keysValues[index];
      if (keysValue == null) {
        indexToAdd = index;
      }
      if (keysValue == key) {
        size++;
        if (keysValues[index + 1] == value) {
          indexToAdd = index;
          hasValue = true;
        }
      }
    }
    if (indexToAdd == -1) {
      indexToAdd = keysValues.length;
      keysValues = Arrays.copyOf(keysValues, indexToAdd < 2 ? 2 : indexToAdd * 2);
    }
    if (!hasValue) {
      keysValues[indexToAdd] = key;
      keysValues[indexToAdd + 1] = value;
    }
    return size == 0;
  }

}