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); equal(a.length, size); check(b == c); check(b[size] == null); check(b[size + 1] == Boolean.TRUE); equal(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) equal(42, zz); } Iterator it = q.iterator(); for (int i = 0; i < size; i++) { check(it.hasNext()); Object x = it.next(); check(x == a[i]); check(x == b[i]); if (xx != null) check(x == yy[i]); } check(!it.hasNext()); }
protected final ShardSearchFailure[] buildShardFailures() { Queue<ShardSearchFailure> localFailures = shardFailures; if (localFailures == null) { return ShardSearchFailure.EMPTY_ARRAY; } return localFailures.toArray(ShardSearchFailure.EMPTY_ARRAY); }
/** * 将多个数组,合并成一个数组。如果这些数组为空,则返回 null * * @param arys 数组对象 * @return 合并后的数组对象 */ @SuppressWarnings("unchecked") public static <T> T[] merge(T[]... arys) { Queue<T> list = new LinkedList<T>(); for (T[] ary : arys) if (null != ary) for (T e : ary) if (null != e) list.add(e); if (list.isEmpty()) return null; Class<T> type = (Class<T>) list.peek().getClass(); return list.toArray((T[]) Array.newInstance(type, list.size())); }
public String toString() { Object[] t = queue.toArray(); StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < t.length; i++) { if (i != 0) sb.append(',').append(' '); sb.append(t[i]); } sb.append(']'); return sb.toString(); }
public static void test获取关键字上下文() throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream(new File("e:\\mvc-20160509.log")), "UTF-8")); String line = null; int range = 30, beforeFlag = 1, afterFlag = 0; String[] afterStrArr = new String[range]; boolean isContain = false; String matchLine = null, key = "0.12659082122436693][执行时间: 112063 毫秒"; Queue<String> beforeQueue = new ArrayBlockingQueue<String>(range); while ((line = br.readLine()) != null) { isContain = isContain ? isContain : line.contains(key); // 如果已经匹配到了就不在更新标志的值 if (!isContain) { if (beforeFlag >= range) { beforeQueue.remove(); // 移除队列中先进入的 } beforeQueue.add(line); // 加入队列 beforeFlag++; } else { matchLine = matchLine == null ? line : matchLine; // System.out.println(afterFlag); if (afterFlag < range) { afterStrArr[afterFlag++] = line; } else break; } } br.close(); Object[] beforeStrArr = beforeQueue.toArray(); for (int i = 0; i < beforeStrArr.length; i++) { System.out.println(beforeStrArr[i]); } System.out.println("---------------------"); System.out.println(matchLine); for (int i = 0; i < afterStrArr.length; i++) { System.out.println(afterStrArr[i]); } }
public static void dispatchEvents() { final Pair[] pairs; _lock.lock(); try { pairs = new Pair[_queue.size()]; _queue.toArray(pairs); _queue.clear(); } finally { _lock.unlock(); } for (final Pair p : pairs) { p.target.dispatchEvent(p.event); if (null != p.latch) { p.latch.countDown(); } if (null != p.callback) { p.callback.run(); } } }
public void verifyNoTrivialErrors() { Object[] receivedErrors = errors.toArray(); assertTrue(Arrays.toString(receivedErrors), receivedErrors.length == 0); }
public void CookItem(int i) { ItemStack cookingItemStack = fireItemStacks[i]; TECrucible crucibleTE = (worldObj.getTileEntity(xCoord, yCoord - 1, zCoord) instanceof TECrucible) ? (TECrucible) worldObj.getTileEntity(xCoord, yCoord - 1, zCoord) : null; /* * Only allow the ore to be smelted if there is a tuyere in the blast furnace, * a crucible below the blast furnace that isn't full, and the delay timer has completed. */ if (cookingItemStack != null && crucibleTE != null && crucibleTE.getTotalMetal() < crucibleTE.MAX_UNITS && storage[1] != null /*Tuyere*/ && cookDelay == 0) { Random R = new Random(); HeatRegistry manager = HeatRegistry.getInstance(); HeatIndex index = manager.findMatchingIndex(cookingItemStack); if (index != null && TFC_ItemHeat.GetTemp(cookingItemStack) >= index.meltTemp) { int output = 0; Item cookingItem = cookingItemStack.getItem(); if (cookingItem instanceof ISmeltable) { output = ((ISmeltable) cookingItem).GetMetalReturnAmount(cookingItemStack); // Attempt to add metal to crucible. if (!crucibleTE.addMetal( ((ISmeltable) cookingItem).GetMetalType(cookingItemStack), output)) return; // Do not decrease fuel or ore if the crucible is too full. } else { Metal m = MetalRegistry.instance.getMetalFromItem(cookingItem); output = index.getOutput(cookingItemStack, R).getItemDamage(); if (m != null) { // Attempt to add metal to crucible. if (!crucibleTE.addMetal(m, (short) (100 - output))) return; // Do not decrease fuel or ore if the crucible is too full. } } oreCount--; charcoalCount--; cookDelay = 100; // Five seconds (20 tps) until the next piece of ore can be smelted fireItemStacks[i] = null; // Delete cooked item /* * Treat fireItemStacks as a queue, and shift everything forward when an item is melted. * This way the hottest item is always in the first slot - Kitty */ Queue<ItemStack> buffer = new ArrayBlockingQueue<ItemStack>(fireItemStacks.length); for (ItemStack is : fireItemStacks) { if (is != null) { buffer.offer(is); } } fireItemStacks = buffer.toArray(new ItemStack[fireItemStacks.length]); // Damage the tuyere 1 point per item (not metal unit) smelted. storage[1].setItemDamage(storage[1].getItemDamage() + 1); if (storage[1] != null && storage[1].getItemDamage() == storage[1].getMaxDamage()) { setInventorySlotContents(1, null); } // Update crucible temperature crucibleTE.temperature = (int) fireTemp; } } }
@Test public void testSendDatagramWithoutCallback() throws Exception { // Single datagram sending Pipe readPipe = Pipe.open(); Pipe writePipe = Pipe.open(); GatheringByteChannel gatheringByteChannel = writePipe.sink(); ScatteringByteChannel scatteringByteChannel = readPipe.source(); SelectionKeyRegistrationReference registrationReference = (SelectionKeyRegistrationReference) _selectorIntraband.registerChannel(writePipe.source(), readPipe.sink()); Thread wakeUpThread = new Thread(new WakeUpRunnable(_selectorIntraband)); wakeUpThread.start(); Selector selector = _selectorIntraband.selector; synchronized (selector) { wakeUpThread.interrupt(); wakeUpThread.join(); Datagram requestDatagram = Datagram.createRequestDatagram(_type, _data); _selectorIntraband.sendDatagram(registrationReference, requestDatagram); SelectionKey writeSelectionKey = registrationReference.writeSelectionKey; ChannelContext channelContext = (ChannelContext) writeSelectionKey.attachment(); Queue<Datagram> sendingQueue = channelContext.getSendingQueue(); Assert.assertEquals(1, sendingQueue.size()); Assert.assertSame(requestDatagram, sendingQueue.peek()); } Datagram receiveDatagram = IntrabandTestUtil.readDatagramFully(scatteringByteChannel); Assert.assertEquals(_type, receiveDatagram.getType()); ByteBuffer dataByteBuffer = receiveDatagram.getDataByteBuffer(); Assert.assertArrayEquals(_data, dataByteBuffer.array()); // Two datagrams continuous sending Datagram requestDatagram1 = Datagram.createRequestDatagram(_type, _data); Datagram requestDatagram2 = Datagram.createRequestDatagram(_type, _data); wakeUpThread = new Thread(new WakeUpRunnable(_selectorIntraband)); wakeUpThread.start(); synchronized (selector) { wakeUpThread.interrupt(); wakeUpThread.join(); _selectorIntraband.sendDatagram(registrationReference, requestDatagram1); _selectorIntraband.sendDatagram(registrationReference, requestDatagram2); SelectionKey writeSelectionKey = registrationReference.writeSelectionKey; ChannelContext channelContext = (ChannelContext) writeSelectionKey.attachment(); Queue<Datagram> sendingQueue = channelContext.getSendingQueue(); Assert.assertEquals(2, sendingQueue.size()); Datagram[] datagrams = sendingQueue.toArray(new Datagram[2]); Assert.assertSame(requestDatagram1, datagrams[0]); Assert.assertSame(requestDatagram2, datagrams[1]); } Datagram receiveDatagram1 = IntrabandTestUtil.readDatagramFully(scatteringByteChannel); Assert.assertEquals(_type, receiveDatagram1.getType()); dataByteBuffer = receiveDatagram1.getDataByteBuffer(); Assert.assertArrayEquals(_data, dataByteBuffer.array()); Datagram receiveDatagram2 = IntrabandTestUtil.readDatagramFully(scatteringByteChannel); Assert.assertEquals(_type, receiveDatagram2.getType()); dataByteBuffer = receiveDatagram2.getDataByteBuffer(); Assert.assertArrayEquals(_data, dataByteBuffer.array()); // Two datagrams delay sending requestDatagram1 = Datagram.createRequestDatagram(_type, _data); requestDatagram2 = Datagram.createRequestDatagram(_type, _data); wakeUpThread = new Thread(new WakeUpRunnable(_selectorIntraband)); wakeUpThread.start(); SelectionKey writeSelectionKey = registrationReference.writeSelectionKey; ChannelContext channelContext = (ChannelContext) writeSelectionKey.attachment(); Queue<Datagram> sendingQueue = channelContext.getSendingQueue(); synchronized (writeSelectionKey) { synchronized (selector) { wakeUpThread.interrupt(); wakeUpThread.join(); _selectorIntraband.sendDatagram(registrationReference, requestDatagram1); Assert.assertEquals(1, sendingQueue.size()); Assert.assertSame(requestDatagram1, sendingQueue.peek()); } receiveDatagram1 = IntrabandTestUtil.readDatagramFully(scatteringByteChannel); Assert.assertEquals(_type, receiveDatagram1.getType()); dataByteBuffer = receiveDatagram1.getDataByteBuffer(); Assert.assertArrayEquals(_data, dataByteBuffer.array()); Thread pollingThread = _selectorIntraband.pollingThread; while (pollingThread.getState() == Thread.State.RUNNABLE) ; _selectorIntraband.sendDatagram(registrationReference, requestDatagram2); Assert.assertEquals(1, sendingQueue.size()); Assert.assertSame(requestDatagram2, sendingQueue.peek()); } receiveDatagram2 = IntrabandTestUtil.readDatagramFully(scatteringByteChannel); Assert.assertEquals(_type, receiveDatagram2.getType()); dataByteBuffer = receiveDatagram2.getDataByteBuffer(); Assert.assertArrayEquals(_data, dataByteBuffer.array()); // Huge datagram sending int hugeBufferSize = 1024 * 1024 * 10; ByteBuffer hugeBuffer = ByteBuffer.allocate(hugeBufferSize); for (int i = 0; i < hugeBufferSize; i++) { hugeBuffer.put(i, (byte) i); } _selectorIntraband.sendDatagram( registrationReference, Datagram.createRequestDatagram(_type, hugeBuffer)); receiveDatagram = DatagramHelper.createReceiveDatagram(); channelContext = (ChannelContext) writeSelectionKey.attachment(); int count = 0; while (!DatagramHelper.readFrom(receiveDatagram, scatteringByteChannel)) { count++; } Assert.assertTrue(count > 0); sendingQueue = channelContext.getSendingQueue(); Assert.assertTrue(sendingQueue.isEmpty()); dataByteBuffer = receiveDatagram.getDataByteBuffer(); Assert.assertArrayEquals(hugeBuffer.array(), dataByteBuffer.array()); unregisterChannels(registrationReference); gatheringByteChannel.close(); scatteringByteChannel.close(); }
@Override public <T> T[] toArray(T[] a) { return queue.toArray(a); }
@Override public Object[] toArray() { return queue.toArray(); }
public void testToArray_TArray() { forward.toArray(new String[0]); assertEquals("[toArray(Object[])]", getCalls()); }
public void testToArray() { forward.toArray(); assertEquals("[toArray]", getCalls()); }
public void clearResults() { log.debug("Clearing all results: {}", Arrays.deepToString(latestResults.toArray())); latestResults.clear(); }
public List<Vehicle> distForATime(Queue<Client> queue) { List<Vehicle> list = new LinkedList<Vehicle>(); List<Client> badClientList = new LinkedList<Client>(); Object[] clientList = queue.toArray(); Vehicle vehicle = new Vehicle(); vehicle.addAClient(clients[0]); list.add(vehicle); if (getRemainingRequireMents(list) > Vehicle.getMaxMaxLoad()) { vehicle.setLoad(Vehicle.getMaxMaxLoad()); } else { vehicle.setLoad(getRemainingRequireMents(list)); } for (int i = 0; i < clientList.length; i++) { Client client = (Client) clientList[i]; if (validateForAClient(vehicle, client)) { vehicle.addAClient(client); List<Client> addedList = new LinkedList<Client>(); for (Client client1 : badClientList) { if (validateForAClient(vehicle, client1)) { addedList.add(client1); vehicle.addAClient(client1); } } for (Client client1 : addedList) { badClientList.remove(client1); } } else { vehicle = new Vehicle(); list.add(vehicle); vehicle.addAClient(clients[0]); if (getRemainingRequireMents(list) > Vehicle.getMaxMaxLoad()) { vehicle.setLoad(Vehicle.getMaxMaxLoad()); } else { vehicle.setLoad(getRemainingRequireMents(list)); } if (!validateForAClient(vehicle, client)) { badClientList.add(client); } else { i = i - 1; } } } if (0 != badClientList.size()) { for (Client client : badClientList) { // System.out.println("bad client " + client); } return null; } for (Vehicle vehicle1 : list) { vehicle1.addAClient(clients[0]); } /* if (2 == list.size()) { return null; }*/ for (Object vehicle1 : list.toArray()) { if (((Vehicle) vehicle1).getClients().length == 2) { list.remove(vehicle1); } } return list; }