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
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 DomainBareJid asDomainBareJid() {
115                return domainBareJid;
116        }
117
118        @Override
119        public Domainpart getDomain() {
120                return domainBareJid.getDomain();
121        }
122
123        @Override
124        public BareJid asBareJid() {
125                return this;
126        }
127
128        @Override
129        public boolean hasNoResource() {
130                return true;
131        }
132
133        @Override
134        public EntityJid asEntityJidIfPossible() {
135                return this;
136        }
137
138        @Override
139        public FullJid asFullJidIfPossible() {
140                return null;
141        }
142
143        @Override
144        public EntityBareJid asEntityBareJid() {
145                return this;
146        }
147
148        @Override
149        public Resourcepart getResourceOrNull() {
150                return null;
151        }
152
153        @Override
154        public Localpart getLocalpartOrNull() {
155                return getLocalpart();
156        }
157
158        @Override
159        public String asEntityBareJidString() {
160                return toString();
161        }
162}