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.internal;
014
015import static org.apache.juneau.internal.ArrayUtils.*;
016import static org.apache.juneau.internal.ThrowableUtils.*;
017
018import java.util.*;
019
020import org.apache.juneau.*;
021
022/**
023 * An instance of a <code>Map</code> where the keys and values are simple <code>String[]</code> and
024 * <code>Object[]</code> arrays.
025 *
026 * <p>
027 * Typically more efficient than <code>HashMaps</code> for small maps (e.g. &lt;10 entries).
028 *
029 * <p>
030 * Does not support adding or removing entries.
031 *
032 * <p>
033 * Setting values overwrites the value on the underlying value array.
034 */
035public final class SimpleMap extends AbstractMap<String,Object> {
036
037   final String[] keys;
038   final Object[] values;
039   final Map.Entry<String,Object>[] entries;
040
041   /**
042    * Constructor.
043    *
044    * @param keys The map keys.  Must not be <jk>null</jk>.
045    * @param values The map values.  Must not be <jk>null</jk>.
046    */
047   public SimpleMap(String[] keys, Object[] values) {
048      assertFieldNotNull(keys, "keys");
049      assertFieldNotNull(values, "values");
050      if (keys.length != values.length)
051         illegalArg("keys ''{0}'' and values ''{1}'' array lengths differ", keys.length, values.length);
052
053      this.keys = keys;
054      this.values = values;
055      entries = new SimpleMapEntry[keys.length];
056      for (int i = 0; i < keys.length; i++) {
057         if (keys[i] == null)
058            illegalArg("Keys array cannot contain a null value.");
059         entries[i] = new SimpleMapEntry(i);
060   }
061   }
062
063   @Override /* Map */
064   public Set<Map.Entry<String,Object>> entrySet() {
065      return asSet(entries);
066   }
067
068   @Override /* Map */
069   public Object get(Object key) {
070      for (int i = 0; i < keys.length; i++)
071         if (keys[i].equals(key))
072            return values[i];
073      return null;
074   }
075
076   @Override /* Map */
077   public Set<String> keySet() {
078      return asSet(keys);
079   }
080
081   @Override /* Map */
082   public Object put(String key, Object value) {
083      for (int i = 0; i < keys.length; i++) {
084         if (keys[i].equals(key)) {
085            Object v = values[i];
086            values[i] = value;
087            return v;
088         }
089      }
090      throw new FormattedIllegalArgumentException("No key ''{0}'' defined in map", key);
091   }
092
093   final class SimpleMapEntry implements Map.Entry<String,Object> {
094
095      private int index;
096
097      SimpleMapEntry(int index) {
098         this.index = index;
099      }
100
101      @Override /* Map.Entry */
102      public String getKey() {
103         return keys[index];
104      }
105
106      @Override /* Map.Entry */
107      public Object getValue() {
108         return values[index];
109      }
110
111      @Override /* Map.Entry */
112      public Object setValue(Object val) {
113         Object v = values[index];
114         values[index] = val;
115         return v;
116      }
117   }
118}