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 DomainpartJid extends AbstractJid implements DomainBareJid {
033
034        /**
035         *
036         */
037        private static final long serialVersionUID = 1L;
038
039        final Domainpart domain;
040
041        DomainpartJid(String domain, JxmppContext context) throws XmppStringprepException {
042                this(Domainpart.from(domain, context));
043        }
044
045        DomainpartJid(Domainpart domain) {
046                this.domain = requireNonNull(domain, "The Domainpart must not be null");
047        }
048
049        @Override
050        public Domainpart getDomain() {
051                return domain;
052        }
053
054        @Override
055        public String toString() {
056                // Prefer the cached version over the domain.toString() one, since the cached version may
057                // also be the internalized representation of the String. Which, e.g. provides benefits when
058                // comparing JIDs.
059                if (cache != null) {
060                        return cache;
061                }
062                cache = domain.toString();
063                return cache;
064        }
065
066        @Override
067        public String asUnescapedString() {
068                // No un-escaping necessary for DomainpartJid
069                return toString();
070        }
071
072        @Override
073        public DomainBareJid asDomainBareJid() {
074                return this;
075        }
076
077        @Override
078        public boolean hasNoResource() {
079                return true;
080        }
081
082        @Override
083        public EntityBareJid asEntityBareJidIfPossible() {
084                return null;
085        }
086
087        @Override
088        public EntityFullJid asEntityFullJidIfPossible() {
089                return null;
090        }
091
092        @Override
093        public DomainFullJid asDomainFullJidIfPossible() {
094                return null;
095        }
096
097        @Override
098        public boolean isParentOf(EntityBareJid bareJid) {
099                return domain.equals(bareJid.getDomain());
100        }
101
102        @Override
103        public boolean isParentOf(EntityFullJid fullJid) {
104                return domain.equals(fullJid.getDomain());
105        }
106
107        @Override
108        public boolean isParentOf(DomainBareJid domainBareJid) {
109                return domain.equals(domainBareJid.getDomain());
110        }
111
112        @Override
113        public boolean isParentOf(DomainFullJid domainFullJid) {
114                return domain.equals(domainFullJid.getDomain());
115        }
116
117        @Override
118        public boolean isStrictParentOf(EntityBareJid bareJid) {
119                return isParentOf(bareJid);
120        }
121
122        @Override
123        public boolean isStrictParentOf(EntityFullJid fullJid) {
124                return isParentOf(fullJid);
125        }
126
127        @Override
128        public boolean isStrictParentOf(DomainBareJid domainBareJid) {
129                return false;
130        }
131
132        @Override
133        public boolean isStrictParentOf(DomainFullJid domainFullJid) {
134                return isParentOf(domainFullJid);
135        }
136
137        @Override
138        public BareJid asBareJid() {
139                return this;
140        }
141
142        @Override
143        public EntityJid asEntityJidIfPossible() {
144                return null;
145        }
146
147        @Override
148        public FullJid asFullJidIfPossible() {
149                return null;
150        }
151
152        @Override
153        public Resourcepart getResourceOrNull() {
154                return null;
155        }
156
157        @Override
158        public Localpart getLocalpartOrNull() {
159                return null;
160        }
161
162}