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