<commit_before>/*
 * Tigase Jabber/XMPP Server
 * Copyright (C) 2004-2008 "<NAME>" <<EMAIL>>
 * 
 * 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, version 3 of the License.
 * 
 * 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. Look for COPYING file in the top folder.
 * If not, see http://www.gnu.org/licenses/.
 * 
 * $Rev$
 * Last modified by $Author$
 * $Date$
 */

package tigase.xmpp;

import tigase.util.TigaseStringprepException;

/**
 * Created: Dec 28, 2009 10:48:04 PM
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Rev$
 */
public class JID implements Comparable<JID> {

	private BareJID bareJid = null;
	private String resource = null;
	private String to_string = null;

	public JID(String jid) throws TigaseStringprepException {
		String[] parsedJid = BareJID.parseJID(jid);
		bareJid = BareJID.bareJIDInstance(parsedJid[0], parsedJid[1]);
		setResource(parsedJid[2]);
	}

	public JID(String localpart, String domain, String resource)
			throws TigaseStringprepException {
		bareJid = BareJID.bareJIDInstance(localpart, domain);
		setResource(resource);
	}

	public JID(BareJID bareJid, String resource)
			throws TigaseStringprepException {
		this.bareJid = bareJid;
		setResource(resource);
	}

	public void setResource(String resource) throws TigaseStringprepException {
		this.resource = BareJID.stringPrep.resourceprep(resource);
		to_string = BareJID.toString(bareJid, resource);
	}

	public String getResource() {
		return resource;
	}

	public void setBareJID(BareJID bareJid) {
		this.bareJid = bareJid;
		to_string = BareJID.toString(bareJid, resource);
	}

	public BareJID getBareJID() {
		return bareJid;
	}

	public String getLocalpart() {
		return bareJid.getLocalpart();
	}

	public String getDomain() {
		return bareJid.getDomain();
	}

	@Override
	public String toString() {
		return to_string;
	}

	@Override
	public int compareTo(JID o) {
		return to_string.compareTo(o.to_string);
	}

	@Override
	public boolean equals(Object b) {
		if (b instanceof JID) {
			JID jid = (JID)b;
			return bareJid.equals(jid.bareJid) &&
					(resource == jid.resource ||
					(resource != null && resource.equals(jid.resource)));
		} else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return to_string.hashCode();
	}

}
<commit_msg>Added convenience method to easily create a copy of JID without resource or with given resource.<commit_after>/*
 * Tigase Jabber/XMPP Server
 * Copyright (C) 2004-2008 "<NAME>" <<EMAIL>>
 * 
 * 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, version 3 of the License.
 * 
 * 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. Look for COPYING file in the top folder.
 * If not, see http://www.gnu.org/licenses/.
 * 
 * $Rev$
 * Last modified by $Author$
 * $Date$
 */

package tigase.xmpp;

import tigase.util.TigaseStringprepException;

/**
 * Created: Dec 28, 2009 10:48:04 PM
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Rev$
 */
public class JID implements Comparable<JID> {

	private BareJID bareJid = null;
	private String resource = null;
	private String to_string = null;

	public JID(String jid) throws TigaseStringprepException {
		String[] parsedJid = BareJID.parseJID(jid);
		bareJid = BareJID.bareJIDInstance(parsedJid[0], parsedJid[1]);
		setResource(parsedJid[2]);
	}

	public JID(String localpart, String domain, String resource)
			throws TigaseStringprepException {
		bareJid = BareJID.bareJIDInstance(localpart, domain);
		setResource(resource);
	}

	public JID(BareJID bareJid, String resource)
			throws TigaseStringprepException {
		this.bareJid = bareJid;
		setResource(resource);
	}
	
	public JID(BareJID bareJid) {
		this.bareJid = bareJid;
		to_string = BareJID.toString(bareJid, resource);
	}

	public JID copyWithoutResource() {
		JID result = new JID(bareJid);
		return result;
	}

	public JID copyWithResource(String resource) throws TigaseStringprepException {
		JID result = new JID(bareJid, resource);
		return result;
	}

	public void setResource(String resource) throws TigaseStringprepException {
		this.resource = BareJID.stringPrep.resourceprep(resource);
		to_string = BareJID.toString(bareJid, resource);
	}

	public String getResource() {
		return resource;
	}

	public void setBareJID(BareJID bareJid) {
		this.bareJid = bareJid;
		to_string = BareJID.toString(bareJid, resource);
	}

	public BareJID getBareJID() {
		return bareJid;
	}

	public String getLocalpart() {
		return bareJid.getLocalpart();
	}

	public String getDomain() {
		return bareJid.getDomain();
	}

	@Override
	public String toString() {
		return to_string;
	}

	@Override
	public int compareTo(JID o) {
		return to_string.compareTo(o.to_string);
	}

	@Override
	public boolean equals(Object b) {
		if (b instanceof JID) {
			JID jid = (JID)b;
			return bareJid.equals(jid.bareJid) &&
					(resource == jid.resource ||
					(resource != null && resource.equals(jid.resource)));
		} else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return to_string.hashCode();
	}

}

{'fraction_non_alphanumeric': 0.10179640718562874, 'fraction_numerical': 0.008212147134302824, 'mean_word_length': 4.141600703605981, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3610243', 'n_tokens_mistral': 2108, 'n_tokens_neox': 1916, 'n_words': 569}