001/*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2024-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.thing.java;
019
020import com.google.common.collect.ImmutableList;
021
022import dev.enola.thing.Thing;
023
024public class TBFChain implements TBF {
025
026    private final ImmutableList<TBF> chain;
027
028    public TBFChain(ImmutableList<TBF> chain) {
029        this.chain = chain;
030    }
031
032    @Override
033    public Thing.Builder<Thing> create(String typeIRI) {
034        for (TBF tbf : chain) {
035            if (tbf.handles(typeIRI)) return tbf.create(typeIRI);
036        }
037        throw new IllegalStateException("No registered TBF handles: " + typeIRI);
038    }
039
040    @Override
041    public <T extends Thing, B extends Thing.Builder<T>> B create(
042            Class<B> builderInterface, Class<T> thingInterface) {
043        for (TBF tbf : chain) {
044            if (tbf.handles(builderInterface)) return tbf.create(builderInterface, thingInterface);
045        }
046        throw new IllegalStateException("No registered TBF handles: " + builderInterface);
047    }
048
049    @Override
050    public <T extends Thing, B extends Thing.Builder<T>> B create(
051            Class<B> builderInterface, Class<T> thingInterface, int expectedSize) {
052        for (TBF tbf : chain) {
053            if (tbf.handles(builderInterface))
054                return tbf.create(builderInterface, thingInterface, expectedSize);
055        }
056        throw new IllegalStateException("No registered TBF handles: " + builderInterface);
057    }
058
059    @Override
060    public boolean handles(String typeIRI) {
061        return true;
062    }
063}