001/*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2025-2026 The Enola <https://enola.dev> Authors
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *     https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package dev.enola.data.iri;
019
020import static java.util.Objects.requireNonNull;
021
022import com.google.common.base.Objects;
023
024import java.io.IOException;
025
026import javax.xml.namespace.QName;
027
028/**
029 * CURIE {@link IRI}.
030 *
031 * <p>See also {@link QName}.
032 */
033/* non-public! */ final /*TODO value*/ class NamespacedIRI extends StringableIRI {
034
035    // NO! private final String prefix;
036    private final String namespaceIRI;
037    private final String localName;
038
039    NamespacedIRI(String namespaceIRI, String localName) {
040        this.namespaceIRI = requireNonNull(namespaceIRI, "namespaceIRI");
041        this.localName = requireNonNull(localName, "localName");
042    }
043
044    @Override
045    public void append(Appendable appendable) throws IOException {
046        appendable.append(namespaceIRI).append(localName);
047    }
048
049    @Override
050    protected String createStringIRI() {
051        return namespaceIRI + localName;
052    }
053
054    @Override
055    public int hashCode() {
056        int result = namespaceIRI.hashCode();
057        result = 31 * result + localName.hashCode();
058        return result;
059    }
060
061    @Override
062    protected boolean isEqualTo(Object other) {
063        if (other instanceof NamespacedIRI otherCurieIRI)
064            return Objects.equal(namespaceIRI, otherCurieIRI.namespaceIRI)
065                    && Objects.equal(localName, otherCurieIRI.localName);
066        return false;
067    }
068
069    @Override
070    protected boolean isComparableTo(Object other) {
071        return other instanceof NamespacedIRI;
072    }
073
074    @Override
075    protected int compare(Object other) {
076        var otherCurieIRI = (NamespacedIRI) other;
077        int namespaceComparison = this.namespaceIRI.compareTo(otherCurieIRI.namespaceIRI);
078        if (namespaceComparison != 0) {
079            return namespaceComparison;
080        }
081        return this.localName.compareTo(otherCurieIRI.localName);
082    }
083}