com.helger.commons.concurrent.collector.ConcurrentCollectorMultiple Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ph-commons Show documentation
Show all versions of ph-commons Show documentation
Java 1.8+ Library with tons of utility classes required in all projects
/*
* Copyright (C) 2014-2022 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.helger.commons.concurrent.collector;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.collection.impl.CommonsArrayList;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.equals.EqualsHelper;
import com.helger.commons.lang.GenericReflection;
import com.helger.commons.state.ESuccess;
/**
* Concurrent collector that performs action on multiple objects at once
*
* @author Philip Helger
* @param
* The type of the objects in the queue.
*/
public class ConcurrentCollectorMultiple extends AbstractConcurrentCollector
{
/** The default number of objects to be put in the queue for execution. */
public static final int DEFAULT_MAX_PERFORM_COUNT = DEFAULT_MAX_QUEUE_SIZE / 2;
private static final Logger LOGGER = LoggerFactory.getLogger (ConcurrentCollectorMultiple.class);
@Nonnegative
private final int m_nMaxPerformCount;
private IConcurrentPerformer > m_aPerformer;
/**
* Constructor that uses {@link #DEFAULT_MAX_QUEUE_SIZE} elements as the
* maximum queue length and {@link #DEFAULT_MAX_PERFORM_COUNT} as the max
* perform count.
*/
public ConcurrentCollectorMultiple ()
{
this (DEFAULT_MAX_QUEUE_SIZE, DEFAULT_MAX_PERFORM_COUNT);
}
/**
* Constructor.
*
* @param nMaxQueueSize
* The maximum number of items that can be in the queue. Must be >
* 0.
* @param nMaxPerformCount
* The maximum number of objects to be put in the queue for execution.
* Must be > 0.
*/
public ConcurrentCollectorMultiple (@Nonnegative final int nMaxQueueSize, @Nonnegative final int nMaxPerformCount)
{
super (nMaxQueueSize);
ValueEnforcer.isGT0 (nMaxPerformCount, "MaxPerformCount");
ValueEnforcer.isTrue (nMaxPerformCount <= nMaxQueueSize,
() -> "max perform size is illegal " +
nMaxPerformCount +
" - must be <= queue size " +
nMaxQueueSize);
m_nMaxPerformCount = nMaxPerformCount;
}
/**
* Constructor using an existing {@link BlockingQueue} and the default max
* perform count ({@value #DEFAULT_MAX_PERFORM_COUNT}).
*
* @param aQueue
* {@link BlockingQueue} to use. May not be null
.
*/
public ConcurrentCollectorMultiple (@Nonnull final BlockingQueue