001/* 002 * SPDX-FileCopyrightText: none 003 * SPDX-License-Identifier: CC0-1.0 004 */ 005 006package gov.nist.secauto.oscal.lib.model.metadata; 007 008import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; 009import gov.nist.secauto.metaschema.core.util.ObjectUtils; 010import gov.nist.secauto.oscal.lib.model.Link; 011 012import java.net.URI; 013import java.util.LinkedList; 014import java.util.List; 015import java.util.stream.Collectors; 016import java.util.stream.Stream; 017 018import edu.umd.cs.findbugs.annotations.NonNull; 019 020public abstract class AbstractLink implements ILink { 021 022 public static List<Link> merge(@NonNull List<Link> original, @NonNull List<Link> additional) { 023 return Stream.concat(original.stream(), additional.stream()) 024 .collect(Collectors.toCollection(LinkedList::new)); 025 } 026 027 @NonNull 028 public static Builder builder(@NonNull URI href) { 029 return new Builder(href); 030 } 031 032 public static class Builder { 033 @NonNull 034 private final URI href; 035 private String rel; 036 private String mediaType; 037 private MarkupLine text; 038 039 public Builder(@NonNull URI href) { 040 this.href = ObjectUtils.requireNonNull(href, "href"); 041 } 042 043 @NonNull 044 public Builder relation(@NonNull String relation) { 045 this.rel = ObjectUtils.requireNonNull(relation, "rel"); 046 return this; 047 } 048 049 @NonNull 050 public Builder value(@NonNull String mediaType) { 051 this.mediaType = ObjectUtils.requireNonNull(mediaType, "mediaType"); 052 return this; 053 } 054 055 @NonNull 056 public Builder clazz(@NonNull MarkupLine text) { 057 this.text = ObjectUtils.requireNonNull(text, "text"); 058 return this; 059 } 060 061 @NonNull 062 public Link build() { 063 Link retval = new Link(); 064 retval.setHref(href); 065 if (rel != null) { 066 retval.setRel(rel); 067 } 068 if (mediaType != null) { 069 retval.setMediaType(mediaType); 070 } 071 if (text != null) { 072 retval.setText(text); 073 } 074 075 return retval; 076 } 077 } 078}