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
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 DomainBareJid asDomainBareJid() {
133                return bareJid.asDomainBareJid();
134        }
135
136        @Override
137        public Resourcepart getResourcepart() {
138                return resource;
139        }
140
141        @Override
142        public BareJid asBareJid() {
143                return asEntityBareJid();
144        }
145
146        @Override
147        public Domainpart getDomain() {
148                return bareJid.getDomain();
149        }
150
151        @Override
152        public Localpart getLocalpart() {
153                return bareJid.getLocalpart();
154        }
155
156        @Override
157        public EntityJid asEntityJidIfPossible() {
158                return this;
159        }
160
161        @Override
162        public FullJid asFullJidIfPossible() {
163                return this;
164        }
165}