001/* 002 * SPDX-FileCopyrightText: none 003 * SPDX-License-Identifier: CC0-1.0 004 */ 005 006package dev.metaschema.core.model; 007 008import java.util.LinkedList; 009import java.util.List; 010 011import dev.metaschema.core.model.impl.DefaultContainerModelSupport; 012import dev.metaschema.core.util.CollectionUtil; 013import edu.umd.cs.findbugs.annotations.NonNull; 014 015/** 016 * A choice model builder. 017 * <p> 018 * Is extended to support other model builders (i.e. assembly model builders). 019 * 020 * @param <MI> 021 * the model instance Java type 022 * @param <NMI> 023 * the named model instance Java type 024 * @param <FI> 025 * the field instance Java type 026 * @param <AI> 027 * the assembly instance Java type 028 * @see DefaultChoiceGroupModelBuilder for a choice group model builder 029 * @see DefaultAssemblyModelBuilder for an assembly model builder 030 */ 031public class DefaultChoiceModelBuilder< 032 MI extends IModelInstance, 033 NMI extends INamedModelInstance, 034 FI extends IFieldInstance, 035 AI extends IAssemblyInstance> 036 extends DefaultChoiceGroupModelBuilder<NMI, FI, AI> { 037 // collections to store model instances 038 @NonNull 039 private final List<MI> modelInstances = new LinkedList<>(); 040 041 @SuppressWarnings("unchecked") 042 @Override 043 public void append(FI instance) { 044 modelInstances.add((MI) instance); 045 super.append(instance); 046 } 047 048 @SuppressWarnings("unchecked") 049 @Override 050 public void append(AI instance) { 051 modelInstances.add((MI) instance); 052 super.append(instance); 053 } 054 055 /** 056 * Get the appended model instances. 057 * 058 * @return the instances or an empty list if no instances were appended 059 */ 060 @NonNull 061 public List<MI> getModelInstances() { 062 return modelInstances; 063 } 064 065 /** 066 * Build an immutable choice model container based on the appended instances. 067 * 068 * @return the container 069 */ 070 @NonNull 071 public IContainerModelSupport<MI, NMI, FI, AI> buildChoice() { 072 return getModelInstances().isEmpty() 073 ? IContainerModelSupport.empty() 074 : new DefaultContainerModelSupport<>( 075 CollectionUtil.unmodifiableList(getModelInstances()), 076 CollectionUtil.unmodifiableMap(getNamedModelInstances()), 077 CollectionUtil.unmodifiableMap(getFieldInstances()), 078 CollectionUtil.unmodifiableMap(getAssemblyInstances())); 079 } 080}