1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-22 20:47:57 +01:00

Add StringUtils.requireNotNullOrEmpty and Objects.requireNonNull

and use this in a few places.
This commit is contained in:
Florian Schmaus 2015-01-26 20:36:55 +01:00
parent 603f64166b
commit 86ea027301
18 changed files with 88 additions and 100 deletions

View file

@ -75,6 +75,7 @@ import org.jivesoftware.smack.parsing.UnparsablePacket;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.DNSUtil;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.util.SmackExecutorThreadFactory;
@ -469,8 +470,8 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
*/
public synchronized void login(CharSequence username, String password, String resource) throws XMPPException,
SmackException, IOException {
if (!config.allowNullOrEmptyUsername && StringUtils.isNullOrEmpty(username)) {
throw new IllegalArgumentException("Username must not be null or empty");
if (!config.allowNullOrEmptyUsername) {
StringUtils.requireNotNullOrEmpty(username, "Username must not be null or empty");
}
throwNotConnectedExceptionIfAppropriate();
throwAlreadyLoggedInExceptionIfAppropriate();
@ -618,9 +619,8 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
@Override
public void sendPacket(Packet packet) throws NotConnectedException {
if (packet == null) {
throw new IllegalArgumentException("Packet must not be null");
}
Objects.requireNonNull(packet, "Packet must not be null");
throwNotConnectedExceptionIfAppropriate();
switch (fromMode) {
case OMITTED:
@ -1419,17 +1419,12 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
public void sendStanzaWithResponseCallback(Packet stanza, PacketFilter replyFilter,
final PacketListener callback, final ExceptionCallback exceptionCallback,
long timeout) throws NotConnectedException {
if (stanza == null) {
throw new IllegalArgumentException("stanza must not be null");
}
if (replyFilter == null) {
// While Smack allows to add PacketListeners with a PacketFilter value of 'null', we
// disallow it here in the async API as it makes no sense
throw new IllegalArgumentException("replyFilter must not be null");
}
if (callback == null) {
throw new IllegalArgumentException("callback must not be null");
}
Objects.requireNonNull(stanza, "stanza must not be null");
// While Smack allows to add PacketListeners with a PacketFilter value of 'null', we
// disallow it here in the async API as it makes no sense
Objects.requireNonNull(replyFilter, "replyFilter must not be null");
Objects.requireNonNull(callback, "callback must not be null");
final PacketListener packetListener = new PacketListener() {
@Override
public void processPacket(Packet packet) throws NotConnectedException {

View file

@ -18,14 +18,15 @@ package org.jivesoftware.smack;
import java.lang.ref.WeakReference;
import org.jivesoftware.smack.util.Objects;
public abstract class Manager {
final WeakReference<XMPPConnection> weakConnection;
public Manager(XMPPConnection connection) {
if (connection == null) {
throw new IllegalArgumentException("XMPPConnection must not be null");
}
Objects.requireNonNull(connection, "XMPPConnection must not be null");
weakConnection = new WeakReference<XMPPConnection>(connection);
}

View file

@ -22,6 +22,7 @@ import java.util.Arrays;
import java.util.List;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.Objects;
/**
* Implements the logical AND operation over two or more packet filters.
@ -50,13 +51,9 @@ public class AndFilter implements PacketFilter {
* @param filters the filters to add.
*/
public AndFilter(PacketFilter... filters) {
if (filters == null) {
throw new IllegalArgumentException("Parameter must not be null.");
}
Objects.requireNonNull(filters, "Parameter must not be null.");
for(PacketFilter filter : filters) {
if(filter == null) {
throw new IllegalArgumentException("Parameter must not be null.");
}
Objects.requireNonNull(filter, "Parameter must not be null.");
}
this.filters = new ArrayList<PacketFilter>(Arrays.asList(filters));
}
@ -68,9 +65,7 @@ public class AndFilter implements PacketFilter {
* @param filter a filter to add to the filter list.
*/
public void addFilter(PacketFilter filter) {
if (filter == null) {
throw new IllegalArgumentException("Parameter must not be null.");
}
Objects.requireNonNull(filter, "Parameter must not be null.");
filters.add(filter);
}

View file

@ -22,6 +22,7 @@ import java.util.Arrays;
import java.util.List;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.Objects;
/**
* Implements the logical OR operation over two or more packet filters. In
@ -50,13 +51,9 @@ public class OrFilter implements PacketFilter {
* @param filters the filters to add.
*/
public OrFilter(PacketFilter... filters) {
if (filters == null) {
throw new IllegalArgumentException("Parameter must not be null.");
}
Objects.requireNonNull(filters, "Parameter must not be null.");
for(PacketFilter filter : filters) {
if(filter == null) {
throw new IllegalArgumentException("Parameter must not be null.");
}
Objects.requireNonNull(filter, "Parameter must not be null.");
}
this.filters = new ArrayList<PacketFilter>(Arrays.asList(filters));
}
@ -68,9 +65,7 @@ public class OrFilter implements PacketFilter {
* @param filter a filter to add to the filter list.
*/
public void addFilter(PacketFilter filter) {
if (filter == null) {
throw new IllegalArgumentException("Parameter must not be null.");
}
Objects.requireNonNull(filter, "Parameter must not be null.");
filters.add(filter);
}

View file

@ -40,9 +40,8 @@ public class PacketExtensionFilter implements PacketFilter {
* @param namespace the XML namespace of the packet extension.
*/
public PacketExtensionFilter(String elementName, String namespace) {
if (StringUtils.isNullOrEmpty(namespace)) {
throw new IllegalArgumentException("namespace must not be null or empty");
}
StringUtils.requireNotNullOrEmpty(namespace, "namespace must not be null or empty");
this.elementName = elementName;
this.namespace = namespace;
}

View file

@ -44,9 +44,7 @@ public class PacketIDFilter implements PacketFilter {
* @param packetID the packet ID to filter for.
*/
public PacketIDFilter(String packetID) {
if (StringUtils.isNullOrEmpty(packetID)) {
throw new IllegalArgumentException("Packet ID must not be null or empty.");
}
StringUtils.requireNotNullOrEmpty(packetID, "Packet ID must not be null or empty.");
this.packetID = packetID;
}

View file

@ -36,9 +36,7 @@ public class ThreadFilter implements PacketFilter {
* @param thread the thread value to filter for.
*/
public ThreadFilter(String thread) {
if (StringUtils.isNullOrEmpty(thread)) {
throw new IllegalArgumentException("Thread must not be null or empty.");
}
StringUtils.requireNotNullOrEmpty(thread, "Thread must not be null or empty.");
this.thread = thread;
}

View file

@ -16,6 +16,8 @@
*/
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.Objects;
public class ErrorIQ extends SimpleIQ {
public static final String ELEMENT = XMPPError.ERROR;
@ -29,9 +31,7 @@ public class ErrorIQ extends SimpleIQ {
*/
public ErrorIQ(XMPPError xmppError) {
super(ELEMENT, null);
if (xmppError == null) {
throw new IllegalArgumentException("XMPPError must not be null");
}
Objects.requireNonNull(xmppError, "XMPPError must not be null");
setType(IQ.Type.error);
setError(xmppError);
}

View file

@ -19,6 +19,7 @@ package org.jivesoftware.smack.packet;
import java.util.Locale;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
@ -84,10 +85,7 @@ public abstract class IQ extends Packet {
* @param type the type of the IQ packet.
*/
public void setType(Type type) {
if (type == null) {
throw new IllegalArgumentException("type must not be null");
}
this.type = type;
this.type = Objects.requireNonNull(type, "type must not be null");
}
/**

View file

@ -21,6 +21,7 @@ import java.util.Map;
import org.jivesoftware.smack.packet.AbstractError;
import org.jivesoftware.smack.packet.PlainStreamElement;
import org.jivesoftware.smack.sasl.SASLError;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
@ -37,14 +38,9 @@ public class SaslStreamElements {
private final String authenticationText;
public AuthMechanism(String mechanism, String authenticationText) {
if (mechanism == null) {
throw new NullPointerException("SASL mechanism shouldn't be null.");
}
if (StringUtils.isNullOrEmpty(authenticationText)) {
throw new IllegalArgumentException("SASL authenticationText must not be null or empty (RFC6120 6.4.2)");
}
this.mechanism = mechanism;
this.authenticationText = authenticationText;
this.mechanism = Objects.requireNonNull(mechanism, "SASL mechanism shouldn't be null.");
this.authenticationText = StringUtils.requireNotNullOrEmpty(authenticationText,
"SASL authenticationText must not be null or empty (RFC6120 6.4.2)");
}
@Override

View file

@ -0,0 +1,27 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* 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;
public class Objects {
public static <T> T requireNonNull(T obj, String message) {
if (obj == null) {
throw new IllegalArgumentException(message);
}
return obj;
}
}

View file

@ -257,4 +257,11 @@ public class StringUtils {
}
return csOne.toString().compareTo(csTwo.toString());
}
public static <CS extends CharSequence> CS requireNotNullOrEmpty(CS cs, String message) {
if (isNullOrEmpty(cs)) {
throw new IllegalArgumentException(message);
}
return cs;
}
}

View file

@ -17,6 +17,7 @@
package org.jivesoftware.smack.util.dns;
import org.jivesoftware.smack.SmackException.ConnectionException;
import org.jivesoftware.smack.util.Objects;
public class HostAddress {
private final String fqdn;
@ -42,8 +43,7 @@ public class HostAddress {
* @throws IllegalArgumentException If the fqdn is null or port is out of valid range (0 - 65535).
*/
public HostAddress(String fqdn, int port) {
if (fqdn == null)
throw new IllegalArgumentException("FQDN is null");
Objects.requireNonNull(fqdn, "FQDN is null");
if (port < 0 || port > 65535)
throw new IllegalArgumentException(
"Port must be a 16-bit unsiged integer (i.e. between 0-65535. Port was: " + port);

View file

@ -1,6 +1,6 @@
/**
*
* Copyright © 2014 Florian Schmaus
* Copyright © 2014-2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,6 +18,7 @@ package org.jivesoftware.smack.util.stringencoder;
import java.io.UnsupportedEncodingException;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils;
public class Base64 {
@ -25,9 +26,7 @@ public class Base64 {
private static Base64.Encoder base64encoder;
public static void setEncoder(Base64.Encoder encoder) {
if (encoder == null) {
throw new IllegalArgumentException("encoder must no be null");
}
Objects.requireNonNull(encoder, "encoder must no be null");
base64encoder = encoder;
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright © 2014 Florian Schmaus
* Copyright © 2014-2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,14 +16,14 @@
*/
package org.jivesoftware.smack.util.stringencoder;
import org.jivesoftware.smack.util.Objects;
public class Base64UrlSafeEncoder {
private static StringEncoder base64UrlSafeEncoder;
public static void setEncoder(StringEncoder encoder) {
if (encoder == null) {
throw new IllegalArgumentException("encoder must no be null");
}
Objects.requireNonNull(encoder, "encoder must no be null");
base64UrlSafeEncoder = encoder;
}

View file

@ -316,14 +316,8 @@ public class DiscoverInfo extends IQ implements Cloneable {
* @param lang the entity's lang.
*/
public Identity(String category, String type, String name, String lang) {
if (StringUtils.isNullOrEmpty(category)) {
throw new IllegalArgumentException("category cannot be null");
}
if (StringUtils.isNullOrEmpty(type)) {
throw new IllegalArgumentException("type cannot be null");
}
this.category = category;
this.type = type;
this.category = StringUtils.requireNotNullOrEmpty(category, "category cannot be null");
this.type = StringUtils.requireNotNullOrEmpty(type, "type cannot be null");
this.key = XmppStringUtils.generateKey(category, type);
this.name = name;
this.lang = lang;
@ -493,9 +487,7 @@ public class DiscoverInfo extends IQ implements Cloneable {
* @param variable the feature's variable.
*/
public Feature(String variable) {
if (variable == null)
throw new IllegalArgumentException("variable cannot be null");
this.variable = variable;
this.variable = StringUtils.requireNotNullOrEmpty(variable, "variable cannot be null");
}
/**

View file

@ -20,6 +20,7 @@ package org.jivesoftware.smackx.iqversion.packet;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
/**
* A Version IQ packet, which is used by XMPP clients to discover version information
@ -85,16 +86,9 @@ public class Version extends IQ {
*/
public Version(String name, String version, String os) {
super(ELEMENT, NAMESPACE);
if (name == null)
{
throw new IllegalArgumentException("name must not be null");
}
if (version == null) {
throw new IllegalArgumentException("version must not be null");
}
this.setType(IQ.Type.result);
this.name = name;
this.version = version;
this.name = StringUtils.requireNotNullOrEmpty(name, "name must not be null");
this.version = StringUtils.requireNotNullOrEmpty(version, "version must not be null");
this.os = os;
}

View file

@ -274,9 +274,7 @@ public class MultiUserChat {
private Presence enter(String nickname, String password, DiscussionHistory history,
long timeout) throws NotConnectedException, NoResponseException,
XMPPErrorException {
if (StringUtils.isNullOrEmpty(nickname)) {
throw new IllegalArgumentException("Nickname must not be null or blank.");
}
StringUtils.requireNotNullOrEmpty(nickname, "Nickname must not be null or blank.");
// We enter a room by sending a presence packet where the "to"
// field is in the form "roomName@service/nickname"
Presence joinPresence = new Presence(Presence.Type.available);
@ -844,9 +842,7 @@ public class MultiUserChat {
* @throws NotConnectedException
*/
public void changeNickname(String nickname) throws NoResponseException, XMPPErrorException, NotConnectedException {
if (StringUtils.isNullOrEmpty(nickname)) {
throw new IllegalArgumentException("Nickname must not be null or blank.");
}
StringUtils.requireNotNullOrEmpty(nickname, "Nickname must not be null or blank.");
// Check that we already have joined the room before attempting to change the
// nickname.
if (!joined) {
@ -881,9 +877,7 @@ public class MultiUserChat {
* @throws NotConnectedException
*/
public void changeAvailabilityStatus(String status, Presence.Mode mode) throws NotConnectedException {
if (StringUtils.isNullOrEmpty(nickname)) {
throw new IllegalArgumentException("Nickname must not be null or blank.");
}
StringUtils.requireNotNullOrEmpty(nickname, "Nickname must not be null or blank.");
// Check that we already have joined the room before attempting to change the
// availability status.
if (!joined) {