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.repo; 019 020import dev.enola.common.context.TLC; 021import dev.enola.common.convert.ConversionException; 022import dev.enola.thing.Thing; 023import dev.enola.thing.impl.OnlyIRIThing; 024 025import org.jspecify.annotations.NonNull; 026 027import java.io.UncheckedIOException; 028 029/** 030 * AlwaysThingProvider is a {@link ThingProvider} which never returns null, but always a Thing. 031 * 032 * <p>The Thing WILL have the requested IRI - but if no such Thing is known (e.g. was pre-loaded, or 033 * could be fetched), then the returned Thing ONLY has an IRI - and no properties (nor datatypes). 034 * 035 * <p>This is useful in some situations where callers don't want to distinguish "non-existent" 036 * Things; e.g. to have a {@link dev.enola.thing.metadata.ThingMetadataProvider} always work, or so. 037 */ 038public class AlwaysThingProvider implements ThingProvider { 039 040 // TODO Make this the private and built-in default, and remove @Nullable from ThingProvider 041 042 public static final AlwaysThingProvider CTX = 043 new AlwaysThingProvider( 044 // TODO Rethink EmptyThingProvider... convenient - but hides errors! So not? 045 TLC.optional(ThingProvider.class).orElse(EmptyThingProvider.INSTANCE)); 046 047 private final ThingProvider delegate; 048 049 public AlwaysThingProvider(ThingProvider delegate) { 050 this.delegate = delegate; 051 } 052 053 @Override 054 public @NonNull Thing get(String iri) throws UncheckedIOException, ConversionException { 055 var thing = delegate.get(iri); 056 if (thing == null) return new OnlyIRIThing(iri); 057 else return thing; 058 } 059}