Ejemplo n.º 1
0
    V put(K key, int hash, V value, boolean onlyIfAbsent) {
      lock();
      try {
        int c = count;
        HashEntry[] tab = table;
        int index = hash & (tab.length - 1);
        HashEntry<K, V> first = (HashEntry<K, V>) tab[index];

        for (HashEntry<K, V> e = first; e != null; e = (HashEntry<K, V>) e.next) {
          if (e.hash == hash && key.equals(e.key)) {
            V oldValue = e.value;
            if (!onlyIfAbsent) e.value = value;
            ++modCount;
            count = c; // write-volatile
            return oldValue;
          }
        }

        tab[index] = new HashEntry<K, V>(hash, key, value, first);
        ++modCount;
        ++c;
        count = c; // write-volatile
        if (c > threshold) setTable(rehash(tab));
        return null;
      } finally {
        unlock();
      }
    }
Ejemplo n.º 2
0
    boolean replace(K key, int hash, V oldValue, V newValue) {
      lock();
      try {
        int c = count;
        HashEntry[] tab = table;
        int index = hash & (tab.length - 1);
        HashEntry<K, V> first = (HashEntry<K, V>) tab[index];
        HashEntry<K, V> e = first;
        for (; ; ) {
          if (e == null) return false;
          if (e.hash == hash && key.equals(e.key)) break;
          e = e.next;
        }

        V v = e.value;
        if (v == null || !oldValue.equals(v)) return false;

        e.value = newValue;
        count = c; // write-volatile
        return true;

      } finally {
        unlock();
      }
    }
Ejemplo n.º 3
0
 public int lastIndexOfSecond(K k, int index) {
   try {
     for (int i = index; i >= 0; i--) {
       if ((k == null ? get(i).second == null : k.equals(get(i).second))) return i;
     }
   } catch (final Exception e) {
   }
   return -1;
 }
Ejemplo n.º 4
0
 public synchronized boolean removeSecond(K k) {
   Quint<T, K, L, M, N> pair;
   for (final Iterator<Quint<T, K, L, M, N>> i = iterator(); i.hasNext(); ) {
     pair = i.next();
     if ((k == null ? pair.second == null : k.equals(pair.second))) {
       i.remove();
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 5
0
 public boolean containsSecond(K k) {
   for (final Iterator<Quint<T, K, L, M, N>> i = iterator(); i.hasNext(); ) {
     if ((k == null) ? i.next() == null : k.equals(i.next().second)) return true;
   }
   return false;
 }
Ejemplo n.º 6
0
  /**
   * Gets registered object from CacheMap. The algorithm used to look up is <br>
   * 1. First check for exact match with clazz and context.<br>
   * 2. If didn't find, look for interfaces that clazz implements using the exact context.<br>
   * 3. If still didn't find, look for super class of clazz using the exact context. <br>
   * 4. If still didn't find, using the exact clazz with default context.<br>
   * 5. If still didn't find, return null.<br>
   * If found a match in step 1, 2, 3 or 4, it will return the registered object immediately.
   *
   * @param clazz the class which is used as the primary key.
   * @param context the context which is used as the secondary key. This parameter could be null in
   *     which case the default context is used.
   * @return registered object the object associated with the class and the context.
   */
  public T getRegisteredObject(Class<?> clazz, K context) {
    if (clazz == null) {
      return null;
    }

    Cache<K, T> cache = getCache(clazz);

    if (cache == null || !cache.containsKey(context)) {
      List<Class<?>> classesToSearch = new ArrayList<>();

      classesToSearch.add(clazz);
      if (TypeUtils.isPrimitive(clazz)) {
        classesToSearch.add(TypeUtils.convertPrimitiveToWrapperType(clazz));
      } else if (TypeUtils.isPrimitiveWrapper(clazz)) {
        classesToSearch.add(TypeUtils.convertWrapperToPrimitiveType(clazz));
      }

      // Direct super interfaces, recursively
      addAllInterfaces(classesToSearch, clazz);

      Class<?> superClass = clazz;
      // Direct super class, recursively
      while (!superClass.isInterface()) {
        superClass = superClass.getSuperclass();
        if (superClass != null) {
          classesToSearch.add(superClass);
          addAllInterfaces(classesToSearch, superClass);
        } else {
          break;
        }
      }

      if (!classesToSearch.contains(Object.class)) {
        classesToSearch.add(Object.class); // use Object as default fallback.
      }

      // search to match context first
      for (Class<?> c : classesToSearch) {
        Cache<K, T> cacheForClass = getCache(c);
        if (cacheForClass != null) {
          T object = cacheForClass.getObject(context);
          if (object != null) {
            return object;
          }
        }
      }

      // fall back to default context
      if (!_defaultContext.equals(context)) {
        for (Class<?> c : classesToSearch) {
          Cache<K, T> cacheForClass = getCache(c);
          if (cacheForClass != null) {
            T object = cacheForClass.getObject(_defaultContext);
            if (object != null) {
              return object;
            }
          }
        }
      }
    }

    if (cache != null) {
      T object = cache.getObject(context);
      if (object == null && !_defaultContext.equals(context)) {
        return getRegisteredObject(clazz, _defaultContext);
      }
      if (object != null) {
        return object;
      }
    }

    return null;
  }
 boolean equals(K key, K key2) {
   return csKey ? equalsCS((CharSequence) key, (CharSequence) key2) : key.equals(key2);
 }