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.id;
019
020import com.google.errorprone.annotations.Immutable;
021
022import dev.enola.common.ByteSeq;
023import dev.enola.common.convert.ObjectToStringBiConverters;
024import dev.enola.data.iri.IRIConverter;
025
026@Immutable
027public class MultibaseIRI extends IDIRI<ByteSeq> {
028
029    // Intentionally different from MultibaseResource's multibase: scheme.
030    private static final String SCHEME = "mb";
031
032    static final ConverterX<MultibaseIRI, ByteSeq> CONVERTER =
033            new ConverterX<>(SCHEME, ObjectToStringBiConverters.MULTIBASE) {
034                @Override
035                protected MultibaseIRI create(ByteSeq id) {
036                    return new MultibaseIRI(id);
037                }
038            };
039
040    public static MultibaseIRI random() {
041        return random(16);
042    }
043
044    public static MultibaseIRI random(int size) {
045        return new MultibaseIRI(size);
046    }
047
048    public static MultibaseIRI parse(String multibase) {
049        return CONVERTER.convertFrom(multibase);
050    }
051
052    public static MultibaseIRI of(ByteSeq id) {
053        return new MultibaseIRI(id);
054    }
055
056    private MultibaseIRI(int size) {
057        this(ByteSeq.random(size));
058    }
059
060    private MultibaseIRI(ByteSeq bs) {
061        super(bs);
062    }
063
064    @Override
065    @SuppressWarnings("unchecked")
066    protected IRIConverter<MultibaseIRI> iriConverter() {
067        return CONVERTER;
068    }
069
070    @Override
071    protected boolean isComparableTo(Object other) {
072        return other instanceof MultibaseIRI;
073    }
074}