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.k2js.translate.intrinsic.functions;
018
019import com.google.common.collect.Lists;
020import com.google.common.collect.Maps;
021import org.jetbrains.annotations.NotNull;
022import org.jetbrains.annotations.Nullable;
023import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
024import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
025import org.jetbrains.k2js.translate.intrinsic.functions.factories.*;
026
027import java.util.List;
028import java.util.Map;
029
030public final class FunctionIntrinsics {
031
032    @NotNull
033    private final Map<FunctionDescriptor, FunctionIntrinsic> intrinsicCache = Maps.newHashMap();
034
035    @NotNull
036    private final List<FunctionIntrinsicFactory> factories = Lists.newArrayList();
037
038    public FunctionIntrinsics() {
039        registerFactories();
040    }
041
042    private void registerFactories() {
043        register(PrimitiveUnaryOperationFIF.INSTANCE);
044        register(PrimitiveBinaryOperationFIF.INSTANCE);
045        register(StringOperationFIF.INSTANCE);
046        register(ArrayFIF.INSTANCE);
047        register(TopLevelFIF.INSTANCE);
048        register(NumberConversionFIF.INSTANCE);
049    }
050
051    private void register(@NotNull FunctionIntrinsicFactory instance) {
052        factories.add(instance);
053    }
054
055    @NotNull
056    public FunctionIntrinsic getIntrinsic(@NotNull FunctionDescriptor descriptor) {
057        FunctionIntrinsic intrinsic = lookUpCache(descriptor);
058        if (intrinsic != null) {
059            return intrinsic;
060        }
061        intrinsic = computeAndCacheIntrinsic(descriptor);
062        return intrinsic;
063    }
064
065    @Nullable
066    private FunctionIntrinsic lookUpCache(@NotNull FunctionDescriptor descriptor) {
067        return intrinsicCache.get(descriptor);
068    }
069
070    @NotNull
071    private FunctionIntrinsic computeAndCacheIntrinsic(@NotNull FunctionDescriptor descriptor) {
072        FunctionIntrinsic result = computeIntrinsic(descriptor);
073        intrinsicCache.put(descriptor, result);
074        return result;
075    }
076
077    @NotNull
078    private FunctionIntrinsic computeIntrinsic(@NotNull FunctionDescriptor descriptor) {
079        for (FunctionIntrinsicFactory factory : factories) {
080            if (factory.getPredicate().apply(descriptor)) {
081                return factory.getIntrinsic(descriptor);
082            }
083        }
084        return FunctionIntrinsic.NO_INTRINSIC;
085    }
086}