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 org.apache.juneau.commons.lang; 018 019import static org.apache.juneau.commons.utils.Utils.*; 020 021/** 022 * Stores a Map of ASCII characters to Strings in a quick-lookup array. 023 */ 024public class AsciiMap { 025 final String[] store = new String[128]; 026 027 /** 028 * Adds an entry to this map. 029 * 030 * @param c The key. 031 * @param s The value. 032 * @return This object. 033 */ 034 public AsciiMap append(char c, String s) { 035 if (c <= 127) 036 store[c] = s; 037 return this; 038 } 039 040 /** 041 * Returns <jk>true</jk> if the specified character is in this store. 042 * 043 * @param c The character to check. 044 * @return <jk>true</jk> if the specified character is in this store. 045 */ 046 public boolean contains(char c) { 047 if (c > 127) 048 return false; 049 return nn(store[c]); 050 } 051 052 /** 053 * Returns <jk>true</jk> if the specified string contains at least one character in this set. 054 * 055 * @param s The string to test. 056 * @return <jk>true</jk> if the string is not null and contains at least one character in this set. 057 */ 058 public boolean contains(CharSequence s) { 059 if (s == null) 060 return false; 061 for (var i = 0; i < s.length(); i++) 062 if (contains(s.charAt(i))) 063 return true; 064 return false; 065 } 066 067 /** 068 * Returns <jk>true</jk> if the specified character is in this store. 069 * 070 * @param c The character to check. 071 * @return <jk>true</jk> if the specified character is in this store. 072 */ 073 public boolean contains(int c) { 074 if (c < 0 || c > 127) 075 return false; 076 return nn(store[c]); 077 } 078 079 /** 080 * Returns the value for the specified key. 081 * 082 * @param c The key. 083 * @return The value. 084 */ 085 public String get(char c) { 086 return store[c]; 087 } 088}