Coverage Report - org.jaudiotagger.tag.datatype.NumberHashMap
 
Classes in this File Line Coverage Branch Coverage Complexity
NumberHashMap
64%
54/85
56%
30/54
0
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: NumberHashMap.java,v 1.14 2008/07/21 10:45:41 paultaylor Exp $
 6  
  *
 7  
  *  MusicTag Copyright (C)2003,2004
 8  
  *
 9  
  *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
 10  
  *  General Public  License as published by the Free Software Foundation; either version 2.1 of the License,
 11  
  *  or (at your option) any later version.
 12  
  *
 13  
  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 14  
  *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 15  
  *  See the GNU Lesser General Public License for more details.
 16  
  *
 17  
  *  You should have received a copy of the GNU Lesser General Public License along with this library; if not,
 18  
  *  you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
 19  
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 20  
  *
 21  
  * Description:
 22  
  *
 23  
  */
 24  
 package org.jaudiotagger.tag.datatype;
 25  
 
 26  
 import org.jaudiotagger.logging.ErrorMessage;
 27  
 import org.jaudiotagger.tag.InvalidDataTypeException;
 28  
 import org.jaudiotagger.tag.id3.AbstractTagFrameBody;
 29  
 import org.jaudiotagger.tag.id3.valuepair.*;
 30  
 import org.jaudiotagger.tag.reference.GenreTypes;
 31  
 import org.jaudiotagger.tag.reference.PictureTypes;
 32  
 
 33  
 import java.util.Iterator;
 34  
 import java.util.Map;
 35  
 import java.util.TreeSet;
 36  
 
 37  
 /**
 38  
  * Represents a number thats acts as a key into an enumeration of values
 39  
  */
 40  
 public class NumberHashMap extends NumberFixedLength implements HashMapInterface<Integer, String>
 41  
 {
 42  
 
 43  
     /**
 44  
      * key to value map
 45  
      */
 46  3211
     private Map<Integer, String> keyToValue = null;
 47  
 
 48  
     /**
 49  
      * value to key map
 50  
      */
 51  3211
     private Map<String, Integer> valueToKey = null;
 52  
 
 53  
     /**
 54  
      *
 55  
      */
 56  3211
     private boolean hasEmptyValue = false;
 57  
 
 58  
 
 59  
     /**
 60  
      * Creates a new ObjectNumberHashMap datatype.
 61  
      *
 62  
      * @param identifier
 63  
      * @param size
 64  
      * @throws IllegalArgumentException
 65  
      */
 66  
     public NumberHashMap(String identifier, AbstractTagFrameBody frameBody, int size)
 67  
     {
 68  2057
         super(identifier, frameBody, size);
 69  
 
 70  2057
         if (identifier.equals(DataTypes.OBJ_GENRE))
 71  
         {
 72  0
             valueToKey = GenreTypes.getInstanceOf().getValueToIdMap();
 73  0
             keyToValue = GenreTypes.getInstanceOf().getIdToValueMap();
 74  
 
 75  
             //genres can be an id or literal value
 76  0
             hasEmptyValue = true;
 77  
         }
 78  2057
         else if (identifier.equals(DataTypes.OBJ_TEXT_ENCODING))
 79  
         {
 80  1940
             valueToKey = TextEncoding.getInstanceOf().getValueToIdMap();
 81  1940
             keyToValue = TextEncoding.getInstanceOf().getIdToValueMap();
 82  
         }
 83  117
         else if (identifier.equals(DataTypes.OBJ_INTERPOLATION_METHOD))
 84  
         {
 85  0
             valueToKey = InterpolationTypes.getInstanceOf().getValueToIdMap();
 86  0
             keyToValue = InterpolationTypes.getInstanceOf().getIdToValueMap();
 87  
         }
 88  117
         else if (identifier.equals(DataTypes.OBJ_PICTURE_TYPE))
 89  
         {
 90  111
             valueToKey = PictureTypes.getInstanceOf().getValueToIdMap();
 91  111
             keyToValue = PictureTypes.getInstanceOf().getIdToValueMap();
 92  
 
 93  
             //Issue #224 Values should map, but have examples where they dont, this is a workaround
 94  111
             hasEmptyValue = true;
 95  
         }
 96  6
         else if (identifier.equals(DataTypes.OBJ_TYPE_OF_EVENT))
 97  
         {
 98  0
             valueToKey = EventTimingTypes.getInstanceOf().getValueToIdMap();
 99  0
             keyToValue = EventTimingTypes.getInstanceOf().getIdToValueMap();
 100  
         }
 101  6
         else if (identifier.equals(DataTypes.OBJ_TIME_STAMP_FORMAT))
 102  
         {
 103  3
             valueToKey = EventTimingTimestampTypes.getInstanceOf().getValueToIdMap();
 104  3
             keyToValue = EventTimingTimestampTypes.getInstanceOf().getIdToValueMap();
 105  
         }
 106  3
         else if (identifier.equals(DataTypes.OBJ_TYPE_OF_CHANNEL))
 107  
         {
 108  0
             valueToKey = ChannelTypes.getInstanceOf().getValueToIdMap();
 109  0
             keyToValue = ChannelTypes.getInstanceOf().getIdToValueMap();
 110  
         }
 111  3
         else if (identifier.equals(DataTypes.OBJ_RECIEVED_AS))
 112  
         {
 113  0
             valueToKey = ReceivedAsTypes.getInstanceOf().getValueToIdMap();
 114  0
             keyToValue = ReceivedAsTypes.getInstanceOf().getIdToValueMap();
 115  
         }
 116  3
         else if (identifier.equals(DataTypes.OBJ_CONTENT_TYPE))
 117  
         {
 118  3
             valueToKey = SynchronisedLyricsContentType.getInstanceOf().getValueToIdMap();
 119  3
             keyToValue = SynchronisedLyricsContentType.getInstanceOf().getIdToValueMap();
 120  
         }
 121  
         else
 122  
         {
 123  0
             throw new IllegalArgumentException("Hashmap identifier not defined in this class: " + identifier);
 124  
         }
 125  2057
     }
 126  
 
 127  
     public NumberHashMap(NumberHashMap copyObject)
 128  
     {
 129  1154
         super(copyObject);
 130  
 
 131  1154
         this.hasEmptyValue = copyObject.hasEmptyValue;
 132  
 
 133  
         // we don't need to clone/copy the maps here because they are static
 134  1154
         this.keyToValue = copyObject.keyToValue;
 135  1154
         this.valueToKey = copyObject.valueToKey;
 136  1154
     }
 137  
 
 138  
     /**
 139  
      * @return the key to value map
 140  
      */
 141  
     public Map<Integer, String> getKeyToValue()
 142  
     {
 143  0
         return keyToValue;
 144  
     }
 145  
 
 146  
     /**
 147  
      * @return the value to key map
 148  
      */
 149  
     public Map<String, Integer> getValueToKey()
 150  
     {
 151  0
         return valueToKey;
 152  
     }
 153  
 
 154  
     /**
 155  
      * @param value
 156  
      */
 157  
     public void setValue(Object value)
 158  
     {
 159  1831
         if (value instanceof Byte)
 160  
         {
 161  1806
             this.value = (long) ((Byte) value).byteValue();
 162  
         }
 163  25
         else if (value instanceof Short)
 164  
         {
 165  0
             this.value = (long) ((Short) value).shortValue();
 166  
         }
 167  25
         else if (value instanceof Integer)
 168  
         {
 169  13
             this.value = (long) ((Integer) value).intValue();
 170  
         }
 171  
         else
 172  
         {
 173  12
             this.value = value;
 174  
         }
 175  1831
     }
 176  
 
 177  
     /**
 178  
      * @param obj
 179  
      * @return
 180  
      */
 181  
     public boolean equals(Object obj)
 182  
     {
 183  1
         if ((obj instanceof NumberHashMap) == false)
 184  
         {
 185  0
             return false;
 186  
         }
 187  
 
 188  1
         NumberHashMap object = (NumberHashMap) obj;
 189  
 
 190  1
         if (this.hasEmptyValue != object.hasEmptyValue)
 191  
         {
 192  0
             return false;
 193  
         }
 194  
 
 195  1
         if (this.keyToValue == null)
 196  
         {
 197  0
             if (object.keyToValue != null)
 198  
             {
 199  0
                 return false;
 200  
             }
 201  
         }
 202  
         else
 203  
         {
 204  1
             if (this.keyToValue.equals(object.keyToValue) == false)
 205  
             {
 206  0
                 return false;
 207  
             }
 208  
         }
 209  
 
 210  1
         if (this.valueToKey == null)
 211  
         {
 212  0
             if (object.valueToKey != null)
 213  
             {
 214  0
                 return false;
 215  
             }
 216  
         }
 217  
         else
 218  
         {
 219  1
             if (this.valueToKey.equals(object.valueToKey) == false)
 220  
             {
 221  0
                 return false;
 222  
             }
 223  
         }
 224  
 
 225  1
         return super.equals(obj);
 226  
     }
 227  
 
 228  
     /**
 229  
      * @return
 230  
      */
 231  
     public Iterator<String> iterator()
 232  
     {
 233  0
         if (keyToValue == null)
 234  
         {
 235  0
             return null;
 236  
         }
 237  
         else
 238  
         {
 239  
             // put them in a treeset first to sort them
 240  0
             TreeSet<String> treeSet = new TreeSet<String>(keyToValue.values());
 241  
 
 242  0
             if (hasEmptyValue)
 243  
             {
 244  0
                 treeSet.add("");
 245  
             }
 246  
 
 247  0
             return treeSet.iterator();
 248  
         }
 249  
     }
 250  
 
 251  
     /**
 252  
      * Read the key from the buffer.
 253  
      *
 254  
      * @param arr
 255  
      * @param offset
 256  
      * @throws InvalidDataTypeException if emptyValues are not allowed and the eky was invalid.
 257  
      */
 258  
     public void readByteArray(byte[] arr, int offset) throws InvalidDataTypeException
 259  
     {
 260  1523
         super.readByteArray(arr, offset);
 261  
 
 262  
         //Mismatch:Superclass uses Long, but maps expect Integer
 263  1522
         Integer intValue = ((Long) value).intValue();
 264  1522
         if (!keyToValue.containsKey(intValue))
 265  
         {
 266  2
             if (hasEmptyValue == false)
 267  
             {
 268  1
                 throw new InvalidDataTypeException(ErrorMessage.MP3_REFERENCE_KEY_INVALID.getMsg(identifier, intValue));
 269  
             }
 270  1
             else if (identifier.equals(DataTypes.OBJ_PICTURE_TYPE))
 271  
             {
 272  1
                 logger.warning(ErrorMessage.MP3_PICTURE_TYPE_INVALID.getMsg(value));
 273  
             }
 274  
         }
 275  1521
     }
 276  
 
 277  
     /**
 278  
      * @return
 279  
      */
 280  
     public String toString()
 281  
     {
 282  4
         if (value == null)
 283  
         {
 284  0
             return "";
 285  
         }
 286  4
         else if (keyToValue.get(value) == null)
 287  
         {
 288  4
             return "";
 289  
         }
 290  
         else
 291  
         {
 292  0
             return keyToValue.get(value).toString();
 293  
         }
 294  
     }
 295  
 }