1 /**
2 * Portions of this software was developed by employees of the National Institute
3 * of Standards and Technology (NIST), an agency of the Federal Government and is
4 * being made available as a public service. Pursuant to title 17 United States
5 * Code Section 105, works of NIST employees are not subject to copyright
6 * protection in the United States. This software may be subject to foreign
7 * copyright. Permission in the United States and in foreign countries, to the
8 * extent that NIST may hold copyright, to use, copy, modify, create derivative
9 * works, and distribute this software and its documentation without fee is hereby
10 * granted on a non-exclusive basis, provided that this notice and disclaimer
11 * of warranty appears in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
19 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25 */
26
27 package gov.nist.secauto.swid.builder.resource.file;
28
29 import static gov.nist.secauto.swid.builder.util.Util.requireNonEmpty;
30
31 import gov.nist.secauto.swid.builder.ValidationException;
32 import gov.nist.secauto.swid.builder.resource.AbstractResourceBuilder;
33
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.Objects;
37
38 public abstract class AbstractFileSystemItemBuilder<E extends AbstractFileSystemItemBuilder<E>>
39 extends AbstractResourceBuilder<E> {
40 private Boolean key;
41 private String root;
42 private List<String> location;
43 private String name;
44
45 protected AbstractFileSystemItemBuilder() {
46 super();
47 }
48
49 @Override
50 public void reset() {
51 super.reset();
52
53 this.key = null;
54 this.root = null;
55 this.location = null;
56 this.name = null;
57 }
58
59 public Boolean getKey() {
60 return key;
61 }
62
63 public String getRoot() {
64 return root;
65 }
66
67 public List<String> getLocation() {
68 return (location == null ? Collections.<String>emptyList() : Collections.unmodifiableList(location));
69 }
70
71 public String getName() {
72 return name;
73 }
74
75 /**
76 * Sets the path representing the location and name of a filesystem resource to the provided value.
77 * This is a shortcut for calling {@link #name(String)} and {@link #location(List)}
78 *
79 * @param pathSegments
80 * one or more path segments
81 * @return the same builder instance
82 * @see #name(String)
83 * @see #location(List)
84 */
85 @SuppressWarnings("unchecked")
86 public E nameAndLocation(List<String> pathSegments) {
87 Objects.requireNonNull(pathSegments, "pathSegments");
88 if (pathSegments.size() < 2) {
89 throw new IllegalArgumentException("two or more path segments must be provided");
90 }
91
92 this.name = pathSegments.get(pathSegments.size() - 1);
93 this.location = pathSegments.subList(0, pathSegments.size() - 1);
94 return (E) this;
95 }
96
97 /**
98 * Sets the filesystem root of a filesystem resource to the provided value.
99 *
100 * @param root
101 * the filesystem root value
102 * @return the same builder instance
103 */
104 @SuppressWarnings("unchecked")
105 public E root(String root) {
106 requireNonEmpty(root, "root");
107 this.root = root;
108
109 return (E) this;
110 }
111
112 /**
113 * Sets the path representing the location of a filesystem resource to the provided value, with the
114 * name omitted.
115 *
116 * @param location
117 * a sequence of paths
118 * @return the same builder instance
119 */
120 @SuppressWarnings("unchecked")
121 public E location(List<String> location) {
122 Objects.requireNonNull(location, "location");
123 if (location.isEmpty()) {
124 throw new IllegalArgumentException("location");
125 }
126 this.location = location;
127 return (E) this;
128 }
129
130 /**
131 * Sets the name of a filesystem resource to the provided value.
132 *
133 * @param name
134 * the name value
135 * @return the same builder instance
136 */
137 @SuppressWarnings("unchecked")
138 public E name(String name) {
139 requireNonEmpty(name, "name");
140 this.name = name;
141
142 return (E) this;
143 }
144
145 @Override
146 public void validate() throws ValidationException {
147 super.validate();
148 validateNonEmpty("name", name);
149 }
150
151 }