001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to you under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package gudusoft.gsqlparser.ext.calcite.sqlnamematcher; 018 019import org.checkerframework.checker.nullness.qual.Nullable; 020 021import java.util.ArrayList; 022import java.util.List; 023import java.util.Map; 024import java.util.Set; 025 026/** 027 * Checks whether two names are the same according to a case-sensitivity policy. 028 * 029 * <p>This is a standalone version that can be used independently of Apache Calcite. 030 * 031 * @see SqlNameMatchers 032 */ 033public interface SqlNameMatcher { 034 /** Returns whether name matching is case-sensitive. */ 035 boolean isCaseSensitive(); 036 037 /** Returns whether a name matches another. 038 * 039 * @param string Name written in code 040 * @param name Name of object we are trying to match 041 * @return Whether matches 042 */ 043 boolean matches(String string, String name); 044 045 /** Looks up an item in a map. 046 * 047 * @param map Map to search 048 * @param prefixNames Prefix names to prepend 049 * @param names Names to search for 050 * @return Value if found, null otherwise 051 */ 052 <K extends List<String>, V> @Nullable V get(Map<K, V> map, List<String> prefixNames, 053 List<String> names); 054 055 /** Returns the most recent match. 056 * 057 * <p>In the default implementation, 058 * throws {@link UnsupportedOperationException}. */ 059 String bestString(); 060 061 /** Returns how many times a string occurs in a collection. 062 * 063 * <p>Similar to {@link java.util.Collections#frequency}. */ 064 int frequency(Iterable<String> names, String name); 065 066 /** Returns a copy of a collection, removing duplicates and retaining 067 * iteration order. */ 068 default List<String> distinctCopy(Iterable<String> names) { 069 final List<String> list = new ArrayList<>(); 070 final Set<String> set = createSet(); 071 for (String name : names) { 072 if (set.add(name)) { 073 list.add(name); 074 } 075 } 076 return list; 077 } 078 079 /** Returns the index of the first element of a collection that matches. */ 080 default int indexOf(Iterable<String> names, String name) { 081 int i = 0; 082 for (String n : names) { 083 if (matches(n, name)) { 084 return i; 085 } 086 i++; 087 } 088 return -1; 089 } 090 091 /** Creates a set that has the same case-sensitivity as this matcher. */ 092 Set<String> createSet(); 093}