001/** 002 * 003 * Copyright © 2014-2024 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 032public final class LocalDomainAndResourcepartJid extends AbstractJid implements EntityFullJid { 033 034 /** 035 * 036 */ 037 private static final long serialVersionUID = 1L; 038 039 private final EntityBareJid bareJid; 040 private final Resourcepart resource; 041 042 private String unescapedCache; 043 044 LocalDomainAndResourcepartJid(String localpart, String domain, String resource, JxmppContext context) throws XmppStringprepException { 045 this(new LocalAndDomainpartJid(localpart, domain, context), Resourcepart.from(resource, context)); 046 } 047 048 LocalDomainAndResourcepartJid(EntityBareJid bareJid, Resourcepart resource) { 049 this.bareJid = requireNonNull(bareJid, "The EntityBareJid must not be null"); 050 this.resource = requireNonNull(resource, "The Resourcepart must not be null"); 051 } 052 053 @Override 054 public String toString() { 055 if (cache != null) { 056 return cache; 057 } 058 cache = bareJid.toString() + '/' + resource; 059 return cache; 060 } 061 062 @Override 063 public String asUnescapedString() { 064 if (unescapedCache != null) { 065 return unescapedCache; 066 } 067 unescapedCache = bareJid.asUnescapedString() + '/' + resource; 068 return unescapedCache; 069 } 070 071 @Override 072 public EntityBareJid asEntityBareJid() { 073 return bareJid; 074 } 075 076 @Override 077 public String asEntityBareJidString() { 078 return asEntityBareJid().toString(); 079 } 080 081 @Override 082 public boolean hasNoResource() { 083 return false; 084 } 085 086 @Override 087 public EntityBareJid asEntityBareJidIfPossible() { 088 return asEntityBareJid(); 089 } 090 091 @Override 092 public EntityFullJid asEntityFullJidIfPossible() { 093 return this; 094 } 095 096 @Override 097 public DomainFullJid asDomainFullJidIfPossible() { 098 return null; 099 } 100 101 @Override 102 public Localpart getLocalpartOrNull() { 103 return bareJid.getLocalpart(); 104 } 105 106 @Override 107 public Resourcepart getResourceOrNull() { 108 return getResourcepart(); 109 } 110 111 @Override 112 public boolean isParentOf(EntityBareJid bareJid) { 113 return false; 114 } 115 116 @Override 117 public boolean isParentOf(EntityFullJid fullJid) { 118 return this.equals(fullJid); 119 } 120 121 @Override 122 public boolean isParentOf(DomainBareJid domainBareJid) { 123 return false; 124 } 125 126 @Override 127 public boolean isParentOf(DomainFullJid domainFullJid) { 128 return false; 129 } 130 131 @Override 132 public boolean isStrictParentOf(EntityBareJid bareJid) { 133 return false; 134 } 135 136 @Override 137 public boolean isStrictParentOf(EntityFullJid fullJid) { 138 return false; 139 } 140 141 @Override 142 public boolean isStrictParentOf(DomainBareJid domainBareJid) { 143 return false; 144 } 145 146 @Override 147 public boolean isStrictParentOf(DomainFullJid domainFullJid) { 148 return false; 149 } 150 151 @Override 152 public DomainBareJid asDomainBareJid() { 153 return bareJid.asDomainBareJid(); 154 } 155 156 @Override 157 public Resourcepart getResourcepart() { 158 return resource; 159 } 160 161 @Override 162 public BareJid asBareJid() { 163 return asEntityBareJid(); 164 } 165 166 @Override 167 public Domainpart getDomain() { 168 return bareJid.getDomain(); 169 } 170 171 @Override 172 public Localpart getLocalpart() { 173 return bareJid.getLocalpart(); 174 } 175 176 @Override 177 public EntityJid asEntityJidIfPossible() { 178 return this; 179 } 180 181 @Override 182 public FullJid asFullJidIfPossible() { 183 return this; 184 } 185}