1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 01:29:38 +02:00

Merge branch '4.3'

This commit is contained in:
Florian Schmaus 2018-06-12 15:49:43 +02:00
commit f290197f6a
20 changed files with 1149 additions and 424 deletions

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2003-2007 Jive Software, 2016-2017 Florian Schmaus.
* Copyright 2003-2007 Jive Software, 2016-2018 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,8 @@
package org.jivesoftware.smack;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
@ -55,7 +57,7 @@ public class StanzaCollector {
private final Stanza request;
private boolean cancelled = false;
private volatile boolean cancelled = false;
/**
* Creates a new stanza collector. If the stanza filter is <tt>null</tt>, then
@ -268,6 +270,27 @@ public class StanzaCollector {
return result;
}
private List<Stanza> collectedCache;
/**
* Return a list of all collected stanzas. This method must be invoked after the collector has been cancelled.
*
* @return a list of collected stanzas.
* @since 4.3.0
*/
public List<Stanza> getCollectedStanzasAfterCancelled() {
if (!cancelled) {
throw new IllegalStateException("Stanza collector was not yet cancelled");
}
if (collectedCache == null) {
collectedCache = new ArrayList<>(getCollectedCount());
resultQueue.drainTo(collectedCache);
}
return collectedCache;
}
/**
* Get the number of collected stanzas this stanza collector has collected so far.
*
@ -300,7 +323,7 @@ public class StanzaCollector {
private void throwIfCancelled() {
if (cancelled) {
throw new IllegalStateException("Packet collector already cancelled");
throw new IllegalStateException("Stanza collector already cancelled");
}
}

View file

@ -0,0 +1,94 @@
/**
*
* Copyright the original author or authors
*
* 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 org.jivesoftware.smack.util;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;
/**
* Extends a {@link HashMap} with {@link WeakReference} values, so that
* weak references which have been cleared are periodically removed from
* the map. The cleaning occurs as part of {@link #put}, after a specific
* number ({@link #cleanInterval}) of calls to {@link #put}.
*
* @param <K> The key type.
* @param <V> The value type.
*
* @author Boris Grozev
*/
public class CleaningWeakReferenceMap<K, V>
extends HashMap<K, WeakReference<V>> {
private static final long serialVersionUID = 0L;
/**
* The number of calls to {@link #put} after which to clean this map
* (i.e. remove cleared {@link WeakReference}s from it).
*/
private final int cleanInterval;
/**
* The number of times {@link #put} has been called on this instance
* since the last time it was {@link #clean}ed.
*/
private int numberOfInsertsSinceLastClean = 0;
/**
* Initializes a new {@link CleaningWeakReferenceMap} instance with the
* default clean interval.
*/
public CleaningWeakReferenceMap() {
this(50);
}
/**
* Initializes a new {@link CleaningWeakReferenceMap} instance with a given
* clean interval.
* @param cleanInterval the number of calls to {@link #put} after which the
* map will clean itself.
*/
public CleaningWeakReferenceMap(int cleanInterval) {
this.cleanInterval = cleanInterval;
}
@Override
public WeakReference<V> put(K key, WeakReference<V> value) {
WeakReference<V> ret = super.put(key, value);
if (numberOfInsertsSinceLastClean++ > cleanInterval) {
numberOfInsertsSinceLastClean = 0;
clean();
}
return ret;
}
/**
* Removes all cleared entries from this map (i.e. entries whose value
* is a cleared {@link WeakReference}).
*/
private void clean() {
Iterator<Entry<K, WeakReference<V>>> iter = entrySet().iterator();
while (iter.hasNext()) {
Entry<K, WeakReference<V>> e = iter.next();
if (e != null && e.getValue() != null
&& e.getValue().get() == null) {
iter.remove();
}
}
}
}

View file

@ -451,6 +451,16 @@ public class StringUtils {
return cs;
}
public static <CS extends CharSequence> CS requireNullOrNotEmpty(CS cs, String message) {
if (cs == null) {
return null;
}
if (cs.toString().isEmpty()) {
throw new IllegalArgumentException(message);
}
return cs;
}
/**
* Return the String representation of the given char sequence if it is not null.
*

View file

@ -75,7 +75,9 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
* @return the XmlStringBuilder
*/
public XmlStringBuilder element(String name, String content) {
assert content != null;
if (content.isEmpty()) {
return emptyElement(name);
}
openElement(name);
escape(content);
closeElement(name);

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2014 Florian Schmaus
* Copyright 2014-2018 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,10 +26,14 @@ import org.xml.sax.SAXException;
public class XmlUnitUtils {
// TOOD: Remove this method.
public static void assertSimilar(CharSequence expected, CharSequence actual) throws SAXException, IOException {
assertXmlSimilar(expected, actual);
}
public static void assertXmlSimilar(CharSequence expected, CharSequence actual) throws SAXException, IOException {
Diff diff = new Diff(expected.toString(), actual.toString());
diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
assertXMLEqual(diff, true);
}
}