001/** 002 * 003 * Copyright © 2014-2019 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jxmpp.jid.impl; 018 019import org.jxmpp.JxmppContext; 020import org.jxmpp.jid.BareJid; 021import org.jxmpp.jid.EntityBareJid; 022import org.jxmpp.jid.DomainBareJid; 023import org.jxmpp.jid.DomainFullJid; 024import org.jxmpp.jid.EntityFullJid; 025import org.jxmpp.jid.EntityJid; 026import org.jxmpp.jid.FullJid; 027import org.jxmpp.jid.parts.Domainpart; 028import org.jxmpp.jid.parts.Localpart; 029import org.jxmpp.jid.parts.Resourcepart; 030import org.jxmpp.stringprep.XmppStringprepException; 031 032/** 033 * RFC6122 2.4 allows JIDs with only a domain and resource part. 034 * <p> 035 * Note that this implementation does not require an cache for the unescaped 036 * string, compared to {@link LocalDomainAndResourcepartJid}. 037 * </p> 038 * 039 */ 040public final class DomainAndResourcepartJid extends AbstractJid implements DomainFullJid { 041 042 /** 043 * 044 */ 045 private static final long serialVersionUID = 1L; 046 047 private final DomainBareJid domainBareJid; 048 private final Resourcepart resource; 049 050 DomainAndResourcepartJid(String domain, String resource, JxmppContext context) throws XmppStringprepException { 051 this(new DomainpartJid(domain, context), Resourcepart.from(resource, context)); 052 } 053 054 DomainAndResourcepartJid(DomainBareJid domainBareJid, Resourcepart resource) { 055 this.domainBareJid = requireNonNull(domainBareJid, "The DomainBareJid must not be null"); 056 this.resource = requireNonNull(resource, "The Resource must not be null"); 057 } 058 059 @Override 060 public String toString() { 061 if (cache != null) { 062 return cache; 063 } 064 cache = domainBareJid.toString() + '/' + resource; 065 return cache; 066 } 067 068 @Override 069 public DomainBareJid asDomainBareJid() { 070 return domainBareJid; 071 } 072 073 @Override 074 public boolean hasNoResource() { 075 return false; 076 } 077 078 @Override 079 public EntityBareJid asEntityBareJidIfPossible() { 080 return null; 081 } 082 083 @Override 084 public EntityFullJid asEntityFullJidIfPossible() { 085 return null; 086 } 087 088 @Override 089 public DomainFullJid asDomainFullJidIfPossible() { 090 return this; 091 } 092 093 @Override 094 public Resourcepart getResourceOrNull() { 095 return getResourcepart(); 096 } 097 098 @Override 099 public boolean isParentOf(EntityBareJid bareJid) { 100 return false; 101 } 102 103 @Override 104 public boolean isParentOf(EntityFullJid fullJid) { 105 return false; 106 } 107 108 @Override 109 public boolean isParentOf(DomainBareJid domainBareJid) { 110 return false; 111 } 112 113 @Override 114 public boolean isParentOf(DomainFullJid domainFullJid) { 115 return domainBareJid.equals(domainFullJid.getDomain()) && resource.equals(domainFullJid.getResourcepart()); 116 } 117 118 @Override 119 public Resourcepart getResourcepart() { 120 return resource; 121 } 122 123 @Override 124 public BareJid asBareJid() { 125 return asDomainBareJid(); 126 } 127 128 @Override 129 public Domainpart getDomain() { 130 return domainBareJid.getDomain(); 131 } 132 133 @Override 134 public String asUnescapedString() { 135 return toString(); 136 } 137 138 @Override 139 public EntityJid asEntityJidIfPossible() { 140 return null; 141 } 142 143 @Override 144 public FullJid asFullJidIfPossible() { 145 return this; 146 } 147 148 @Override 149 public Localpart getLocalpartOrNull() { 150 return null; 151 } 152}