Eljay's coworker is afflicted with the rather embarrassing condition of premature optimization. Every few blocks of code, he'll start to panic, worrying that his code isn't fast enough, that he's wasting too many resources, that he's just not doing it perfect. As is often the case, this condition creates some rather entertaining (though often buggy and less efficient) code. The FastMap demonstrates this, employing the superstitious belief that a HashMap with less than five values is just too slow ...

package initech.utils;

import java.util.HashMap;
import java.util.Map;

public class FastMap implements Map, java.io.Serializable
{
   protected Map useHashMap = null;

   int size;
   String nam1 = null;
   String nam2 = null;
   String nam3 = null;
   String nam4 = null;
   Object val1 = null;
   Object val2 = null;
   Object val3 = null;
   Object val4 = null;

   public FastMap() {}

   public FastMap(String nam1, Object val1)
   {
      size = 1;
      this.nam1 = nam1;
      this.val1 = val1;
   }

   public FastMap(String nam1, Object val1, String nam2, Object val2)
   {
      size = 2;
      this.nam1 = nam1;
      this.val1 = val1;
      this.nam2 = nam2;
      this.val2 = val2;
   }

   public FastMap(String nam1, Object val1, String nam2, Object val2, String nam3, Object val3)
   {
      size = 3;
      this.nam1 = nam1;
      this.val1 = val1;
      this.nam2 = nam2;
      this.val2 = val2;
      this.nam3 = nam3;
      this.val3 = val3;
   }

   public FastMap(String nam1, Object val1, String nam2, Object val2, String nam3, Object val3, String nam4, Object val4)
   {
      size = 4;
      this.nam1 = nam1;
      this.val1 = val1;
      this.nam2 = nam2;
      this.val2 = val2;
      this.nam3 = nam3;
      this.val3 = val3;
      this.nam4 = nam4;
      this.val4 = val4;
   }

   protected void createRealHashMap()
   {
      useHashMap = new HashMap();
      if (nam1 != null) useHashMap.put(nam1, val1);
      if (nam2 != null) useHashMap.put(nam2, val2);
      if (nam3 != null) useHashMap.put(nam3, val3);
      if (nam4 != null) useHashMap.put(nam4, val4);
   }

   public void clear()
   {
      if (useHashMap != null)
      {
         useHashMap.clear();
      }
      else
      {
         useHashMap = new HashMap();
         nam1 = null;
         nam2 = null;
         nam3 = null;
         nam4 = null;
         val1 = null;
         val2 = null;
         val3 = null;
         val4 = null;
      }
   }

   public boolean containsKey(Object obj)
   {
      if (useHashMap != null)
      {
         return useHashMap.containsKey(obj);
      }
      else
      {
         if (nam1 != null && nam1.equals(obj)) return true;
         if (nam2 != null && nam2.equals(obj)) return true;
         if (nam3 != null && nam3.equals(obj)) return true;
         if (nam4 != null && nam4.equals(obj)) return true;
         return false;
      }
   }

   public boolean containsValue(Object obj)
   {
      if (useHashMap != null)
      {
         return useHashMap.containsValue(obj);
      }
      else
      {
         if (val1 != null && val1.equals(obj)) return true;
         if (val2 != null && val2.equals(obj)) return true;
         if (val3 != null && val3.equals(obj)) return true;
         if (val4 != null && val4.equals(obj)) return true;
         return false;
      }
   }

   public java.util.Set entrySet()
   {
      if (useHashMap != null)
      {
         return useHashMap.entrySet();
      }
      else
      {
         this.createRealHashMap();
         return useHashMap.entrySet();
      }
   }

   public Object get(Object obj)
   {
      if (useHashMap != null)
      {
         return useHashMap.get(obj);
      }
      else
      {
         if (nam1 != null && nam1.equals(obj)) return val1;
         if (nam2 != null && nam2.equals(obj)) return val2;
         if (nam3 != null && nam3.equals(obj)) return val3;
         if (nam4 != null && nam4.equals(obj)) return val4;
         return null;
      }
   }

   public boolean isEmpty()
   {
      if (useHashMap != null)
      {
         return useHashMap.isEmpty();
      }
      else
      {
         if (this.size == 0) return true;
         return false;
      }
   }

   public java.util.Set keySet()
   {
      if (useHashMap != null)
      {
         return useHashMap.keySet();
      }
      else
      {
         this.createRealHashMap();
         return useHashMap.keySet();
      }
   }

   public Object put(Object obj, Object obj1)
   {
      if (useHashMap != null)
      {
         return useHashMap.put(obj, obj1);
      }
      else
      {
         this.createRealHashMap();
         return useHashMap.put(obj, obj1);
      }
   }

   public void putAll(java.util.Map map)
   {
      if (useHashMap != null)
      {
         useHashMap.putAll(map);
      }
      else
      {
         this.createRealHashMap();
         useHashMap.putAll(map);
      }
   }

   public Object remove(Object obj)
   {
      if (useHashMap != null)
      {
         return useHashMap.remove(obj);
      }
      else
      {
         this.createRealHashMap();
         return useHashMap.remove(obj);
      }
   }

   public int size()
   {
      if (useHashMap != null)
      {
         return useHashMap.size();
      }
      else
      {
         return this.size;
      }
   }

   public java.util.Collection values()
   {
      if (useHashMap != null)
      {
         return useHashMap.values();
      }
      else
      {
         this.createRealHashMap();
         return useHashMap.values();
      }
   }

   public String toString()
   {
      StringBuffer strOut = new StringBuffer("{");

      if (nam1 != null)
      {
         strOut.append('{');
         strOut.append(nam1);
         strOut.append(',');
         strOut.append(val1);
         strOut.append('}');
      }
      if (nam2 != null)
      {
         if (strOut.length() > 1) strOut.append(',');
         strOut.append('{');
         strOut.append(nam2);
         strOut.append(',');
         strOut.append(val2);
         strOut.append('}');
      }
      if (nam3 != null)
      {
         if (strOut.length() > 1) strOut.append(',');
         strOut.append('{');
         strOut.append(nam3);
         strOut.append(',');
         strOut.append(val3);
         strOut.append('}');
      }
      if (nam4 != null)
      {
         if (strOut.length() > 1) strOut.append(',');
         strOut.append('{');
         strOut.append(nam4);
         strOut.append(',');
         strOut.append(val4);
         strOut.append('}');
      }
      strOut.append('}');
      return strOut.toString();
   }
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!