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    
017    package org.jetbrains.jet.analyzer;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
022    import org.jetbrains.jet.lang.resolve.BindingContext;
023    import org.jetbrains.jet.lang.types.ErrorUtils;
024    
025    public class AnalyzeExhaust {
026        public static final AnalyzeExhaust EMPTY = success(BindingContext.EMPTY, ErrorUtils.getErrorModule());
027    
028        @NotNull
029        public static AnalyzeExhaust success(@NotNull BindingContext bindingContext, @NotNull ModuleDescriptor module) {
030            return new AnalyzeExhaust(bindingContext, module, null);
031        }
032    
033        @NotNull
034        public static AnalyzeExhaust error(@NotNull BindingContext bindingContext, @NotNull Throwable error) {
035            return new AnalyzeExhaust(bindingContext, ErrorUtils.getErrorModule(), error);
036        }
037    
038        private final BindingContext bindingContext;
039        private final Throwable error;
040        private final ModuleDescriptor moduleDescriptor;
041    
042        private AnalyzeExhaust(
043                @NotNull BindingContext bindingContext,
044                @NotNull ModuleDescriptor moduleDescriptor,
045                @Nullable Throwable error
046        ) {
047            this.bindingContext = bindingContext;
048            this.error = error;
049            this.moduleDescriptor = moduleDescriptor;
050        }
051    
052        @NotNull
053        public BindingContext getBindingContext() {
054            return bindingContext;
055        }
056    
057        @NotNull
058        public Throwable getError() {
059            if (error == null) throw new IllegalStateException("Should be called only for error analyze result");
060            return error;
061        }
062    
063        public boolean isError() {
064            return error != null;
065        }
066    
067        public void throwIfError() {
068            if (isError()) {
069                throw new IllegalStateException("failed to analyze: " + error, error);
070            }
071        }
072    
073        @NotNull
074        public ModuleDescriptor getModuleDescriptor() {
075            return moduleDescriptor;
076        }
077    }