
uk.org.retep.xmpp.JID Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2007, Peter T Mount
* All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*
*
*
* GNU GENERAL PUBLIC LICENSE - CLASSPATH EXCEPTION
*
*
*
* Linking this library statically or dynamically with other modules
* is making a combined work based on this library. Thus, the terms
* and conditions of the GNU General Public License cover the whole
* combination.
*
*
*
* As a special exception, the copyright holders of this library give
* you permission to link this library with independent modules to
* produce an executable, regardless of the license terms of these
* independent modules, and to copy and distribute the resulting
* executable under terms of your choice, provided that you also meet,
* for each linked independent module, the terms and conditions of the
* license of that module.
*
*
*
* An independent module is a module which is either not derived from or based
* on this library, or a module who's classes extend those within this library
* as part of the implementation of the library.
*
*
*
* If you modify this library, you may extend this exception to your version
* of the library, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
*/
package uk.org.retep.xmpp;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import net.jcip.annotations.ThreadSafe;
import org.terracotta.modules.annotations.HonorTransient;
import org.terracotta.modules.annotations.InstrumentedClass;
import uk.org.retep.util.string.StringUtils;
/**
* Bean representing a Jabber IDentity
* @author peter
*/
@ThreadSafe
@HonorTransient
@InstrumentedClass
public class JID
implements Comparable,
Externalizable
{
static final long serialVersionUID = 2233984628143280590L;
private String node;
private String domain;
private String resource;
/**
* Construct a new JID with no node, domain or resource defined
*
*/
public JID()
{
this( null, null, null );
}
/**
* Construct a new JID based on node and domain.
* This will form the JID node@domain
* @param node JID Node
* @param domain JID Domain
*/
public JID( String node, String domain )
{
this( node, domain, null );
}
/**
* Construct a new JID based on node, domain and resource.
* This will form the JID node@domain/resource
* @param node JID node
* @param domain JID domain
* @param resource JID resource
*/
public JID( String node, String domain, String resource )
{
setNode( node );
setDomain( domain );
setResource( resource );
}
/**
* Construct a copy of another JID
* @param jid JID to copy
*/
public JID( JID jid )
{
this( jid.getNode(), jid.getDomain(), jid.getResource() );
}
public JID( ObjectInput in )
throws IOException,
ClassNotFoundException
{
readExternal( in );
}
/**
* The node, before the @, or null if not present
* @return String or null if not present
*/
public String getNode()
{
return node;
}
/**
* Set the node
* @param node String or null if no node
* @return This JID. Intended to allow chaining.
*/
public JID setNode( String node )
{
this.node =node ;
return this;
}
/**
* The domain or null if not present
* @return String or null if not present
*/
public String getDomain()
{
return domain;
}
/**
* Set the domain
* @param domain String or null if not present
* @return This JID. Intended to allow chaining.
*/
public JID setDomain( String domain )
{
this.domain = domain;
return this;
}
/**
* The resource or null if not present
* @return String or null if not present
*/
public String getResource()
{
return resource;
}
/**
* Set the resource
* @param resource String or null if not present
* @return This JID. Intended to allow chaining.
*/
public JID setResource( String resource )
{
this.resource = resource;
return this;
}
/**
* Return a copy of this JID
* @return JID which is a copy of this JID
*/
public JID copy()
{
return new JID( this );
}
/**
* Return a copy of this JID, but with the resource removed
* @return JID which is a copy of this JID but has a null resource
*/
public JID copyWithoutResource()
{
return copy().clearResource();
}
/**
* Return a copy of this JID, but with the resource removed
* @return JID which is a copy of this JID but has a null resource
*/
public JID copyWithoutNode()
{
return copy().clearNode();
}
/**
* Return a copy of this JID, but with the resource removed
* @return JID which is a copy of this JID but has a null resource
*/
public JID copyWithoutNodeOrResource()
{
return copy().clearNode().clearResource();
}
/**
* Remove the resource from the JID
* @return This JID. Intended to allow chaining.
*/
public JID clearResource()
{
setResource( null );
return this;
}
/**
* Remove the node from the JID
* @return This JID. Intended to allow chaining.
*/
public JID clearNode()
{
setNode( null );
return this;
}
/**
* Hash code
* @return hashCode
*/
@Override
public int hashCode()
{
int h = hashCodeWithoutResource();
if( resource != null )
{
h = (31 * h) + resource.hashCode();
}
return h;
}
public int hashCodeWithoutResource()
{
int h = 1;
if( node != null )
{
h = (31 * h) + node.hashCode();
}
if( domain != null )
{
h = (31 * h) + domain.hashCode();
}
return h;
}
/**
* Equality
* @param obj to test
* @return true if obj equals this JID
*/
@Override
public boolean equals( Object obj )
{
if( obj == null || !(obj instanceof JID) )
{
return false;
}
boolean ret = equalsWithoutResource( obj );
if( ret )
{
// It must be a jid for ret to be true here, see equalsWithoutResource()
JID jid = JID.class.cast( obj );
ret = resource == null ? jid.getResource() == null : resource.equals(
jid.getResource() );
}
return ret;
}
public boolean equalsWithoutResource( Object obj )
{
if( obj == null || !(obj instanceof JID) )
{
return false;
}
JID jid = JID.class.cast( obj );
boolean ret = getNode() == null ? jid.getNode() == null : getNode().
equals( jid.getNode() );
if( ret )
{
ret = domain == null ? jid.getDomain() == null : domain.equals(
jid.getDomain() );
}
return ret;
}
/**
* String representation of this JID
* @return String representation of this JID
*/
@Override
public String toString()
{
return XMPPDatatypeConverter.jidToString( node, domain, resource, false );
}
public String toUniqueString()
{
return XMPPDatatypeConverter.jidToString( node, domain, resource, true );
}
public static String toUniqueString( final JID jid )
{
return jid == null ? "@/" : jid.toUniqueString();
}
@Override
public int compareTo( JID o )
{
int r = getDomain().compareTo( o.getDomain() );
if( r == 0 )
{
r = getNode().compareTo( o.getNode() );
}
if( r == 0 && getResource() != null )
{
r = getResource().compareTo( o.getResource() );
}
return r;
}
@Override
public void readExternal( ObjectInput in )
throws IOException,
ClassNotFoundException
{
setNode( in.readBoolean() ? in.readUTF() : null );
setDomain( in.readUTF() );
setResource( in.readBoolean() ? in.readUTF() : null );
}
@Override
public void writeExternal( ObjectOutput out )
throws IOException
{
// Handle node encoding
out.writeBoolean( node != null );
if( node != null )
{
out.writeUTF( node );
}
out.writeUTF( domain );
out.writeBoolean( resource != null );
if( resource != null )
{
out.writeUTF( resource );
}
}
public boolean isDomainEmpty()
{
return StringUtils.isStringEmpty( domain );
}
public boolean isDomainNotEmpty()
{
return StringUtils.isStringNotEmpty( domain );
}
public boolean isNodeEmpty()
{
return StringUtils.isStringEmpty( node );
}
public boolean isNodeNotEmpty()
{
return StringUtils.isStringNotEmpty( node );
}
public boolean isResourceEmpty()
{
return StringUtils.isStringEmpty( resource );
}
public boolean isResourceNotEmpty()
{
return StringUtils.isStringNotEmpty( resource );
}
public boolean isDomainOnly()
{
return isDomainNotEmpty() && isNodeEmpty() && isResourceEmpty();
}
public boolean isNodeDomainOnly()
{
return isDomainNotEmpty() && isNodeNotEmpty() && isResourceEmpty();
}
public boolean isNodeDomainResource()
{
return isDomainNotEmpty() && isNodeNotEmpty() && isResourceNotEmpty();
}
}