/* * This file is part of SpoutcraftPlugin. * * Copyright (c) 2011 SpoutcraftDev <http://spoutcraft.org//> * SpoutcraftPlugin is licensed under the GNU Lesser General Public License. * * SpoutcraftPlugin is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * SpoutcraftPlugin is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.getspout.spoutapi.util.map; import java.util.Collection; import gnu.trove.iterator.TIntObjectIterator; import gnu.trove.map.hash.TIntObjectHashMap; import gnu.trove.set.TIntSet; /** * A simplistic map that supports a 3 bytes for keys, using a trove int Object hashmap in the backend. */ public class TByteTripleObjectHashMap<K>{ private TIntObjectHashMap<K> map; public TByteTripleObjectHashMap() { map = new TIntObjectHashMap<K>(100); } public TByteTripleObjectHashMap(int capacity) { map = new TIntObjectHashMap<K>(capacity); } public K put(byte key1, byte key2, byte key3, K value) { int key = key(key1, key2, key3); return map.put(key, value); } public K get(byte key1, byte key2, byte key3) { int key = key(key1, key2, key3); return map.get(key); } public boolean containsKey(byte key1, byte key2, byte key3) { int key = key(key1, key2, key3); return map.containsKey(key); } public void clear() { map.clear(); } public boolean containsValue(K val) { return map.containsValue(val); } public boolean isEmpty() { return map.isEmpty(); } public TIntObjectIterator<K> iterator() { return map.iterator(); } public TIntSet keySet() { return map.keySet(); } public int[] keys() { return map.keys(); } public K remove(byte key1, byte key2, byte key3) { int key = key(key1, key2, key3); return map.remove(key); } public int size() { return map.size(); } public Collection<K> valueCollection() { return map.valueCollection(); } @SuppressWarnings("unchecked") public K[] values() { return (K[]) map.values(); } private static final int key(int x, int y, int z) { return (x & 0xF) << 11 | (z & 0xF) << 7 | (y & 0x7F); } }