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.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.jet.lang.resolve.name.Name;
027    import org.jetbrains.jet.lang.types.lang.PrimitiveType;
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 CHAR = new NamePredicate(PrimitiveType.CHAR.getTypeName());
064    
065        @NotNull
066        public static final NamePredicate LONG = new NamePredicate(PrimitiveType.LONG.getTypeName());
067    
068        @NotNull
069        private final Set<Name> validNames = Sets.newHashSet();
070    
071        public NamePredicate(@NotNull String... validNames) {
072            this(Arrays.asList(validNames));
073        }
074    
075        public NamePredicate(@NotNull List<String> validNames) {
076            for (String validName : validNames) {
077                this.validNames.add(Name.guess(validName));
078            }
079        }
080    
081        public NamePredicate(@NotNull Collection<Name> validNames) {
082            this.validNames.addAll(validNames);
083        }
084    
085        public NamePredicate(@NotNull Name... validNames) {
086            this.validNames.addAll(Lists.newArrayList(validNames));
087        }
088    
089        @Override
090        public boolean apply(@Nullable Name name) {
091            return name != null && validNames.contains(name);
092        }
093    }