public boolean equals(Object o) {
      if (!(o instanceof Map.Entry)) return false;
      Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;

      return (key == null ? e.getKey() == null : key.equals(e.getKey()))
          && (value == null ? e.getValue() == null : value.equals(e.getValue()));
    }
  /**
   * {@inheritDoc}
   *
   * <p>This method will, on a best-effort basis, throw a {@link
   * java.util.ConcurrentModificationException} if the remapping function modified this map during
   * computation.
   *
   * @throws ConcurrentModificationException if it is detected that the remapping function modified
   *     this map
   */
  @Override
  public synchronized V computeIfPresent(
      K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
    Objects.requireNonNull(remappingFunction);

    Entry<?, ?> tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    @SuppressWarnings("unchecked")
    Entry<K, V> e = (Entry<K, V>) tab[index];
    for (Entry<K, V> prev = null; e != null; prev = e, e = e.next) {
      if (e.hash == hash && e.key.equals(key)) {
        int mc = modCount;
        V newValue = remappingFunction.apply(key, e.value);
        if (mc != modCount) {
          throw new ConcurrentModificationException();
        }
        if (newValue == null) {
          if (prev != null) {
            prev.next = e.next;
          } else {
            tab[index] = e.next;
          }
          modCount = mc + 1;
          count--;
        } else {
          e.value = newValue;
        }
        return newValue;
      }
    }
    return null;
  }
Example #3
0
  /**
   * Maps the specified <code>key</code> to the specified <code>value</code> in this hashtable.
   * Neither the key nor the value can be <code>null</code>.
   *
   * <p>The value can be retrieved by calling the <code>get</code> method with a key that is equal
   * to the original key.
   *
   * @param key the hashtable key
   * @param value the value
   * @return the previous value of the specified key in this hashtable, or <code>null</code> if it
   *     did not have one
   * @exception NullPointerException if the key or value is <code>null</code>
   * @see Object#equals(Object)
   * @see #get(Object)
   */
  public synchronized V put(K key, V value) {
    // Make sure the value is not null
    if (value == null) {
      throw new NullPointerException();
    }

    // Makes sure the key is not already in the hashtable.
    Entry tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    for (Entry<K, V> e = tab[index]; e != null; e = e.next) {
      if ((e.hash == hash) && e.key.equals(key)) {
        V old = e.value;
        e.value = value;
        return old;
      }
    }

    modCount++;
    if (count >= threshold) {
      // Rehash the table if the threshold is exceeded
      rehash();

      tab = table;
      index = (hash & 0x7FFFFFFF) % tab.length;
    }

    // Creates the new entry.
    Entry<K, V> e = tab[index];
    tab[index] = new Entry<>(hash, key, value, e);
    count++;
    return null;
  }
  /**
   * {@inheritDoc}
   *
   * <p>This method will, on a best-effort basis, throw a {@link
   * java.util.ConcurrentModificationException} if the mapping function modified this map during
   * computation.
   *
   * @throws ConcurrentModificationException if it is detected that the mapping function modified
   *     this map
   */
  @Override
  public synchronized V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
    Objects.requireNonNull(mappingFunction);

    Entry<?, ?> tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    @SuppressWarnings("unchecked")
    Entry<K, V> e = (Entry<K, V>) tab[index];
    for (; e != null; e = e.next) {
      if (e.hash == hash && e.key.equals(key)) {
        // Hashtable not accept null value
        return e.value;
      }
    }

    int mc = modCount;
    V newValue = mappingFunction.apply(key);
    if (mc != modCount) {
      throw new ConcurrentModificationException();
    }
    if (newValue != null) {
      addEntry(hash, key, newValue, index);
    }

    return newValue;
  }
Example #5
0
 @SuppressWarnings("unchecked")
 @Override
 protected <K> K onSetProperty(String[] taPath, K toValue) {
   return (K)
       m_oProperties.setProperty(
           denormalise(taPath),
           toValue != null
               ? Java.isPrimitive(toValue) ? toValue.toString() : Utilities.toJSONString(toValue)
               : null);
 }
  /**
   * Returns a string representation of this {@code Hashtable} object in the form of a set of
   * entries, enclosed in braces and separated by the ASCII characters "<code> ,&nbsp;</code>"
   * (comma and space). Each entry is rendered as the key, an equals sign {@code =}, and the
   * associated element, where the {@code toString} method is used to convert the key and element to
   * strings.
   *
   * @return a string representation of this hashtable
   */
  public synchronized String toString() {
    int max = size() - 1;
    if (max == -1) return "{}";

    StringBuilder sb = new StringBuilder();
    Iterator<Map.Entry<K, V>> it = entrySet().iterator();

    sb.append('{');
    for (int i = 0; ; i++) {
      Map.Entry<K, V> e = it.next();
      K key = e.getKey();
      V value = e.getValue();
      sb.append(key == this ? "(this Map)" : key.toString());
      sb.append('=');
      sb.append(value == this ? "(this Map)" : value.toString());

      if (i == max) return sb.append('}').toString();
      sb.append(", ");
    }
  }
Example #7
0
    public boolean remove(Object o) {
      if (!(o instanceof Map.Entry)) return false;
      Map.Entry<K, V> entry = (Map.Entry<K, V>) o;
      K key = entry.getKey();
      Entry[] tab = table;
      int hash = key.hashCode();
      int index = (hash & 0x7FFFFFFF) % tab.length;

      for (Entry<K, V> e = tab[index], prev = null; e != null; prev = e, e = e.next) {
        if (e.hash == hash && e.equals(entry)) {
          modCount++;
          if (prev != null) prev.next = e.next;
          else tab[index] = e.next;

          count--;
          e.value = null;
          return true;
        }
      }
      return false;
    }
Example #8
0
 /**
  * JavaPropertyManager stored data as strings at the moment, so need to convert to the correct
  * type here
  *
  * @param taPath the path of the property to get
  * @param toDefault the default value of the property
  * @param <K> the type of the value
  * @return the value or null if there was no value of this type
  */
 @SuppressWarnings("unchecked")
 @Override
 protected <K> K onGetProperty(String[] taPath, K toDefault) {
   K loReturn = onGetProperty(taPath);
   if (loReturn == null) {
     setProperty(taPath, toDefault);
     loReturn = toDefault;
   } else {
     loReturn = (K) Java.stringToPrimitive((String) loReturn, toDefault.getClass());
   }
   return loReturn;
 }
 @Override
 public synchronized V replace(K key, V value) {
   Objects.requireNonNull(value);
   Entry<?, ?> tab[] = table;
   int hash = key.hashCode();
   int index = (hash & 0x7FFFFFFF) % tab.length;
   @SuppressWarnings("unchecked")
   Entry<K, V> e = (Entry<K, V>) tab[index];
   for (; e != null; e = e.next) {
     if ((e.hash == hash) && e.key.equals(key)) {
       V oldValue = e.value;
       e.value = value;
       return oldValue;
     }
   }
   return null;
 }
  private void addEntry(int hash, K key, V value, int index) {
    Entry<?, ?> tab[] = table;
    if (count >= threshold) {
      // Rehash the table if the threshold is exceeded
      rehash();

      tab = table;
      hash = key.hashCode();
      index = (hash & 0x7FFFFFFF) % tab.length;
    }

    // Creates the new entry.
    @SuppressWarnings("unchecked")
    Entry<K, V> e = (Entry<K, V>) tab[index];
    tab[index] = new Entry<>(hash, key, value, e);
    count++;
    modCount++;
  }
Example #11
0
  /**
   * Associates the specified value with the specified key in this map. If the map previously
   * contained a mapping for the key, the old value is replaced.
   *
   * @param key key with which the specified value is to be associated
   * @param value value to be associated with the specified key
   * @return the previous value associated with <tt>key</tt>, or <tt>null</tt> if there was no
   *     mapping for <tt>key</tt>. (A <tt>null</tt> return can also indicate that the map previously
   *     associated <tt>null</tt> with <tt>key</tt>.)
   */
  public V put(K key, V value) {
    if (key == null) return putForNullKey(value);
    int hash = hash(key);
    int i = indexFor(hash, table.length);
    for (Entry<K, V> e = table[i]; e != null; e = e.next) {
      Object k;
      if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
        V oldValue = e.value;
        e.value = value;
        e.recordAccess(this);
        return oldValue;
      }
    }

    modCount++;
    addEntry(hash, key, value, i);
    return null;
  }
Example #12
0
 /**
  * The put method used by readObject. This is provided because put is overridable and should not
  * be called in readObject since the subclass will not yet be initialized.
  *
  * <p>This differs from the regular put method in several ways. No checking for rehashing is
  * necessary since the number of elements initially in the table is known. The modCount is not
  * incremented because we are creating a new instance. Also, no return value is needed.
  */
 private void reconstitutionPut(Entry[] tab, K key, V value) throws StreamCorruptedException {
   if (value == null) {
     throw new java.io.StreamCorruptedException();
   }
   // Makes sure the key is not already in the hashtable.
   // This should not happen in deserialized version.
   int hash = key.hashCode();
   int index = (hash & 0x7FFFFFFF) % tab.length;
   for (Entry<K, V> e = tab[index]; e != null; e = e.next) {
     if ((e.hash == hash) && e.key.equals(key)) {
       throw new java.io.StreamCorruptedException();
     }
   }
   // Creates the new entry.
   Entry<K, V> e = tab[index];
   tab[index] = new Entry<>(hash, key, value, e);
   count++;
 }
Example #13
0
  /**
   * This method is used instead of put by constructors and pseudoconstructors (clone, readObject).
   * It does not resize the table, check for comodification, etc. It calls createEntry rather than
   * addEntry.
   */
  private void putForCreate(K key, V value) {
    int hash = null == key ? 0 : hash(key);
    int i = indexFor(hash, table.length);

    /**
     * Look for preexisting entry for key. This will never happen for clone or deserialize. It will
     * only happen for construction if the input Map is a sorted map whose ordering is inconsistent
     * w/ equals.
     */
    for (Entry<K, V> e = table[i]; e != null; e = e.next) {
      Object k;
      if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) {
        e.value = value;
        return;
      }
    }

    createEntry(hash, key, value, i);
  }
 @Override
 public synchronized boolean replace(K key, V oldValue, V newValue) {
   Objects.requireNonNull(oldValue);
   Objects.requireNonNull(newValue);
   Entry<?, ?> tab[] = table;
   int hash = key.hashCode();
   int index = (hash & 0x7FFFFFFF) % tab.length;
   @SuppressWarnings("unchecked")
   Entry<K, V> e = (Entry<K, V>) tab[index];
   for (; e != null; e = e.next) {
     if ((e.hash == hash) && e.key.equals(key)) {
       if (e.value.equals(oldValue)) {
         e.value = newValue;
         return true;
       } else {
         return false;
       }
     }
   }
   return false;
 }
  @Override
  public synchronized V putIfAbsent(K key, V value) {
    Objects.requireNonNull(value);

    // Makes sure the key is not already in the hashtable.
    Entry<?, ?> tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    @SuppressWarnings("unchecked")
    Entry<K, V> entry = (Entry<K, V>) tab[index];
    for (; entry != null; entry = entry.next) {
      if ((entry.hash == hash) && entry.key.equals(key)) {
        V old = entry.value;
        if (old == null) {
          entry.value = value;
        }
        return old;
      }
    }

    addEntry(hash, key, value, index);
    return null;
  }
  /**
   * Maps the specified {@code key} to the specified {@code value} in this hashtable. Neither the
   * key nor the value can be {@code null}.
   *
   * <p>The value can be retrieved by calling the {@code get} method with a key that is equal to the
   * original key.
   *
   * @param key the hashtable key
   * @param value the value
   * @return the previous value of the specified key in this hashtable, or {@code null} if it did
   *     not have one
   * @exception NullPointerException if the key or value is {@code null}
   * @see Object#equals(Object)
   * @see #get(Object)
   */
  public synchronized V put(K key, V value) {
    // Make sure the value is not null
    if (value == null) {
      throw new NullPointerException();
    }

    // Makes sure the key is not already in the hashtable.
    Entry<?, ?> tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    @SuppressWarnings("unchecked")
    Entry<K, V> entry = (Entry<K, V>) tab[index];
    for (; entry != null; entry = entry.next) {
      if ((entry.hash == hash) && entry.key.equals(key)) {
        V old = entry.value;
        entry.value = value;
        return old;
      }
    }

    addEntry(hash, key, value, index);
    return null;
  }
Example #17
0
 public final int hashCode() {
   return (key == null ? 0 : key.hashCode()) ^ (value == null ? 0 : value.hashCode());
 }
 public String toString() {
   return key.toString() + "=" + value.toString();
 }
Example #19
0
    // O: 1-{4,5,6,7} 0-{0,1,2,3}
    // P: 1-{2,3,6,7} 0-{0,1,4,5}
    // S: 1-{1,3,5,7} 0-{0,2,4,6}
    public int getPartition(K key, V value, int numReduceTasks) {

      String line = key.toString();

      if (line.startsWith("O")) // order
      {
        String keyIdS = line.substring(1, line.length() - 1);
        String placeId = line.substring(line.length() - 1);
        Integer keyId = 0;

        try {
          keyId = Integer.parseInt(keyIdS);
        } catch (Exception ex) {
          return 0;
        }
        if (keyId % 2 == 1) {
          // 4,5,6,7
          if (placeId.equalsIgnoreCase("A")) {
            return 4;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 5;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 6;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 7;
          } else {
            return 0;
          }
        } else {
          // 0,1,2,3
          if (placeId.equalsIgnoreCase("A")) {
            return 0;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 1;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 2;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 3;
          } else {
            return 0;
          }
        }
      } else if (line.startsWith("P")) // part
      {
        String keyIdS = line.substring(1, line.length() - 1);
        String placeId = line.substring(line.length() - 1);
        Integer keyId = 0;

        try {
          keyId = Integer.parseInt(keyIdS);
        } catch (Exception ex) {
          return 0;
        }

        if (keyId % 2 == 1) {
          // 2,3,6,7
          if (placeId.equalsIgnoreCase("A")) {
            return 2;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 3;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 6;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 7;
          } else {
            return 0;
          }
        } else {
          // 0,1,4,5
          if (placeId.equalsIgnoreCase("A")) {
            return 0;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 1;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 4;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 5;
          } else {
            return 0;
          }
        }
      } else if (line.startsWith("S")) // supplier
      {
        String keyIdS = line.substring(1, line.length() - 1);
        String placeId = line.substring(line.length() - 1);
        Integer keyId = 0;

        try {
          keyId = Integer.parseInt(keyIdS);
        } catch (Exception ex) {
          return 0;
        }

        if (keyId % 2 == 1) {
          // 1,3,5,7
          if (placeId.equalsIgnoreCase("A")) {
            return 1;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 3;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 5;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 7;
          } else {
            return 0;
          }
        } else {
          // 0,2,4,6
          if (placeId.equalsIgnoreCase("A")) {
            return 0;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 2;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 4;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 6;
          } else {
            return 0;
          }
        }
      } else if (line.startsWith("L")) // lineitem
      {
        String[] keyIdS = line.substring(1).split("[+]");

        Integer keyId0 = 0;
        Integer keyId1 = 0;
        Integer keyId2 = 0;

        try {
          keyId0 = Integer.parseInt(keyIdS[0].substring(1)) % 2; // Order
          keyId1 = Integer.parseInt(keyIdS[1].substring(1)) % 2; // Part
          keyId2 = Integer.parseInt(keyIdS[2].substring(1)) % 2; // Supplier
        } catch (Exception ex) {
          return 0;
        }
        return 4 * keyId0 + 2 * keyId1 + 1 * keyId2;

      } else {
        return 0;
      }
    }