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.web;
019
020import com.google.common.collect.Iterables;
021
022import dev.enola.data.Repository;
023import dev.enola.thing.Thing;
024import dev.enola.thing.message.ThingAdapter;
025import dev.enola.thing.repo.ThingRepository;
026
027import org.jspecify.annotations.Nullable;
028
029import java.util.ArrayList;
030
031public class ProtoToThingRepository implements ThingRepository {
032
033    // TODO Like EnolaThingProvider, move somewhere else
034
035    private final Repository<dev.enola.thing.proto.Thing> protoThingRepository;
036
037    public ProtoToThingRepository(Repository<dev.enola.thing.proto.Thing> protoThingRepository) {
038        this.protoThingRepository = protoThingRepository;
039    }
040
041    @Override
042    public @Nullable Thing get(String iri) {
043        var protoThing = protoThingRepository.get(iri);
044        if (protoThing == null) return null;
045        return new ThingAdapter(protoThing);
046    }
047
048    @Override
049    public Iterable<String> listIRI() {
050        return protoThingRepository.listIRI();
051    }
052
053    @Override
054    public Iterable<Thing> list() {
055        var protoThings = protoThingRepository.list();
056        var list = new ArrayList<Thing>(Iterables.size(protoThings));
057        for (var protoThing : protoThings) list.add(new ThingAdapter(protoThing));
058        return list;
059    }
060}