Added read-only mode for complier configuration.

This commit is contained in:
Evgeny Gerashchenko
2012-07-24 17:15:05 +04:00
parent edb2e0e3b6
commit 17e3738084
2 changed files with 39 additions and 3 deletions
@@ -76,7 +76,8 @@ public class JetCoreEnvironment {
private boolean initialized = false; private boolean initialized = false;
public JetCoreEnvironment(Disposable parentDisposable, @NotNull CompilerConfiguration configuration) { public JetCoreEnvironment(Disposable parentDisposable, @NotNull CompilerConfiguration configuration) {
this.configuration = configuration; this.configuration = configuration.copy();
this.configuration.setReadOnly(true);
this.applicationEnvironment = new JavaCoreApplicationEnvironment(parentDisposable); this.applicationEnvironment = new JavaCoreApplicationEnvironment(parentDisposable);
applicationEnvironment.registerFileType(JetFileType.INSTANCE, "kt"); applicationEnvironment.registerFileType(JetFileType.INSTANCE, "kt");
@@ -24,6 +24,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
/** /**
@@ -32,16 +33,18 @@ import java.util.concurrent.CopyOnWriteArrayList;
*/ */
public class CompilerConfiguration { public class CompilerConfiguration {
private final UserDataHolderBase holder = new UserDataHolderBase(); private final UserDataHolderBase holder = new UserDataHolderBase();
private boolean readOnly = false;
@Nullable @Nullable
public <T> T get(@NotNull Key<T> key) { public <T> T get(@NotNull Key<T> key) {
return holder.getUserData(key); T data = holder.getUserData(key);
return data == null ? null : unmodifiable(data);
} }
@NotNull @NotNull
public <T> T get(@NotNull Key<T> key, @NotNull T defaultValue) { public <T> T get(@NotNull Key<T> key, @NotNull T defaultValue) {
T data = holder.getUserData(key); T data = holder.getUserData(key);
return data == null ? defaultValue : data; return data == null ? defaultValue : unmodifiable(data);
} }
@NotNull @NotNull
@@ -56,15 +59,18 @@ public class CompilerConfiguration {
} }
public <T> void put(@NotNull Key<T> key, @Nullable T value) { public <T> void put(@NotNull Key<T> key, @Nullable T value) {
checkReadOnly();
holder.putUserData(key, value); holder.putUserData(key, value);
} }
public <T> void add(@NotNull Key<List<T>> key, @NotNull T value) { public <T> void add(@NotNull Key<List<T>> key, @NotNull T value) {
checkReadOnly();
List<T> list = holder.putUserDataIfAbsent(key, new CopyOnWriteArrayList<T>()); List<T> list = holder.putUserDataIfAbsent(key, new CopyOnWriteArrayList<T>());
list.add(value); list.add(value);
} }
public <T> void addAll(@NotNull Key<List<T>> key, @NotNull Collection<T> value) { public <T> void addAll(@NotNull Key<List<T>> key, @NotNull Collection<T> value) {
checkReadOnly();
List<T> list = holder.putUserDataIfAbsent(key, new CopyOnWriteArrayList<T>()); List<T> list = holder.putUserDataIfAbsent(key, new CopyOnWriteArrayList<T>());
list.addAll(value); list.addAll(value);
} }
@@ -74,4 +80,33 @@ public class CompilerConfiguration {
holder.copyUserDataTo(copy.holder); holder.copyUserDataTo(copy.holder);
return copy; return copy;
} }
private void checkReadOnly() {
if (readOnly) {
throw new IllegalStateException("CompilerConfiguration is read-only");
}
}
public void setReadOnly(boolean readOnly) {
if (readOnly != this.readOnly) {
checkReadOnly();
this.readOnly = readOnly;
}
}
@NotNull
private static <T> T unmodifiable(@NotNull T object) {
if (object instanceof List) {
return (T) Collections.unmodifiableList((List) object);
}
else if (object instanceof Map) {
return (T) Collections.unmodifiableMap((Map) object);
}
else if (object instanceof Collection) {
return (T) Collections.unmodifiableCollection((Collection) object);
}
else {
return object;
}
}
} }