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