下面列出了java.util.Queue#toArray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main ( final String[] args ) throws Exception
{
if ( args.length == 0 )
{
throw new RuntimeException ( "syntax: DaemonStarter <daemon class name>" );
}
final Queue<String> argList = new LinkedList<String> ();
argList.addAll ( Arrays.asList ( args ) );
new DaemonStarter ( Class.forName ( argList.poll () ), argList.toArray ( new String[0] ) );
while ( true )
{
Thread.sleep ( 1000 );
}
}
@Test
public void toArray() throws Exception {
Queue<Integer> queue = new ConcurrentEvictingQueue<>(5);
Object[] objects = queue.toArray();
assertThat(objects.length).isEqualTo(0);
queue.add(1);
assertThat(queue.toArray()).containsExactly(1);
queue.clear();
queue.addAll(asList(1, 2, 3, 4, 5));
assertThat(queue.toArray()).containsExactly(1, 2, 3, 4, 5);
queue.clear();
assertThat(queue).isEmpty();
queue.addAll(asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
assertThat(queue.toArray()).containsExactly(5, 6, 7, 8, 9);
queue.clear();
assertThat(queue).isEmpty();
}
private void populateMetrics(ChartModel cm, Queue<FlowInfo> flowInfoQ) {
FlowInfo[] flowInfos = flowInfoQ.toArray(new FlowInfo[flowInfoQ.size()]);
SimpleDateFormat form = new SimpleDateFormat(CHART_TIME_FORMAT);
int timeOffset = 0;
Integer dataPointCount = PERIOD_OPTION_MAP.get(currentPeriod);
if (dataPointCount != null) {
timeOffset = flowInfos.length - (int) dataPointCount;
if (timeOffset < 0) {
timeOffset = 0;
}
}
for (int idx = timeOffset; idx < flowInfos.length; idx++) {
Map<String, Object> local = Maps.newHashMap();
local.put(LABEL, form.format(new Date(flowInfos[idx].statsInfo().fstPktArrTime())));
local.put(STAT_CURR_ACC_PACKET, flowInfos[idx].statsInfo().currAccPkts());
local.put(STAT_PREV_ACC_PACKET, flowInfos[idx].statsInfo().prevAccPkts());
local.put(STAT_CURR_ACC_BYTE, flowInfos[idx].statsInfo().currAccBytes());
local.put(STAT_PREV_ACC_BYTE, flowInfos[idx].statsInfo().prevAccBytes());
local.put(STAT_ERROR_PACKET, flowInfos[idx].statsInfo().errorPkts());
local.put(STAT_DROP_PACKET, flowInfos[idx].statsInfo().dropPkts());
populateMetric(cm.addDataPoint(flowInfos[idx].uniqueFlowInfoKey()), local);
}
}
private String getChooserJob() {
Worker worker = (Worker) getInstanceField("worker", chooser);
ConcurrentQ<?, ?> cq = (ConcurrentQ<?, ?>) getInstanceField("concurrentQ", worker);
Queue<?> q = (Queue<?>) getInstanceField("queue", cq);
Object[] jobs = q.toArray();
return Arrays.toString(jobs);
}
@Override
public String[] getSupportedActions() {
final Set<NativeBundleType> nbts = NativeBundleType.getSupported();
final Queue<String> res = new ArrayDeque<>(nbts.size());
for (NativeBundleType nbt : nbts) {
res.add(nbt.getCommand());
}
return res.toArray(new String[res.size()]);
}
void checkIterationSanity(Queue q) {
if (rnd.nextBoolean())
return;
int size = q.size();
Object[] a = q.toArray();
Object[] b = new Object[size+2];
Arrays.fill(b, Boolean.TRUE);
Object[] c = q.toArray(b);
assertEquals(a.length, size);
assertSame(b, c);
assertNull(b[size]);
assertSame(b[size+1], Boolean.TRUE);
assertEquals(q.toString(), Arrays.toString(a));
Integer[] xx = null, yy = null;
if (size > 0) {
xx = new Integer[size - 1];
Arrays.fill(xx, 42);
yy = ((Queue<Integer>)q).toArray(xx);
for (Integer zz : xx)
assertEquals(42, (int) zz);
}
Iterator it = q.iterator();
for (int i = 0; i < size; i++) {
if (rnd.nextBoolean()) assertTrue(it.hasNext());
Object x = it.next();
assertSame(x, a[i]);
assertSame(x, b[i]);
if (xx != null) assertSame(x, yy[i]);
}
if (rnd.nextBoolean()) assertTrue(!it.hasNext());
}
private void resizeOnce(int capacity) {
int nextNumberToPut = 0;
Queue<Integer> referenceQueue = new ArrayBlockingQueue<>(capacity);
CircularBuffer<Integer> buffer = new CircularBuffer<>(capacity);
// Fill full, so the next add will wrap
for (int i = 0; i < capacity; i++, nextNumberToPut++) {
buffer.add(nextNumberToPut);
referenceQueue.add(nextNumberToPut);
}
int gets = r.nextInt(capacity); // [0, capacity)
for (int i = 0; i < gets; i++) {
referenceQueue.poll();
buffer.remove();
}
int puts = r.nextInt(gets + 1); // [0, gets]
for (int i = 0; i < puts; i++, nextNumberToPut++) {
buffer.add(nextNumberToPut);
referenceQueue.add(nextNumberToPut);
}
Integer[] expected = referenceQueue.toArray(new Integer[0]);
buffer.resize(expected.length);
assertEquals(buffer.elements, expected);
}
@Test
public void emptyOneQueueShouldConvertToArrayAndPutNullMarkerAndReuseInputArrayOnWhenPassedOneLengthArray() {
Queue<Integer> q = emptyOneQueue();
//and
Integer[] passedArray = new Integer[1];
//when
Integer[] convertedArray = q.toArray(passedArray);
//then
assertThat(convertedArray)
.containsExactly((Integer)null)
.isSameAs(passedArray);
}
@Test
public void oneQueueWithOneElementShouldConvertToArrayAndReuseInputArrayWhenPassedOneLengthArray() {
Queue<Integer> q = oneQueueWithTestElement(TEST_ELEMENT);
//and
Integer[] passedArray = new Integer[1];
//when
Integer[] convertedArray = q.toArray(passedArray);
//then
assertThat(convertedArray)
.containsExactly(TEST_ELEMENT)
.isSameAs(passedArray);
}
@Test
public void emptyOneQueueShouldConvertToArrayAndPutNullMarkerAndReuseInputArrayWhenPassedLargerArray() {
//given
Queue<Integer> q = emptyOneQueue();
//and
Integer[] passedArray = {1, 2, 3};
//when
Integer[] convertedArray = q.toArray(passedArray);
//then
assertThat(convertedArray)
.hasSize(3)
.startsWith(null, 2, 3)
.isSameAs(passedArray);
}
@Test
public void oneQueueWithOneElementShouldConvertToArrayAndPutNullMarkerAndReuseInputArrayWhenPassedLargerArray() {
Queue<Integer> q = oneQueueWithTestElement(TEST_ELEMENT);
//and
Integer[] passedArray = {1, 2, 3};
//given
Integer[] convertedArray = q.toArray(passedArray);
//then
assertThat(convertedArray)
.hasSize(3)
.startsWith(TEST_ELEMENT, null, 3)
.isSameAs(passedArray);
}
@Test
public void toPreAllocatedArray() throws Exception {
Queue<Integer> queue = new ConcurrentEvictingQueue<>(5);
Integer[] emptyArray = queue.toArray(new Integer[]{});
assertThat(emptyArray.length).isEqualTo(0);
queue.add(1);
assertThat(queue.toArray()).containsExactly(1);
queue.clear();
queue.addAll(asList(1, 2, 3, 4, 5));
Integer[] first = new Integer[5];
queue.toArray(first);
assertThat(first).containsExactly(1, 2, 3, 4, 5);
Integer[] second = new Integer[7];
queue.toArray(second);
assertThat(second).containsExactly(1, 2, 3, 4, 5, null, null);
Integer[] third = new Integer[2];
Integer[] thirdResult = queue.toArray(third);
assertThat(third).containsExactly(null, null);
assertThat(thirdResult).containsExactly(1, 2, 3, 4, 5);
queue.clear();
assertThat(queue).isEmpty();
queue.addAll(asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
Integer[] fourth = {11, 22, 33, 44, 55, 66, 77, 88};
assertThat(queue.toArray(fourth)).containsExactly(5, 6, 7, 8, 9, 66, 77, 88);
queue.clear();
assertThat(queue).isEmpty();
}
/**
* Returns an array of all SQL exceptions collected by {@link #addException}. This method will be
* called when a pooled Connection is closed, in order to determine whether the underlying (raw)
* JDBC Connection also needs to be closed.
*/
final SQLException[] getExceptions() {
Queue<SQLException> ex = exceptions;
if (ex == null) {
return NO_EXCEPTIONS;
}
return ex.toArray(NO_EXCEPTIONS);
}
/**
* @param request
* @param queue
* @param attributeName
*/
private void pruneQueueIfNeeded(WebRequest request, Queue<String> queue, String attributeName) {
// now check to see if we have hit the limit of conversations for the
// command name.
if (queue.size() > getNumConversationsToKeep()) {
if (_logger.isDebugEnabled()) {
for (Object str : queue.toArray()) {
_logger.debug("pruneQueueIfNeeded - (" + attributeName +
") queue entry (" + str + " " + new java.util.Date(Long.parseLong((String)str)));
}
}
// grab the next item to be removed.
String conversationId = queue.peek();
if (conversationId != null) {
_logger.debug("pruneQueueIfNeeded - (" + attributeName +
") removed (" + conversationId + " " + new java.util.Date(
Long.parseLong(conversationId)));
// remove the reference object from the session.
removeEntityFromSession(request, attributeName, conversationId);
}
}
}
@Test(dataProvider = "empty,singleton,populated")
public void toArray(Queue<Integer> queue) {
Object[] expect = new ArrayList<>(queue).toArray();
Object[] actual = queue.toArray();
assertThat(actual, queue.isEmpty() ? emptyArray() : arrayContaining(expect));
}
@Test(dataProvider = "empty,singleton,populated")
public void toTypedArray(Queue<Integer> queue) {
Integer[] expect = new ArrayList<>(queue).toArray(new Integer[] {});
Integer[] actual = queue.toArray(new Integer[] {});
assertThat(actual, queue.isEmpty() ? emptyArray() : arrayContaining(expect));
}
private FlowInfo getLatestFlowInfo(Queue<FlowInfo> flowInfoQ) {
FlowInfo[] flowInfos = flowInfoQ.toArray(new FlowInfo[flowInfoQ.size()]);
return flowInfos[flowInfos.length - 1];
}