下面列出了java.util.ArrayDeque#toArray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressWarnings("unchecked")
private IEvent[] convertToArray(Object eventOrDeque)
{
if (eventOrDeque == null)
{
return null;
}
if (eventOrDeque instanceof IEvent)
{
return new IEvent[] {(IEvent)eventOrDeque};
}
ArrayDeque<IEvent> deque = (ArrayDeque<IEvent>)eventOrDeque;
return deque.toArray(new IEvent[deque.size()]);
}
static void arrayDequeStuff() {
ArrayDeque<Object> d = new ArrayDeque<>();
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.add(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.addFirst(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.addLast(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.offerFirst(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.offerLast(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.offer(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.push(null);
Object[] o = null;
// BUG: Diagnostic contains: passing @Nullable parameter 'o' where @NonNull is required
d.toArray(o);
// this should be fine
d.toArray();
}
@Test
public void testMessagesToInactive() throws XMPPException, TigaseStringprepException {
String recipient = "[email protected]";
JID recp1 = JID.jidInstanceNS(recipient + "/res1");
JID connId1 = JID.jidInstanceNS("[email protected]/recipient1-res1");
XMPPResourceConnection session1 = getSession(connId1, recp1);
enableCSI(session1);
ArrayDeque<Packet> results = new ArrayDeque<>();
Packet p = Packet.packetInstance("message", "[email protected]/res1", recp1.toString(), StanzaType.chat);
p.setPacketTo(connId1);
results.offer(p);
Packet[] expected = results.toArray(new Packet[results.size()]);
csi.filter(p, session1, null, results);
Packet[] processed = results.toArray(new Packet[results.size()]);
Assert.assertArrayEquals(expected, processed);
}
@Test
public void testPresenceToInactive() throws XMPPException, TigaseStringprepException {
String recipient = "[email protected]";
JID recp1 = JID.jidInstanceNS(recipient + "/res1");
JID connId1 = JID.jidInstanceNS("[email protected]/recipient1-res1");
XMPPResourceConnection session1 = getSession(connId1, recp1);
enableCSI(session1);
ArrayDeque<Packet> results = new ArrayDeque<>();
Packet p = Packet.packetInstance("presence", "[email protected]/res1", recp1.toString(), StanzaType.available);
p.setPacketTo(connId1);
results.offer(p);
Packet[] expected = new Packet[0];
csi.filter(p, session1, null, results);
Packet[] processed = results.toArray(new Packet[results.size()]);
Assert.assertArrayEquals(expected, processed);
}
@TruffleBoundary
public static Object[] allInstancesOf(final SqueakImageContext image, final ClassObject classObj) {
final ArrayDeque<AbstractSqueakObjectWithHash> result = new ArrayDeque<>();
final ObjectTracer pending = new ObjectTracer(image);
AbstractSqueakObjectWithHash currentObject;
while ((currentObject = pending.getNextPending()) != null) {
if (currentObject.tryToMark(pending.getCurrentMarkingFlag())) {
if (classObj == currentObject.getSqueakClass()) {
result.add(currentObject);
}
pending.tracePointers(currentObject);
}
}
return result.toArray();
}
/**
* toArray(null) throws NullPointerException
*/
public void testToArray_NullArg() {
ArrayDeque l = new ArrayDeque();
l.add(new Object());
try {
l.toArray(null);
shouldThrow();
} catch (NullPointerException success) {}
}
void checkToArray(ArrayDeque q) {
int size = q.size();
Object[] o = q.toArray();
assertEquals(size, o.length);
Iterator it = q.iterator();
for (int i = 0; i < size; i++) {
Integer x = (Integer) it.next();
assertEquals((Integer)o[0] + i, (int) x);
assertSame(o[i], x);
}
}
void checkToArray2(ArrayDeque q) {
int size = q.size();
Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
Integer[] a2 = new Integer[size];
Integer[] a3 = new Integer[size + 2];
if (size > 0) Arrays.fill(a1, 42);
Arrays.fill(a2, 42);
Arrays.fill(a3, 42);
Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
Integer[] b2 = (Integer[]) q.toArray(a2);
Integer[] b3 = (Integer[]) q.toArray(a3);
assertSame(a2, b2);
assertSame(a3, b3);
Iterator it = q.iterator();
for (int i = 0; i < size; i++) {
Integer x = (Integer) it.next();
assertSame(b1[i], x);
assertEquals(b1[0] + i, (int) x);
assertSame(b2[i], x);
assertSame(b3[i], x);
}
assertNull(a3[size]);
assertEquals(42, (int) a3[size + 1]);
if (size > 0) {
assertNotSame(a1, b1);
assertEquals(size, b1.length);
for (int i = 0; i < a1.length; i++) {
assertEquals(42, (int) a1[i]);
}
}
}
/**
* toArray(null) throws NullPointerException
*/
public void testToArray_NullArg() {
ArrayDeque l = new ArrayDeque();
l.add(new Object());
try {
l.toArray(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* toArray(incompatible array type) throws ArrayStoreException
*/
public void testToArray1_BadArg() {
ArrayDeque l = new ArrayDeque();
l.add(new Integer(5));
try {
l.toArray(new String[10]);
shouldThrow();
} catch (ArrayStoreException success) {}
}
private void addCompounds(final ArrayDeque<Term> terms, final boolean reverse) throws IOException {
final CombineSuggestion[] combinations = suggestCombination(reverse
? terms.descendingIterator() : terms.iterator());
if (combinations != null && combinations.length > 0) {
final Term[] termArray;
if (reverse) {
termArray = new Term[terms.size()];
int i = terms.size() - 1;
final Iterator<Term> termIterator = terms.descendingIterator();
while (termIterator.hasNext()) {
termArray[i--] = termIterator.next();
}
} else {
termArray = terms.toArray(new Term[0]);
}
for (final CombineSuggestion suggestion : combinations) {
// add compound to each sibling that is part of the compound to maintain mm logic
Arrays.stream(suggestion.originalTermIndexes)
.mapToObj(idx -> termArray[idx])
.forEach(sibling -> nodesToAdd.add(
new Term(sibling.getParent(), sibling.getField(), suggestion.suggestion.string, true)));
}
}
}
@Test
public void testFlushStopping() throws XMPPException, TigaseStringprepException {
String recipient = "[email protected]";
JID recp1 = JID.jidInstanceNS(recipient + "/res1");
JID connId1 = JID.jidInstanceNS("[email protected]/recipient1-res1");
XMPPResourceConnection session1 = getSession(connId1, recp1);
enableCSI(session1);
ArrayDeque<Packet> results = new ArrayDeque<>();
Packet p = Packet.packetInstance("presence", "[email protected]/res1", recp1.toString(), StanzaType.available);
p.setPacketTo(connId1);
csi.filter(p, session1, null, results);
results.clear();
Packet m = Packet.packetInstance("message", "[email protected]/res1", recp1.toString(), StanzaType.chat);
m.getElement().addChild(new Element("received", new String[]{ "xmlns" }, new String[] { "urn:xmpp:receipts" }));
m.setPacketTo(connId1);
results.offer(m);
csi.filter(m, session1, null, results);
results.clear();
results.offer(m);
results.clear();
csi.stopped(session1, results, new HashMap<>());
Packet[] processed = results.toArray(new Packet[results.size()]);
Assert.assertEquals(0, processed.length);
}
/**
* Returns an array of class objects representing the entire class hierarchy
* with the most-super class as the first element followed by all subclasses
* in the order they are declared. This method does not include the generic
* Object type in its list. If this class represents the Object type, this
* method will return a zero-size array.
*/
public static Class<?>[] getClassHierarchy(Class<?> type) {
ArrayDeque<Class<?>> classes = new ArrayDeque<Class<?>>();
// class to start our search from, we'll loop thru the entire class hierarchy
Class<?> classType = type;
// keep searching up until we reach an Object class type
while (classType != null && !classType.equals(Object.class)) {
// keep adding onto front
classes.addFirst(classType);
classType = classType.getSuperclass();
}
return classes.toArray(new Class[0]);
}
@SuppressWarnings({"unchecked"})
private void endLocationPath(int scope, ArrayDeque steps){
LocationPath path = new LocationPath(scope, steps.size());
steps.toArray(path.steps);
push(LocationPathAnalyzer.simplify(path));
}
private Object getUniqueID(TreePath path, Node node, ArrayDeque<? super String> deque) {
deque.addFirst(node.getName());
Object object = path.getLastPathComponent();
TreePath parent = path.getParentPath();
return parent != null && object instanceof Node ? getUniqueID(parent, (Node)object, deque) : parent != null || object != state ? null : deque.toArray();
}