Java Code Examples for java.util.IdentityHashMap#size()

The following examples show how to use java.util.IdentityHashMap#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataSerializer.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Writes a <code>IdentityHashMap</code> to a <code>DataOutput</code>.  Note
 * that even though <code>map</code> may be an instance of a
 * subclass of <code>IdentityHashMap</code>, <code>readIdentityHashMap</code> will
 * always return an instance of <code>IdentityHashMap</code>, <B>not</B> an
 * instance of the subclass.  To preserve the class type of
 * <code>map</code>, {@link #writeObject(Object, DataOutput)} should be used for data
 * serialization.
 *
 * @throws IOException
 *         A problem occurs while writing to <code>out</code>
 *
 * @see #readIdentityHashMap
 */
public static void writeIdentityHashMap(IdentityHashMap<?,?> map, DataOutput out)
  throws IOException {

  InternalDataSerializer.checkOut(out);

  int size;
  if (map == null) {
    size = -1;
  } else {
    size = map.size();
  }
  InternalDataSerializer.writeArrayLength(size, out);
  if (DEBUG) {
    InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing IdentityHashMap with " + size + " elements: " + map);
  }
  if (size > 0) {
    for (Map.Entry<?,?> entry: map.entrySet()){
      writeObject(entry.getKey(), out);
      writeObject(entry.getValue(), out);
    }
  }
}
 
Example 2
Source File: SpanNode2AnnotationContainer.java    From vespa with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
Iterator<Annotation> iteratorRecursive(SpanNode node) {
    IdentityHashMap<SpanNode, SpanNode> nodes = new IdentityHashMap<SpanNode, SpanNode>();
    nodes.put(node, node);
    {
        Iterator<SpanNode> childrenIt = node.childIteratorRecursive();
        while (childrenIt.hasNext()) {
            SpanNode child = childrenIt.next();
            nodes.put(child, child);
        }
    }
    List<Collection<Annotation>> annotationLists = new ArrayList<Collection<Annotation>>(nodes.size());
    for (SpanNode includedNode : nodes.keySet()) {
        Collection<Annotation> includedAnnotations = spanNode2Annotation.get(includedNode);
        if (includedAnnotations != null) {
            annotationLists.add(includedAnnotations);
        }
    }
    return new AnnotationCollectionIterator(annotationLists);
}
 
Example 3
Source File: DataSerializer.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Writes a <code>IdentityHashMap</code> to a <code>DataOutput</code>.  Note
 * that even though <code>map</code> may be an instance of a
 * subclass of <code>IdentityHashMap</code>, <code>readIdentityHashMap</code> will
 * always return an instance of <code>IdentityHashMap</code>, <B>not</B> an
 * instance of the subclass.  To preserve the class type of
 * <code>map</code>, {@link #writeObject(Object, DataOutput)} should be used for data
 * serialization.
 *
 * @throws IOException
 *         A problem occurs while writing to <code>out</code>
 *
 * @see #readIdentityHashMap
 */
public static void writeIdentityHashMap(IdentityHashMap<?,?> map, DataOutput out)
  throws IOException {

  InternalDataSerializer.checkOut(out);

  int size;
  if (map == null) {
    size = -1;
  } else {
    size = map.size();
  }
  InternalDataSerializer.writeArrayLength(size, out);
  if (DEBUG) {
    InternalDataSerializer.logger.info( LocalizedStrings.DEBUG, "Writing IdentityHashMap with " + size + " elements: " + map);
  }
  if (size > 0) {
    for (Map.Entry<?,?> entry: map.entrySet()){
      writeObject(entry.getKey(), out);
      writeObject(entry.getValue(), out);
    }
  }
}
 
Example 4
Source File: Capacity.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given the expected size, computes such a number N of items that
 * inserting (N+1) items will trigger resizing of the internal storage
 */
static int threshold(int size) throws Throwable {
    IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
    int initialCapacity = capacity(m);
    while (capacity(m) == initialCapacity)
        growUsingPut(m, 1);
    return m.size() - 1;
}
 
Example 5
Source File: HealthServiceImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
int numWatchersForTest(String service) {
  synchronized (watchLock) {
    IdentityHashMap<StreamObserver<HealthCheckResponse>, Boolean> serviceWatchers =
        watchers.get(service);
    if (serviceWatchers == null) {
      return 0;
    }
    return serviceWatchers.size();
  }
}
 
Example 6
Source File: Capacity.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given the expected size, computes such a number N of items that
 * inserting (N+1) items will trigger resizing of the internal storage
 */
static int threshold(int size) throws Throwable {
    IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
    int initialCapacity = capacity(m);
    while (capacity(m) == initialCapacity)
        growUsingPut(m, 1);
    return m.size() - 1;
}
 
Example 7
Source File: Capacity.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given the expected size, computes such a number N of items that
 * inserting (N+1) items will trigger resizing of the internal storage
 */
static int threshold(int size) throws Throwable {
    IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
    int initialCapacity = capacity(m);
    while (capacity(m) == initialCapacity)
        growUsingPut(m, 1);
    return m.size() - 1;
}
 
Example 8
Source File: Capacity.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given the expected size, computes such a number N of items that
 * inserting (N+1) items will trigger resizing of the internal storage
 */
static int threshold(int size) throws Throwable {
    IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
    int initialCapacity = capacity(m);
    while (capacity(m) == initialCapacity)
        growUsingPut(m, 1);
    return m.size() - 1;
}
 
Example 9
Source File: HealthServiceImpl.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
int numWatchersForTest(String service) {
  synchronized (watchLock) {
    IdentityHashMap<StreamObserver<HealthCheckResponse>, Boolean> serviceWatchers =
        watchers.get(service);
    if (serviceWatchers == null) {
      return 0;
    }
    return serviceWatchers.size();
  }
}
 
Example 10
Source File: Capacity.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given the expected size, computes such a number N of items that
 * inserting (N+1) items will trigger resizing of the internal storage
 */
static int threshold(int size) throws Throwable {
    IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
    int initialCapacity = capacity(m);
    while (capacity(m) == initialCapacity)
        growUsingPut(m, 1);
    return m.size() - 1;
}
 
Example 11
Source File: Capacity.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given the expected size, computes such a number N of items that
 * inserting (N+1) items will trigger resizing of the internal storage
 */
static int threshold(int size) throws Throwable {
    IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
    int initialCapacity = capacity(m);
    while (capacity(m) == initialCapacity)
        growUsingPut(m, 1);
    return m.size() - 1;
}
 
Example 12
Source File: Capacity.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given the expected size, computes such a number N of items that
 * inserting (N+1) items will trigger resizing of the internal storage
 */
static int threshold(int size) throws Throwable {
    IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
    int initialCapacity = capacity(m);
    while (capacity(m) == initialCapacity)
        growUsingPut(m, 1);
    return m.size() - 1;
}
 
Example 13
Source File: Capacity.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given the expected size, computes such a number N of items that
 * inserting (N+1) items will trigger resizing of the internal storage
 */
static int threshold(int size) throws Throwable {
    IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
    int initialCapacity = capacity(m);
    while (capacity(m) == initialCapacity)
        growUsingPut(m, 1);
    return m.size() - 1;
}
 
Example 14
Source File: Capacity.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given the expected size, computes such a number N of items that
 * inserting (N+1) items will trigger resizing of the internal storage
 */
static int threshold(int size) throws Throwable {
    IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
    int initialCapacity = capacity(m);
    while (capacity(m) == initialCapacity)
        growUsingPut(m, 1);
    return m.size() - 1;
}
 
Example 15
Source File: Capacity.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given the expected size, computes such a number N of items that
 * inserting (N+1) items will trigger resizing of the internal storage
 */
static int threshold(int size) throws Throwable {
    IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
    int initialCapacity = capacity(m);
    while (capacity(m) == initialCapacity)
        growUsingPut(m, 1);
    return m.size() - 1;
}
 
Example 16
Source File: IdentityHashSet.java    From CrossMobile with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public boolean retainAll(Collection<?> elements) {
    IdentityHashMap<E, Object> newMap = new IdentityHashMap<E, Object>();

    for (Object element : elements) {
        if (map.containsKey(element))
            newMap.put((E) element, null);
    }

    boolean anyChanges = newMap.size() != map.size();

    map = newMap;
    return anyChanges;
}
 
Example 17
Source File: Capacity.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given the expected size, computes such a number N of items that
 * inserting (N+1) items will trigger resizing of the internal storage
 */
static int threshold(int size) throws Throwable {
    IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size);
    int initialCapacity = capacity(m);
    while (capacity(m) == initialCapacity)
        growUsingPut(m, 1);
    return m.size() - 1;
}
 
Example 18
Source File: OptimalCapacity.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Checks adequacy of the expected maximum size of a static field
 * of type {@code IdentityHashMap}.
 *
 * Having
 * <pre>
 * class XClass {
 *     static IdentityHashMap theMap = new IdentityHashMap(M);
 * }
 * </pre>
 *
 * you should call from the test
 *
 * <pre>
 * OptimalCapacity.ofIdentityHashMap(XClass.class, "theMap", M);
 * </pre>
 */
public static void ofIdentityHashMap(Class<?> clazz, String fieldName,
        int expectedMaxSize)
{
    try {
        Field field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        Object obj = field.get(null);
        if (!IdentityHashMap.class.equals(obj.getClass())) {
            throw new RuntimeException("'" + field +
                "' expected to be of type IdentityHashMap");
        }
        IdentityHashMap<?,?> map = (IdentityHashMap<?,?>)obj;

        // Check that size of map is what was expected
        if (map.size() != expectedMaxSize) {
            throw new RuntimeException("Size of '" + field +
                "' is " + map.size() +
                ", which differs from expected " + expectedMaxSize);
        }

        // Check that the map allocated only necessary amount of memory
        IdentityHashMap<Object, Object> tmp = new IdentityHashMap<>(map);
        if (internalArraySize(map) != internalArraySize(tmp)) {
            throw new RuntimeException("Final capacity of '" + field +
                "' is " + internalArraySize(map) +
                ", which exceeds necessary minimum " + internalArraySize(tmp));
        }

        // Check that map was initially properly sized
        tmp = new IdentityHashMap<>(expectedMaxSize);
        tmp.put(new Object(), new Object()); // trigger storage init
        if (internalArraySize(map) != internalArraySize(tmp)) {
            throw new RuntimeException("Requested number of elements in '" + field +
                "' was " + expectedMaxSize +
                ", which resulted in final capacity " + internalArraySize(tmp) +
                ", which differs from necessary minimum " + internalArraySize(map));
        }
    } catch (ReflectiveOperationException roe) {
        throw new RuntimeException(roe);
    }
}
 
Example 19
Source File: FacetProcessor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void handleFilterExclusions() throws IOException {
  List<String> excludeTags = freq.domain.excludeTags;

  if (excludeTags == null || excludeTags.size() == 0) {
    return;
  }

  @SuppressWarnings({"rawtypes"})
  Map tagMap = (Map) fcontext.req.getContext().get("tags");
  if (tagMap == null) {
    // no filters were tagged
    return;
  }

  IdentityHashMap<Query,Boolean> excludeSet = new IdentityHashMap<>();
  for (String excludeTag : excludeTags) {
    Object olst = tagMap.get(excludeTag);
    // tagMap has entries of List<String,List<QParser>>, but subject to change in the future
    if (!(olst instanceof Collection)) continue;
    for (Object o : (Collection<?>)olst) {
      if (!(o instanceof QParser)) continue;
      QParser qp = (QParser)o;
      try {
        excludeSet.put(qp.getQuery(), Boolean.TRUE);
      } catch (SyntaxError syntaxError) {
        // This should not happen since we should only be retrieving a previously parsed query
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, syntaxError);
      }
    }
  }
  if (excludeSet.size() == 0) return;

  List<Query> qlist = new ArrayList<>();

  // TODO: somehow remove responsebuilder dependency
  ResponseBuilder rb = SolrRequestInfo.getRequestInfo().getResponseBuilder();

  // add the base query
  if (!excludeSet.containsKey(rb.getQuery())) {
    qlist.add(rb.getQuery());
  }

  // add the filters
  if (rb.getFilters() != null) {
    for (Query q : rb.getFilters()) {
      if (!excludeSet.containsKey(q)) {
        qlist.add(q);
      }
    }
  }

  // now walk back up the context tree
  // TODO: we lose parent exclusions...
  for (FacetContext curr = fcontext; curr != null; curr = curr.parent) {
    if (curr.filter != null) {
      qlist.add( curr.filter );
    }
  }

  // recompute the base domain
  fcontext.base = fcontext.searcher.getDocSet(qlist);
}
 
Example 20
Source File: StatsField.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Computes a base {@link DocSet} for the current request to be used
 * when computing global stats for the local index.
 *
 * This is typically the same as the main DocSet for the {@link ResponseBuilder}
 * unless {@link CommonParams#TAG tag}ged filter queries have been excluded using 
 * the {@link CommonParams#EXCLUDE ex} local param
 */
public DocSet computeBaseDocSet() throws IOException {

  DocSet docs = rb.getResults().docSet;
  Map<?,?> tagMap = (Map<?,?>) rb.req.getContext().get("tags");

  if (excludeTagList.isEmpty() || null == tagMap) {
    // either the exclude list is empty, or there
    // aren't any tagged filters to exclude anyway.
    return docs;
  }

  IdentityHashMap<Query,Boolean> excludeSet = new IdentityHashMap<Query,Boolean>();
  for (String excludeTag : excludeTagList) {
    Object olst = tagMap.get(excludeTag);
    // tagMap has entries of List<String,List<QParser>>, but subject to change in the future
    if (!(olst instanceof Collection)) continue;
    for (Object o : (Collection<?>)olst) {
      if (!(o instanceof QParser)) continue;
      QParser qp = (QParser)o;
      try {
        excludeSet.put(qp.getQuery(), Boolean.TRUE);
      } catch (SyntaxError e) {
        // this shouldn't be possible since the request should have already
        // failed when attempting to execute the query, but just in case...
        throw new SolrException(ErrorCode.BAD_REQUEST, "Excluded query can't be parsed: " + 
                                originalParam + " due to: " + e.getMessage(), e);
      }
    }
  }
  if (excludeSet.size() == 0) return docs;
  
  List<Query> qlist = new ArrayList<Query>();
  
  // add the base query
  if (!excludeSet.containsKey(rb.getQuery())) {
    qlist.add(rb.getQuery());
  }
  
  // add the filters
  if (rb.getFilters() != null) {
    for (Query q : rb.getFilters()) {
      if (!excludeSet.containsKey(q)) {
        qlist.add(q);
      }
    }
  }
  
  // get the new base docset for this facet
  return searcher.getDocSet(qlist);
}