001/*
002 * Copyright (C) 2022 - 2024, the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *    http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.github.ascopes.jct.workspaces;
017
018import java.io.Closeable;
019import java.io.IOException;
020import java.util.List;
021
022/**
023 * Base interface for a managed directory, including the interfaces for creating fluent-style
024 * builders.
025 *
026 * @author Ashley Scopes
027 * @since 0.0.1
028 */
029public interface ManagedDirectory extends DirectoryBuilder, PathRoot {
030
031  /**
032   * Method that returns the object it is called upon to enable creating fluent-language builders.
033   *
034   * <p>For example:
035   *
036   * <pre><code>
037   *   thisDirectory
038   *       .createFile("foo", "bar", "baz.txt").withContents(...)
039   *       .and().also()
040   *       .createFile("foo", "bar", "bork.txt").withContents(...);
041   * </code></pre>
042   *
043   * @return this object.
044   * @see #and
045   * @see #then
046   */
047  default ManagedDirectory also() {
048    return this;
049  }
050
051  /**
052   * Method that returns the object it is called upon to enable creating fluent-language builders.
053   *
054   * <p>For example:
055   *
056   * <pre><code>
057   *   thisDirectory
058   *       .createFile("foo", "bar", "baz.txt").withContents(...)
059   *       .and()
060   *       .createFile("foo", "bar", "bork.txt").withContents(...);
061   * </code></pre>
062   *
063   * @return this object.
064   * @see #also
065   * @see #then
066   */
067  default ManagedDirectory and() {
068    return this;
069  }
070
071  /**
072   * Close the resource.
073   *
074   * <p>This specifically is not provided by implementing {@link Closeable} as to prevent IDEs
075   * giving false linting errors about not closing resources.
076   *
077   * <p>Users should not need to call this directly if they are using instances produced from a
078   * {@link Workspace} implementation.
079   *
080   * @throws IOException if an IO exception occurs.
081   */
082  void close() throws IOException;
083
084  /**
085   * Create a directory builder for the given path in this RAM file system.
086   *
087   * <p>Examples:
088   *
089   * <pre><code>
090   *   // Using platform-specific separators.
091   *   dir.createDirectory("foo/bar/baz")...;
092   *
093   *   // Letting JCT infer the correct path separators to use (recommended).
094   *   dir.createDirectory("foo", "bar", "baz")...;
095   * </code></pre>
096   *
097   * @param fragments the parts of the path.
098   * @return the directory builder.
099   * @throws IllegalArgumentException if no path fragments are provided.
100   * @throws NullPointerException     if any of the path fragments are {@code null}.
101   */
102  default DirectoryBuilder createDirectory(String... fragments) {
103    return createDirectory(List.of(fragments));
104  }
105
106  /**
107   * Create a directory builder for the given path in this RAM file system.
108   *
109   * <p>Examples:
110   *
111   * <pre><code>
112   *   // Using platform-specific separators.
113   *   dir.createDirectory(List.of("foo/bar/baz"))...;
114   *
115   *   // Letting JCT infer the correct path separators to use (recommended).
116   *   dir.createDirectory(List.of("foo", "bar", "baz"))...;
117   * </code></pre>
118   *
119   * @param fragments the parts of the path.
120   * @return the directory builder.
121   * @throws IllegalArgumentException if no path fragments are provided.
122   * @throws NullPointerException     if any of the path fragments are {@code null}.
123   * @since 4.0.0
124   */
125  DirectoryBuilder createDirectory(List<String> fragments);
126
127  /**
128   * Create a file builder for the given path in this RAM file system.
129   *
130   * <pre><code>
131   *   // Using platform-specific separators.
132   *   dir.createFile("foo/bar/baz.txt")...;
133   *
134   *   // Letting JCT infer the correct path separators to use (recommended).
135   *   dir.createFile("foo", "bar", "baz.txt")...;
136   * </code></pre>
137   *
138   * @param fragments the parts of the path.
139   * @return the file builder.
140   * @throws IllegalArgumentException if no path fragments are provided.
141   * @throws NullPointerException     if any of the path fragments are {@code null}.
142   */
143  default FileBuilder createFile(String... fragments) {
144    return createFile(List.of(fragments));
145  }
146
147  /**
148   * Create a file builder for the given path in this RAM file system.
149   *
150   * <pre><code>
151   *   // Using platform-specific separators.
152   *   dir.createFile(List.of("foo/bar/baz.txt"))...;
153   *
154   *   // Letting JCT infer the correct path separators to use (recommended).
155   *   dir.createFile(List.of("foo", "bar", "baz.txt"))...;
156   * </code></pre>
157   *
158   * @param fragments the parts of the path.
159   * @return the file builder.
160   * @throws IllegalArgumentException if no path fragments are provided.
161   * @throws NullPointerException     if any of the path fragments are {@code null}.
162   * @since 4.0.0
163   */
164  FileBuilder createFile(List<String> fragments);
165
166  /**
167   * Get the identifying name of the temporary file system.
168   *
169   * @return the identifier string.
170   */
171  String getName();
172
173  /**
174   * Method that returns the object it is called upon to enable creating fluent-language builders.
175   *
176   * <p>For example:
177   *
178   * <pre><code>
179   *   thisDirectory
180   *       .createFile("foo", "bar", "baz.txt").withContents(...)
181   *       .and().then()
182   *       .createFile("foo", "bar", "bork.txt").withContents(...);
183   * </code></pre>
184   *
185   * @return this object.
186   * @see #and
187   * @see #also
188   */
189  default ManagedDirectory then() {
190    return this;
191  }
192
193}