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.patterns; 018 019import com.google.common.base.Predicate; 020import com.google.common.collect.Lists; 021import com.intellij.util.Function; 022import com.intellij.util.containers.ContainerUtil; 023import org.jetbrains.annotations.NotNull; 024import org.jetbrains.annotations.Nullable; 025import org.jetbrains.jet.lang.resolve.name.Name; 026import org.jetbrains.jet.lang.types.lang.PrimitiveType; 027 028import java.util.Arrays; 029import java.util.Collection; 030import java.util.List; 031 032public 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 private final List<Name> validNames = Lists.newArrayList(); 045 046 public NamePredicate(@NotNull String... validNames) { 047 this(Arrays.asList(validNames)); 048 } 049 050 public NamePredicate(@NotNull List<String> validNames) { 051 for (String validName : validNames) { 052 this.validNames.add(Name.guess(validName)); 053 } 054 } 055 056 public NamePredicate(@NotNull Collection<Name> validNames) { 057 this.validNames.addAll(validNames); 058 } 059 060 public NamePredicate(@NotNull Name... validNames) { 061 this.validNames.addAll(Lists.newArrayList(validNames)); 062 } 063 064 @Override 065 public boolean apply(@Nullable Name name) { 066 if (name == null) { 067 return false; 068 } 069 for (Name validName : validNames) { 070 if (name.equals(validName)) { 071 return true; 072 } 073 } 074 return false; 075 } 076}