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