1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-26 05:52:06 +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.PacketExtensionProvider;
import org.jivesoftware.smack.provider.ProviderManager; import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.DNSUtil; import org.jivesoftware.smack.util.DNSUtil;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.ParserUtils; import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.util.SmackExecutorThreadFactory; 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, public synchronized void login(CharSequence username, String password, String resource) throws XMPPException,
SmackException, IOException { SmackException, IOException {
if (!config.allowNullOrEmptyUsername && StringUtils.isNullOrEmpty(username)) { if (!config.allowNullOrEmptyUsername) {
throw new IllegalArgumentException("Username must not be null or empty"); StringUtils.requireNotNullOrEmpty(username, "Username must not be null or empty");
} }
throwNotConnectedExceptionIfAppropriate(); throwNotConnectedExceptionIfAppropriate();
throwAlreadyLoggedInExceptionIfAppropriate(); throwAlreadyLoggedInExceptionIfAppropriate();
@ -618,9 +619,8 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
@Override @Override
public void sendPacket(Packet packet) throws NotConnectedException { public void sendPacket(Packet packet) throws NotConnectedException {
if (packet == null) { Objects.requireNonNull(packet, "Packet must not be null");
throw new IllegalArgumentException("Packet must not be null");
}
throwNotConnectedExceptionIfAppropriate(); throwNotConnectedExceptionIfAppropriate();
switch (fromMode) { switch (fromMode) {
case OMITTED: case OMITTED:
@ -1419,17 +1419,12 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
public void sendStanzaWithResponseCallback(Packet stanza, PacketFilter replyFilter, public void sendStanzaWithResponseCallback(Packet stanza, PacketFilter replyFilter,
final PacketListener callback, final ExceptionCallback exceptionCallback, final PacketListener callback, final ExceptionCallback exceptionCallback,
long timeout) throws NotConnectedException { long timeout) throws NotConnectedException {
if (stanza == null) { Objects.requireNonNull(stanza, "stanza must not be 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 // 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 // disallow it here in the async API as it makes no sense
throw new IllegalArgumentException("replyFilter must not be null"); Objects.requireNonNull(replyFilter, "replyFilter must not be null");
} Objects.requireNonNull(callback, "callback must not be null");
if (callback == null) {
throw new IllegalArgumentException("callback must not be null");
}
final PacketListener packetListener = new PacketListener() { final PacketListener packetListener = new PacketListener() {
@Override @Override
public void processPacket(Packet packet) throws NotConnectedException { public void processPacket(Packet packet) throws NotConnectedException {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -21,6 +21,7 @@ import java.util.Map;
import org.jivesoftware.smack.packet.AbstractError; import org.jivesoftware.smack.packet.AbstractError;
import org.jivesoftware.smack.packet.PlainStreamElement; import org.jivesoftware.smack.packet.PlainStreamElement;
import org.jivesoftware.smack.sasl.SASLError; import org.jivesoftware.smack.sasl.SASLError;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
@ -37,14 +38,9 @@ public class SaslStreamElements {
private final String authenticationText; private final String authenticationText;
public AuthMechanism(String mechanism, String authenticationText) { public AuthMechanism(String mechanism, String authenticationText) {
if (mechanism == null) { this.mechanism = Objects.requireNonNull(mechanism, "SASL mechanism shouldn't be null.");
throw new NullPointerException("SASL mechanism shouldn't be null."); this.authenticationText = StringUtils.requireNotNullOrEmpty(authenticationText,
} "SASL authenticationText must not be null or empty (RFC6120 6.4.2)");
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;
} }
@Override @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()); 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; package org.jivesoftware.smack.util.dns;
import org.jivesoftware.smack.SmackException.ConnectionException; import org.jivesoftware.smack.SmackException.ConnectionException;
import org.jivesoftware.smack.util.Objects;
public class HostAddress { public class HostAddress {
private final String fqdn; 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). * @throws IllegalArgumentException If the fqdn is null or port is out of valid range (0 - 65535).
*/ */
public HostAddress(String fqdn, int port) { public HostAddress(String fqdn, int port) {
if (fqdn == null) Objects.requireNonNull(fqdn, "FQDN is null");
throw new IllegalArgumentException("FQDN is null");
if (port < 0 || port > 65535) if (port < 0 || port > 65535)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Port must be a 16-bit unsiged integer (i.e. between 0-65535. Port was: " + port); "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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 java.io.UnsupportedEncodingException;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
public class Base64 { public class Base64 {
@ -25,9 +26,7 @@ public class Base64 {
private static Base64.Encoder base64encoder; private static Base64.Encoder base64encoder;
public static void setEncoder(Base64.Encoder encoder) { public static void setEncoder(Base64.Encoder encoder) {
if (encoder == null) { Objects.requireNonNull(encoder, "encoder must no be null");
throw new IllegalArgumentException("encoder must no be null");
}
base64encoder = encoder; 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,14 +16,14 @@
*/ */
package org.jivesoftware.smack.util.stringencoder; package org.jivesoftware.smack.util.stringencoder;
import org.jivesoftware.smack.util.Objects;
public class Base64UrlSafeEncoder { public class Base64UrlSafeEncoder {
private static StringEncoder base64UrlSafeEncoder; private static StringEncoder base64UrlSafeEncoder;
public static void setEncoder(StringEncoder encoder) { public static void setEncoder(StringEncoder encoder) {
if (encoder == null) { Objects.requireNonNull(encoder, "encoder must no be null");
throw new IllegalArgumentException("encoder must no be null");
}
base64UrlSafeEncoder = encoder; base64UrlSafeEncoder = encoder;
} }

View file

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

View file

@ -20,6 +20,7 @@ package org.jivesoftware.smackx.iqversion.packet;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet; 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 * 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) { public Version(String name, String version, String os) {
super(ELEMENT, NAMESPACE); 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.setType(IQ.Type.result);
this.name = name; this.name = StringUtils.requireNotNullOrEmpty(name, "name must not be null");
this.version = version; this.version = StringUtils.requireNotNullOrEmpty(version, "version must not be null");
this.os = os; this.os = os;
} }

View file

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