예제 #1
0
 /**
  * Puts all the values from the specified map into this map.
  *
  * @param map the map to add
  * @throws NullPointerException if the map is null
  */
 public void putAll(Map map) {
   int size = map.size();
   if (size == 0) {
     return;
   }
   if (delegateMap != null) {
     delegateMap.putAll(map);
     return;
   }
   if (size < 4) {
     for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
       Map.Entry entry = (Map.Entry) it.next();
       put(entry.getKey(), entry.getValue());
     }
   } else {
     convertToMap();
     delegateMap.putAll(map);
   }
 }
예제 #2
0
  /**
   * Puts a key-value mapping into this map.
   *
   * @param key the key to add
   * @param value the value to add
   * @return the value previously mapped to this key, null if none
   */
  public Object put(Object key, Object value) {
    if (delegateMap != null) {
      return delegateMap.put(key, value);
    }
    // change existing mapping
    if (key == null) {
      switch (size) { // drop through
        case 3:
          if (key3 == null) {
            Object old = value3;
            value3 = value;
            return old;
          }
        case 2:
          if (key2 == null) {
            Object old = value2;
            value2 = value;
            return old;
          }
        case 1:
          if (key1 == null) {
            Object old = value1;
            value1 = value;
            return old;
          }
      }
    } else {
      if (size > 0) {
        int hashCode = key.hashCode();
        switch (size) { // drop through
          case 3:
            if (hash3 == hashCode && key.equals(key3)) {
              Object old = value3;
              value3 = value;
              return old;
            }
          case 2:
            if (hash2 == hashCode && key.equals(key2)) {
              Object old = value2;
              value2 = value;
              return old;
            }
          case 1:
            if (hash1 == hashCode && key.equals(key1)) {
              Object old = value1;
              value1 = value;
              return old;
            }
        }
      }
    }

    // add new mapping
    switch (size) {
      default:
        convertToMap();
        delegateMap.put(key, value);
        return null;
      case 2:
        hash3 = (key == null ? 0 : key.hashCode());
        key3 = key;
        value3 = value;
        break;
      case 1:
        hash2 = (key == null ? 0 : key.hashCode());
        key2 = key;
        value2 = value;
        break;
      case 0:
        hash1 = (key == null ? 0 : key.hashCode());
        key1 = key;
        value1 = value;
        break;
    }
    size++;
    return null;
  }