001/*
002 * Copyright 2010-2013 JetBrains s.r.o.
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 */
016
017package org.jetbrains.jet.config;
018
019import com.intellij.openapi.util.Key;
020import org.jetbrains.annotations.NotNull;
021import org.jetbrains.annotations.Nullable;
022
023import java.util.*;
024
025public class CompilerConfiguration {
026    private final Map<Key, Object> map = new HashMap<Key, Object>();
027    private boolean readOnly = false;
028
029    @Nullable
030    public <T> T get(@NotNull CompilerConfigurationKey<T> key) {
031        T data = (T) map.get(key.ideaKey);
032        return data == null ? null : unmodifiable(data);
033    }
034
035    @NotNull
036    public <T> T get(@NotNull CompilerConfigurationKey<T> key, @NotNull T defaultValue) {
037        T data = (T) map.get(key.ideaKey);
038        return data == null ? defaultValue : unmodifiable(data);
039    }
040
041    @NotNull
042    public <T> List<T> getList(@NotNull CompilerConfigurationKey<List<T>> key) {
043        List<T> data = (List<T>) map.get(key.ideaKey);
044        if (data == null) {
045            return Collections.emptyList();
046        }
047        else {
048            return Collections.unmodifiableList(data);
049        }
050    }
051
052    public <T> void put(@NotNull CompilerConfigurationKey<T> key, @Nullable T value) {
053        checkReadOnly();
054        map.put(key.ideaKey, value);
055    }
056
057    public <T> void add(@NotNull CompilerConfigurationKey<List<T>> key, @NotNull T value) {
058        checkReadOnly();
059        Key<List<T>> ideaKey = key.ideaKey;
060        if (map.get(ideaKey) == null) {
061            map.put(ideaKey, new ArrayList<T>());
062        }
063        List<T> list = (List<T>) map.get(ideaKey);
064        list.add(value);
065    }
066
067    public <T> void addAll(@NotNull CompilerConfigurationKey<List<T>> key, @NotNull Collection<T> values) {
068        checkReadOnly();
069        checkForNullElements(values);
070        Key<List<T>> ideaKey = key.ideaKey;
071        if (map.get(ideaKey) == null) {
072            map.put(ideaKey, new ArrayList<T>());
073        }
074        List<T> list = (List<T>) map.get(ideaKey);
075        list.addAll(values);
076    }
077
078    public CompilerConfiguration copy() {
079        CompilerConfiguration copy = new CompilerConfiguration();
080        copy.map.putAll(map);
081        return copy;
082    }
083
084    private void checkReadOnly() {
085        if (readOnly) {
086            throw new IllegalStateException("CompilerConfiguration is read-only");
087        }
088    }
089
090    public void setReadOnly(boolean readOnly) {
091        if (readOnly != this.readOnly) {
092            checkReadOnly();
093            this.readOnly = readOnly;
094        }
095    }
096
097    @NotNull
098    private static <T> T unmodifiable(@NotNull T object) {
099        if (object instanceof List) {
100            return (T) Collections.unmodifiableList((List) object);
101        }
102        else if (object instanceof Map) {
103            return (T) Collections.unmodifiableMap((Map) object);
104        }
105        else if (object instanceof Collection) {
106            return (T) Collections.unmodifiableCollection((Collection) object);
107        }
108        else {
109            return object;
110        }
111    }
112
113    private static <T> void checkForNullElements(Collection<T> values) {
114        int index = 0;
115        for (T value : values) {
116            if (value == null) {
117                throw new IllegalArgumentException("Element " + index
118                                                   + " is null, while null values in compiler configuration are not allowed");
119            }
120            index++;
121        }
122    }
123}