All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.unboundid.util.ResultCodeCounter Maven / Gradle / Ivy

Go to download

The UnboundID LDAP SDK for Java is a fast, comprehensive, and easy-to-use Java API for communicating with LDAP directory servers and performing related tasks like reading and writing LDIF, encoding and decoding data using base64 and ASN.1 BER, and performing secure communication. This package contains the Standard Edition of the LDAP SDK, which is a complete, general-purpose library for communicating with LDAPv3 directory servers.

There is a newer version: 7.0.1
Show newest version
/*
 * Copyright 2009-2018 Ping Identity Corporation
 * All Rights Reserved.
 */
/*
 * Copyright (C) 2009-2018 Ping Identity Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (GPLv2 only)
 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see .
 */
package com.unboundid.util;



import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import com.unboundid.ldap.sdk.ResultCode;



/**
 * This class provides a utility that may be used to count operation results and
 * categorize them based on the total number of results of each type.  It also
 * provides a method for retrieving result code counts, sorted by the number of
 * occurrences for each.
 */
@Mutable()
@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
public final class ResultCodeCounter
       implements Serializable
{
  /**
   * The serial version UID for this serializable class.
   */
  private static final long serialVersionUID = -2280620218815022241L;



  // The reference to the current map used to hold result code counts.
  private final AtomicReference> rcMap;



  /**
   * Creates a new instance of this result code counter.
   */
  public ResultCodeCounter()
  {
    rcMap = new AtomicReference<>();
    rcMap.set(new ConcurrentHashMap(
         StaticUtils.computeMapCapacity(ResultCode.values().length)));
  }



  /**
   * Increments the count for the provided result code.
   *
   * @param  resultCode  The result code for which to increment the count.
   */
  public void increment(final ResultCode resultCode)
  {
    increment(resultCode, 1);
  }



  /**
   * Increments the count for the provided result code by the specified amount.
   *
   * @param  resultCode  The result code for which to increment the count.
   * @param  amount      The amount by which to increment the count.
   */
  public void increment(final ResultCode resultCode, final int amount)
  {
    final ConcurrentHashMap m = rcMap.get();

    AtomicLong l = m.get(resultCode);
    if (l == null)
    {
      l = new AtomicLong(0L);
      final AtomicLong l2 = m.putIfAbsent(resultCode, l);
      if (l2 != null)
      {
        l = l2;
      }
    }

    l.addAndGet(amount);
  }



  /**
   * Clears all collected data from the result code counter.  Any
   * previously-collected data will be lost.
   */
  public void reset()
  {
    rcMap.set(new ConcurrentHashMap(
         StaticUtils.computeMapCapacity(ResultCode.values().length)));
  }



  /**
   * Retrieves a list of the result codes of each type along with their
   * respective counts.  The returned list will be sorted by number of
   * occurrences, from most frequent to least frequent.
   *
   * @param  reset  Indicates whether to clear the results after obtaining
   *                them.
   *
   * @return  A list of the result codes of each type along with their
   *          respective counts.
   */
  public List> getCounts(final boolean reset)
  {
    final ConcurrentHashMap m;
    if (reset)
    {
      m = rcMap.getAndSet(new ConcurrentHashMap(
           StaticUtils.computeMapCapacity(ResultCode.values().length)));
    }
    else
    {
      m = new ConcurrentHashMap<>(rcMap.get());
    }


    if (m.isEmpty())
    {
      return Collections.emptyList();
    }


    final TreeMap> sortedMap =
         new TreeMap<>(new ReverseComparator());
    for (final Map.Entry e : m.entrySet())
    {
      final long l = e.getValue().longValue();
      TreeMap rcByValue = sortedMap.get(l);
      if (rcByValue == null)
      {
        rcByValue = new TreeMap<>();
        sortedMap.put(l, rcByValue);
      }

      final ResultCode rc = e.getKey();
      rcByValue.put(rc.intValue(), rc);
    }


    final ArrayList> rcCounts =
         new ArrayList<>(2*sortedMap.size());
    for (final Map.Entry> e :
         sortedMap.entrySet())
    {
      final long count = e.getKey();
      for (final ResultCode rc : e.getValue().values())
      {
        rcCounts.add(new ObjectPair<>(rc, count));
      }
    }

    return Collections.unmodifiableList(rcCounts);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy