001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.common.internal;
014
015/**
016 * Stores a Map of ASCII characters to Strings in a quick-lookup array.
017 */
018public final class AsciiMap {
019   final String[] store = new String[128];
020
021   /**
022    * Adds an entry to this map.
023    *
024    * @param c The key.
025    * @param s The value.
026    * @return This object.
027    */
028   public AsciiMap append(char c, String s) {
029      if (c <= 127)
030         store[c] = s;
031      return this;
032   }
033
034   /**
035    * Returns the value for the specified key.
036    *
037    * @param c The key.
038    * @return The value.
039    */
040   public String get(char c) {
041      return store[c];
042   }
043
044   /**
045    * Returns <jk>true</jk> if the specified character is in this store.
046    *
047    * @param c The character to check.
048    * @return <jk>true</jk> if the specified character is in this store.
049    */
050   public boolean contains(char c) {
051      if (c > 127)
052         return false;
053      return store[c] != null;
054   }
055
056   /**
057    * Returns <jk>true</jk> if the specified character is in this store.
058    *
059    * @param c The character to check.
060    * @return <jk>true</jk> if the specified character is in this store.
061    */
062   public boolean contains(int c) {
063      if (c < 0 || c > 127)
064         return false;
065      return store[c] != null;
066   }
067
068   /**
069    * Returns <jk>true</jk> if the specified string contains at least one character in this set.
070    *
071    * @param s The string to test.
072    * @return <jk>true</jk> if the string is not null and contains at least one character in this set.
073    */
074   public boolean contains(CharSequence s) {
075      if (s == null)
076         return false;
077      for (int i = 0; i < s.length(); i++)
078         if (contains(s.charAt(i)))
079            return true;
080      return false;
081   }
082}