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