2929
3030package org .scijava .code .lsp ;
3131
32+ import java .io .Reader ;
33+ import java .io .StringReader ;
34+ import java .io .Writer ;
3235import java .util .ArrayList ;
36+ import java .util .Arrays ;
3337import java .util .Collections ;
3438import java .util .List ;
3539import java .util .Objects ;
3640
41+ import javax .script .Bindings ;
3742import javax .script .ScriptContext ;
43+ import javax .script .SimpleBindings ;
3844
3945
4046/**
4955 */
5056public final class Environment {
5157
58+ /**
59+ * The state of no interpreter, for code not typed into one: no variables,
60+ * and none can be set.
61+ */
62+ public static final ScriptContext NO_STATE = new NoState ();
63+
5264 /** No particular environment. */
53- public static final Environment NONE = new Environment (null , null , null );
65+ public static final Environment NONE = new Environment (null , null );
5466
5567 private final String interpreter ;
5668 private final List <String > paths ;
5769 private final ScriptContext live ;
5870
71+ /**
72+ * An environment for code not typed into an interpreter (see
73+ * {@link #NO_STATE}).
74+ *
75+ * @param interpreter Path of the interpreter or executable parsing the code.
76+ * @param paths Extra places imports are resolved from, in order. Language-dependent.
77+ */
78+ public Environment (final String interpreter , final List <String > paths ) {
79+ this (interpreter , paths , NO_STATE );
80+ }
81+
5982 /**
6083 * @param interpreter Path of the interpreter or executable parsing the code.
6184 * @param paths Extra places imports are resolved from, in order. Language-dependent.
62- * @param live Existing state (e.g. from a REPL)
85+ * @param live The state of the interpreter the code is typed into (e.g. a
86+ * REPL's), or {@link #NO_STATE}.
6387 */
6488 public Environment (final String interpreter , final List <String > paths ,
6589 final ScriptContext live )
6690 {
6791 this .interpreter = interpreter ;
6892 this .paths = paths == null ? Collections .emptyList () : Collections
6993 .unmodifiableList (new ArrayList <>(paths ));
70- this .live = live ;
94+ this .live = Objects . requireNonNull ( live , "live" ) ;
7195 }
7296
7397 /** Path of the interpreter or executable running the script, or null. */
@@ -84,7 +108,10 @@ public List<String> paths() {
84108 return paths ;
85109 }
86110
87- /** The state of the interpreter the code is typed into, or null. */
111+ /**
112+ * The state of the interpreter the code is typed into; or
113+ * {@link #NO_STATE}, if none.
114+ */
88115 public ScriptContext live () {
89116 return live ;
90117 }
@@ -112,6 +139,102 @@ public int hashCode() {
112139 @ Override
113140 public String toString () {
114141 return "Environment[interpreter=" + interpreter + ", paths=" + paths +
115- (live == null ? "" : ", live" ) + "]" ;
142+ (live == NO_STATE ? "" : ", live" ) + "]" ;
143+ }
144+
145+ /** The state of no interpreter: see {@link #NO_STATE}. */
146+ private static final class NoState implements ScriptContext {
147+
148+ private static final Bindings NONE = new SimpleBindings (Collections
149+ .emptyMap ());
150+
151+ private static final Writer NO_OUTPUT = new Writer () {
152+
153+ @ Override
154+ public void write (final char [] buf , final int off , final int len ) {}
155+
156+ @ Override
157+ public void flush () {}
158+
159+ @ Override
160+ public void close () {}
161+ };
162+
163+ @ Override
164+ public void setBindings (final Bindings bindings , final int scope ) {
165+ throw unmodifiable ();
166+ }
167+
168+ @ Override
169+ public Bindings getBindings (final int scope ) {
170+ return NONE ;
171+ }
172+
173+ @ Override
174+ public void setAttribute (final String name , final Object value ,
175+ final int scope )
176+ {
177+ throw unmodifiable ();
178+ }
179+
180+ @ Override
181+ public Object getAttribute (final String name , final int scope ) {
182+ return null ;
183+ }
184+
185+ @ Override
186+ public Object removeAttribute (final String name , final int scope ) {
187+ return null ;
188+ }
189+
190+ @ Override
191+ public Object getAttribute (final String name ) {
192+ return null ;
193+ }
194+
195+ @ Override
196+ public int getAttributesScope (final String name ) {
197+ return -1 ;
198+ }
199+
200+ @ Override
201+ public Writer getWriter () {
202+ return NO_OUTPUT ;
203+ }
204+
205+ @ Override
206+ public Writer getErrorWriter () {
207+ return NO_OUTPUT ;
208+ }
209+
210+ @ Override
211+ public void setWriter (final Writer writer ) {
212+ throw unmodifiable ();
213+ }
214+
215+ @ Override
216+ public void setErrorWriter (final Writer writer ) {
217+ throw unmodifiable ();
218+ }
219+
220+ @ Override
221+ public Reader getReader () {
222+ return new StringReader ("" );
223+ }
224+
225+ @ Override
226+ public void setReader (final Reader reader ) {
227+ throw unmodifiable ();
228+ }
229+
230+ @ Override
231+ public List <Integer > getScopes () {
232+ return Arrays .asList (ENGINE_SCOPE , GLOBAL_SCOPE );
233+ }
234+
235+ private static UnsupportedOperationException unmodifiable () {
236+ return new UnsupportedOperationException (
237+ "The state of no interpreter cannot be changed" );
238+ }
116239 }
117240}
0 commit comments