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.k2js.translate.intrinsic.functions.patterns;
018    
019    import com.google.common.base.Predicate;
020    import com.google.common.collect.Lists;
021    import com.intellij.util.Function;
022    import com.intellij.util.containers.ContainerUtil;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.annotations.Nullable;
025    import org.jetbrains.jet.lang.resolve.name.Name;
026    import org.jetbrains.jet.lang.types.lang.PrimitiveType;
027    
028    import java.util.Arrays;
029    import java.util.Collection;
030    import java.util.List;
031    
032    public final class NamePredicate implements Predicate<Name> {
033    
034        @NotNull
035        public static final NamePredicate PRIMITIVE_NUMBERS = new NamePredicate(
036                ContainerUtil.map(PrimitiveType.NUMBER_TYPES, new Function<PrimitiveType, String>() {
037            @Override
038            public String fun(PrimitiveType type) {
039                return type.getTypeName().asString();
040            }
041        }));
042    
043        @NotNull
044        public static final NamePredicate STRING = new NamePredicate("String");
045    
046        @NotNull
047        private final List<Name> validNames = Lists.newArrayList();
048    
049        public NamePredicate(@NotNull String... validNames) {
050            this(Arrays.asList(validNames));
051        }
052    
053        public NamePredicate(@NotNull List<String> validNames) {
054            for (String validName : validNames) {
055                this.validNames.add(Name.guess(validName));
056            }
057        }
058    
059        public NamePredicate(@NotNull Collection<Name> validNames) {
060            this.validNames.addAll(validNames);
061        }
062    
063        public NamePredicate(@NotNull Name... validNames) {
064            this.validNames.addAll(Lists.newArrayList(validNames));
065        }
066    
067        @Override
068        public boolean apply(@Nullable Name name) {
069            if (name == null) {
070                return false;
071            }
072            for (Name validName : validNames) {
073                if (name.equals(validName)) {
074                    return true;
075                }
076            }
077            return false;
078        }
079    }