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.util.lazy;
018    
019    public abstract class RecursionIntolerantLazyValue<T> {
020    
021        private enum State {
022            NOT_COMPUTED,
023            BEING_COMPUTED,
024            COMPUTED,
025            ERROR
026        }
027    
028        private State state = State.NOT_COMPUTED;
029        private T value;
030    
031        protected abstract T compute();
032    
033        protected T getValueOnErrorReentry() {
034            throw new ReenteringLazyValueComputationException();
035        }
036    
037        public boolean isComputed() {
038            return state == State.ERROR || state == State.COMPUTED;
039        }
040    
041        public final T get() {
042            switch (state) {
043                case NOT_COMPUTED:
044                    state = State.BEING_COMPUTED;
045                    value = compute();
046                    state = State.COMPUTED;
047                    return value;
048                case BEING_COMPUTED:
049                    state = State.ERROR;
050                    throw new ReenteringLazyValueComputationException();
051                case COMPUTED:
052                    return value;
053                case ERROR:
054                    return getValueOnErrorReentry();
055            }
056            throw new IllegalStateException("Unreachable");
057        }
058    
059    
060    
061    }