Skip to content

Commit d0c23d2

Browse files
committed
Add EnvironmentSettings
1 parent 4cd3f12 commit d0c23d2

File tree

13 files changed

+461
-230
lines changed

13 files changed

+461
-230
lines changed

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
<maven.test.skip>true</maven.test.skip>
2121
</properties>
2222

23+
<distributionManagement>
24+
<snapshotRepository>
25+
<id>luck-snapshots</id>
26+
<url>https://nexus.lucko.me/repository/maven-snapshots/</url>
27+
</snapshotRepository>
28+
<repository>
29+
<id>luck-releases</id>
30+
<url>https://nexus.lucko.me/repository/maven-releases/</url>
31+
</repository>
32+
</distributionManagement>
33+
2334
<dependencies>
2435
<dependency>
2536
<groupId>junit</groupId>

src/main/java/me/lucko/scriptcontroller/ScriptController.java

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,13 @@
2525

2626
package me.lucko.scriptcontroller;
2727

28-
import me.lucko.scriptcontroller.bindings.BindingsSupplier;
2928
import me.lucko.scriptcontroller.environment.ScriptEnvironment;
30-
import me.lucko.scriptcontroller.environment.loader.ScriptLoadingExecutor;
29+
import me.lucko.scriptcontroller.environment.settings.EnvironmentSettings;
3130
import me.lucko.scriptcontroller.internal.ScriptControllerImpl;
3231
import me.lucko.scriptcontroller.logging.SystemLogger;
3332

3433
import java.nio.file.Path;
3534
import java.util.Collection;
36-
import java.util.concurrent.Executor;
37-
import java.util.concurrent.TimeUnit;
3835

3936
/**
4037
* Controls the execution and management of {@link ScriptEnvironment}s.
@@ -47,6 +44,7 @@ public interface ScriptController {
4744
* @return the builder
4845
*/
4946
static Builder builder() {
47+
//noinspection deprecation
5048
return ScriptControllerImpl.builder();
5149
}
5250

@@ -61,11 +59,24 @@ static Builder builder() {
6159
* Sets up a new {@link ScriptEnvironment} in the given load directory.
6260
*
6361
* @param loadDirectory the directory
62+
* @param settings the environment settings
6463
* @return the new environment
6564
* @throws UnsupportedOperationException if the controller does not support
6665
* setting up new environments after construction
6766
*/
68-
ScriptEnvironment setupNewEnvironment(Path loadDirectory);
67+
ScriptEnvironment setupNewEnvironment(Path loadDirectory, EnvironmentSettings settings);
68+
69+
/**
70+
* Sets up a new {@link ScriptEnvironment} in the given load directory.
71+
*
72+
* @param loadDirectory the directory
73+
* @return the new environment
74+
* @throws UnsupportedOperationException if the controller does not support
75+
* setting up new environments after construction
76+
*/
77+
default ScriptEnvironment setupNewEnvironment(Path loadDirectory) {
78+
return setupNewEnvironment(loadDirectory, EnvironmentSettings.defaults());
79+
}
6980

7081
/**
7182
* Shuts down this script controller
@@ -77,23 +88,6 @@ static Builder builder() {
7788
*/
7889
interface Builder {
7990

80-
/**
81-
* Define the executor service used to setup task to poll scripts for
82-
* changes and load new scripts.
83-
*
84-
* @param executor the executor
85-
* @return this builder
86-
*/
87-
Builder loadExecutor(ScriptLoadingExecutor executor);
88-
89-
/**
90-
* Define the executor used to run scripts
91-
*
92-
* @param executor the executor
93-
* @return this builder
94-
*/
95-
Builder runExecutor(Executor executor);
96-
9791
/**
9892
* Add a directory to be handled by this script controller
9993
*
@@ -103,29 +97,21 @@ interface Builder {
10397
Builder withDirectory(Path loadDirectory);
10498

10599
/**
106-
* Adds a bindings supplier to the controller
107-
*
108-
* @param supplier the bindings supplier
109-
* @return this builder
110-
*/
111-
Builder withBindings(BindingsSupplier supplier);
112-
113-
/**
114-
* Define how often the script loader should poll scripts for updates
100+
* Defines the logger to use.
115101
*
116-
* @param time the time
117-
* @param unit the unit
102+
* @param logger the logger
118103
* @return this builder
119104
*/
120-
Builder pollRate(long time, TimeUnit unit);
105+
Builder logger(SystemLogger logger);
121106

122107
/**
123-
* Defines the logger to use.
108+
* Defines the default {@link EnvironmentSettings} to use when this
109+
* controller creates new {@link ScriptEnvironment}s.
124110
*
125-
* @param logger the logger
111+
* @param settings the default settings
126112
* @return this builder
127113
*/
128-
Builder logger(SystemLogger logger);
114+
Builder defaultEnvironmentSettings(EnvironmentSettings settings);
129115

130116
/**
131117
* Builds a new {@link ScriptController} from the settings defined in

src/main/java/me/lucko/scriptcontroller/closable/AbstractWeakCompositeAutoClosable.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/main/java/me/lucko/scriptcontroller/closable/CompositeAutoClosable.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ static CompositeAutoClosable create() {
4545
return new AbstractCompositeAutoClosable();
4646
}
4747

48-
/**
49-
* Creates a new standalone {@link CompositeAutoClosable}, which wrapped
50-
* contained closables in {@link java.lang.ref.WeakReference}s.
51-
*
52-
* @return a new {@link CompositeAutoClosable}.
53-
*/
54-
static CompositeAutoClosable createWeak() {
55-
return new AbstractWeakCompositeAutoClosable();
56-
}
57-
5848
/**
5949
* Binds an {@link AutoCloseable} with this composite closable.
6050
*

src/main/java/me/lucko/scriptcontroller/environment/ScriptEnvironment.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import me.lucko.scriptcontroller.ScriptController;
2929
import me.lucko.scriptcontroller.environment.loader.SystemScriptLoader;
3030
import me.lucko.scriptcontroller.environment.registry.ScriptRegistry;
31+
import me.lucko.scriptcontroller.environment.settings.EnvironmentSettings;
3132
import me.lucko.scriptcontroller.exports.ExportRegistry;
3233

3334
import java.nio.file.Path;
@@ -47,6 +48,13 @@ public interface ScriptEnvironment extends AutoCloseable {
4748
*/
4849
ScriptController getController();
4950

51+
/**
52+
* Gets the environment settings
53+
*
54+
* @return the settings
55+
*/
56+
EnvironmentSettings getSettings();
57+
5058
/**
5159
* Gets the environments root scripts directory
5260
*
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* This file is part of scriptcontroller, licensed under the MIT License.
3+
*
4+
* Copyright (c) lucko (Luck) <luck@lucko.me>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
package me.lucko.scriptcontroller.environment.settings;
27+
28+
import me.lucko.scriptcontroller.bindings.BindingsSupplier;
29+
import me.lucko.scriptcontroller.environment.ScriptEnvironment;
30+
import me.lucko.scriptcontroller.environment.loader.ScriptLoadingExecutor;
31+
import me.lucko.scriptcontroller.internal.ScriptControllerImpl;
32+
33+
import java.util.concurrent.Executor;
34+
import java.util.concurrent.TimeUnit;
35+
36+
/**
37+
* Represents the settings for a given {@link ScriptEnvironment}.
38+
*/
39+
public interface EnvironmentSettings {
40+
41+
/**
42+
* Creates a new {@link Builder}.
43+
*
44+
* @return a new builder
45+
*/
46+
static Builder builder() {
47+
//noinspection deprecation
48+
return ScriptControllerImpl.newSettingsBuilder();
49+
}
50+
51+
/**
52+
* Returns a default set of environment settings
53+
*
54+
* @return the default settings
55+
*/
56+
static EnvironmentSettings defaults() {
57+
//noinspection deprecation
58+
return ScriptControllerImpl.defaultSettings();
59+
}
60+
61+
/**
62+
* Returns a builder encapsulating the properties already defined by this
63+
* instance
64+
*
65+
* @return a builder
66+
*/
67+
default Builder toBuilder() {
68+
return builder().mergeSettingsFrom(this);
69+
}
70+
71+
/**
72+
* Builds {@link EnvironmentSettings}
73+
*/
74+
interface Builder {
75+
76+
/**
77+
* Applies the settings from the give instance to this builder
78+
*
79+
* @param other the other settings
80+
* @return this builder
81+
*/
82+
Builder mergeSettingsFrom(EnvironmentSettings other);
83+
84+
/**
85+
* Define the executor service used to setup task to poll scripts for
86+
* changes and load new scripts.
87+
*
88+
* @param executor the executor
89+
* @return this builder
90+
*/
91+
Builder loadExecutor(ScriptLoadingExecutor executor);
92+
93+
/**
94+
* Define the executor used to run scripts
95+
*
96+
* @param executor the executor
97+
* @return this builder
98+
*/
99+
Builder runExecutor(Executor executor);
100+
101+
/**
102+
* Adds a bindings supplier to the settings
103+
*
104+
* @param supplier the bindings supplier
105+
* @return this builder
106+
*/
107+
Builder withBindings(BindingsSupplier supplier);
108+
109+
/**
110+
* Define how often the script loader should poll scripts for updates
111+
*
112+
* @param time the time
113+
* @param unit the unit
114+
* @return this builder
115+
*/
116+
Builder pollRate(long time, TimeUnit unit);
117+
118+
/**
119+
* Defines the init script for the environment
120+
*
121+
* @param path the path
122+
* @return this builder
123+
*/
124+
Builder initScript(String path);
125+
126+
/**
127+
* Builds a new {@link EnvironmentSettings} instance.
128+
*
129+
* @return the resultant environment settings
130+
*/
131+
EnvironmentSettings build();
132+
133+
}
134+
135+
}

0 commit comments

Comments
 (0)