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