001    /*
002     * Copyright 2010-2015 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.kotlin.js.patterns;
018    
019    import com.google.common.base.Predicate;
020    import com.google.common.collect.Lists;
021    import com.google.common.collect.Sets;
022    import com.intellij.util.Function;
023    import com.intellij.util.containers.ContainerUtil;
024    import org.jetbrains.annotations.NotNull;
025    import org.jetbrains.annotations.Nullable;
026    import org.jetbrains.kotlin.builtins.PrimitiveType;
027    import org.jetbrains.kotlin.name.Name;
028    
029    import java.util.Arrays;
030    import java.util.Collection;
031    import java.util.List;
032    import java.util.Set;
033    
034    public final class NamePredicate implements Predicate<Name> {
035    
036        @NotNull
037        public static final NamePredicate PRIMITIVE_NUMBERS = new NamePredicate(
038                ContainerUtil.map(PrimitiveType.NUMBER_TYPES,
039                                  new Function<PrimitiveType, String>() {
040                                      @Override
041                                      public String fun(PrimitiveType type) {
042                                          return type.getTypeName().asString();
043                                      }
044                                  }));
045    
046        @NotNull
047        public static final NamePredicate PRIMITIVE_NUMBERS_MAPPED_TO_PRIMITIVE_JS = new NamePredicate(
048                ContainerUtil.mapNotNull(PrimitiveType.NUMBER_TYPES,
049                                  new Function<PrimitiveType, String>() {
050                                      @Override
051                                      public String fun(PrimitiveType type) {
052                                          return type != PrimitiveType.LONG ? type.getTypeName().asString() : null;
053                                      }
054                                  }));
055    
056        @NotNull
057        public static final NamePredicate STRING = new NamePredicate("String");
058    
059        @NotNull
060        public static final NamePredicate NUMBER = new NamePredicate("Number");
061    
062        @NotNull
063        public static final NamePredicate BOOLEAN = new NamePredicate("Boolean");
064    
065        @NotNull
066        public static final NamePredicate CHAR = new NamePredicate(PrimitiveType.CHAR.getTypeName());
067    
068        @NotNull
069        public static final NamePredicate LONG = new NamePredicate(PrimitiveType.LONG.getTypeName());
070    
071        @NotNull
072        private final Set<Name> validNames = Sets.newHashSet();
073    
074        public NamePredicate(@NotNull String... validNames) {
075            this(Arrays.asList(validNames));
076        }
077    
078        public NamePredicate(@NotNull List<String> validNames) {
079            for (String validName : validNames) {
080                this.validNames.add(Name.guess(validName));
081            }
082        }
083    
084        public NamePredicate(@NotNull Collection<Name> validNames) {
085            this.validNames.addAll(validNames);
086        }
087    
088        public NamePredicate(@NotNull Name... validNames) {
089            this.validNames.addAll(Lists.newArrayList(validNames));
090        }
091    
092        @Override
093        public boolean apply(@Nullable Name name) {
094            return name != null && validNames.contains(name);
095        }
096    }