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.core.thing; 019 020import dev.enola.data.ProviderFromIRI; 021import dev.enola.data.iri.template.URITemplateMatcherChain; 022import dev.enola.thing.proto.Thing; 023import dev.enola.thing.proto.Things; 024import dev.enola.thing.repo.ThingProvider; 025 026/** 027 * ThingConnectorsProvider implements {@link ThingProvider} by delegating to a list of {@link 028 * ThingConnector}. 029 */ 030public class ThingConnectorsProvider 031 implements ProviderFromIRI<Thing> /* TODO implements ThingProvider ? */ { 032 033 // TODO Align the overlap this has with EnolaThingProvider & EnolaServiceRegistry 034 035 private final URITemplateMatcherChain<ThingConnector> matcher; 036 037 public ThingConnectorsProvider(Iterable<ThingConnector> connectors) { 038 var builder = URITemplateMatcherChain.<ThingConnector>builder(); 039 for (var connector : connectors) { 040 builder.add(connector.iri(), connector); 041 } 042 043 this.matcher = builder.build(); 044 } 045 046 @Override 047 public Thing get(String iri) { 048 var opt = matcher.match(iri); 049 if (opt.isEmpty()) throw new IllegalArgumentException("No template matched: " + iri); 050 051 var entry = opt.get(); 052 var thingConnector = entry.getKey(); 053 var parameters = entry.getValue(); 054 055 var thingsBuilder = Things.newBuilder(); 056 thingConnector.augment(thingsBuilder, iri, parameters); 057 // TODO The get(0) is wrong, we need to return Things here, and UI needs to show all 058 return thingsBuilder.build().getThings(0); 059 } 060}