Coverage Report - org.jaudiotagger.tag.datatype.NumberHashMap
 
Classes in this File Line Coverage Branch Coverage Complexity
NumberHashMap
63%
54/85
55%
30/54
5.222
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: NumberHashMap.java 836 2009-11-12 15:44:07Z paultaylor $
 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  14880
     private Map<Integer, String> keyToValue = null;
 47  
 
 48  
     /**
 49  
      * value to key map
 50  
      */
 51  14880
     private Map<String, Integer> valueToKey = null;
 52  
 
 53  
     /**
 54  
      *
 55  
      */
 56  14880
     private boolean hasEmptyValue = false;
 57  
 
 58  
 
 59  
     /**
 60  
      * Creates a new ObjectNumberHashMap datatype.
 61  
      *
 62  
      * @param identifier
 63  
      * @param frameBody
 64  
      * @param size
 65  
      * @throws IllegalArgumentException
 66  
      */
 67  
     public NumberHashMap(String identifier, AbstractTagFrameBody frameBody, int size)
 68  
     {
 69  9451
         super(identifier, frameBody, size);
 70  
 
 71  9451
         if (identifier.equals(DataTypes.OBJ_GENRE))
 72  
         {
 73  0
             valueToKey = GenreTypes.getInstanceOf().getValueToIdMap();
 74  0
             keyToValue = GenreTypes.getInstanceOf().getIdToValueMap();
 75  
 
 76  
             //genres can be an id or literal value
 77  0
             hasEmptyValue = true;
 78  
         }
 79  9451
         else if (identifier.equals(DataTypes.OBJ_TEXT_ENCODING))
 80  
         {
 81  8965
             valueToKey = TextEncoding.getInstanceOf().getValueToIdMap();
 82  8965
             keyToValue = TextEncoding.getInstanceOf().getIdToValueMap();
 83  
         }
 84  486
         else if (identifier.equals(DataTypes.OBJ_INTERPOLATION_METHOD))
 85  
         {
 86  0
             valueToKey = InterpolationTypes.getInstanceOf().getValueToIdMap();
 87  0
             keyToValue = InterpolationTypes.getInstanceOf().getIdToValueMap();
 88  
         }
 89  486
         else if (identifier.equals(DataTypes.OBJ_PICTURE_TYPE))
 90  
         {
 91  462
             valueToKey = PictureTypes.getInstanceOf().getValueToIdMap();
 92  462
             keyToValue = PictureTypes.getInstanceOf().getIdToValueMap();
 93  
 
 94  
             //Issue #224 Values should map, but have examples where they dont, this is a workaround
 95  462
             hasEmptyValue = true;
 96  
         }
 97  24
         else if (identifier.equals(DataTypes.OBJ_TYPE_OF_EVENT))
 98  
         {
 99  0
             valueToKey = EventTimingTypes.getInstanceOf().getValueToIdMap();
 100  0
             keyToValue = EventTimingTypes.getInstanceOf().getIdToValueMap();
 101  
         }
 102  24
         else if (identifier.equals(DataTypes.OBJ_TIME_STAMP_FORMAT))
 103  
         {
 104  12
             valueToKey = EventTimingTimestampTypes.getInstanceOf().getValueToIdMap();
 105  12
             keyToValue = EventTimingTimestampTypes.getInstanceOf().getIdToValueMap();
 106  
         }
 107  12
         else if (identifier.equals(DataTypes.OBJ_TYPE_OF_CHANNEL))
 108  
         {
 109  0
             valueToKey = ChannelTypes.getInstanceOf().getValueToIdMap();
 110  0
             keyToValue = ChannelTypes.getInstanceOf().getIdToValueMap();
 111  
         }
 112  12
         else if (identifier.equals(DataTypes.OBJ_RECIEVED_AS))
 113  
         {
 114  0
             valueToKey = ReceivedAsTypes.getInstanceOf().getValueToIdMap();
 115  0
             keyToValue = ReceivedAsTypes.getInstanceOf().getIdToValueMap();
 116  
         }
 117  12
         else if (identifier.equals(DataTypes.OBJ_CONTENT_TYPE))
 118  
         {
 119  12
             valueToKey = SynchronisedLyricsContentType.getInstanceOf().getValueToIdMap();
 120  12
             keyToValue = SynchronisedLyricsContentType.getInstanceOf().getIdToValueMap();
 121  
         }
 122  
         else
 123  
         {
 124  0
             throw new IllegalArgumentException("Hashmap identifier not defined in this class: " + identifier);
 125  
         }
 126  9451
     }
 127  
 
 128  
     public NumberHashMap(NumberHashMap copyObject)
 129  
     {
 130  5429
         super(copyObject);
 131  
 
 132  5429
         this.hasEmptyValue = copyObject.hasEmptyValue;
 133  
 
 134  
         // we don't need to clone/copy the maps here because they are static
 135  5429
         this.keyToValue = copyObject.keyToValue;
 136  5429
         this.valueToKey = copyObject.valueToKey;
 137  5429
     }
 138  
 
 139  
     /**
 140  
      * @return the key to value map
 141  
      */
 142  
     public Map<Integer, String> getKeyToValue()
 143  
     {
 144  0
         return keyToValue;
 145  
     }
 146  
 
 147  
     /**
 148  
      * @return the value to key map
 149  
      */
 150  
     public Map<String, Integer> getValueToKey()
 151  
     {
 152  0
         return valueToKey;
 153  
     }
 154  
 
 155  
     /**
 156  
      * @param value
 157  
      */
 158  
     public void setValue(Object value)
 159  
     {
 160  8795
         if (value instanceof Byte)
 161  
         {
 162  8682
             this.value = (long) ((Byte) value).byteValue();
 163  
         }
 164  113
         else if (value instanceof Short)
 165  
         {
 166  0
             this.value = (long) ((Short) value).shortValue();
 167  
         }
 168  113
         else if (value instanceof Integer)
 169  
         {
 170  65
             this.value = (long) ((Integer) value).intValue();
 171  
         }
 172  
         else
 173  
         {
 174  48
             this.value = value;
 175  
         }
 176  8795
     }
 177  
 
 178  
     /**
 179  
      * @param obj
 180  
      * @return
 181  
      */
 182  
     public boolean equals(Object obj)
 183  
     {
 184  4
         if (!(obj instanceof NumberHashMap))
 185  
         {
 186  0
             return false;
 187  
         }
 188  
 
 189  4
         NumberHashMap object = (NumberHashMap) obj;
 190  
 
 191  4
         if (this.hasEmptyValue != object.hasEmptyValue)
 192  
         {
 193  0
             return false;
 194  
         }
 195  
 
 196  4
         if (this.keyToValue == null)
 197  
         {
 198  0
             if (object.keyToValue != null)
 199  
             {
 200  0
                 return false;
 201  
             }
 202  
         }
 203  
         else
 204  
         {
 205  4
             if (!this.keyToValue.equals(object.keyToValue))
 206  
             {
 207  0
                 return false;
 208  
             }
 209  
         }
 210  
 
 211  4
         if (this.valueToKey == null)
 212  
         {
 213  0
             if (object.valueToKey != null)
 214  
             {
 215  0
                 return false;
 216  
             }
 217  
         }
 218  
         else
 219  
         {
 220  4
             if (!this.valueToKey.equals(object.valueToKey))
 221  
             {
 222  0
                 return false;
 223  
             }
 224  
         }
 225  
 
 226  4
         return super.equals(obj);
 227  
     }
 228  
 
 229  
     /**
 230  
      * @return
 231  
      */
 232  
     public Iterator<String> iterator()
 233  
     {
 234  0
         if (keyToValue == null)
 235  
         {
 236  0
             return null;
 237  
         }
 238  
         else
 239  
         {
 240  
             // put them in a treeset first to sort them
 241  0
             TreeSet<String> treeSet = new TreeSet<String>(keyToValue.values());
 242  
 
 243  0
             if (hasEmptyValue)
 244  
             {
 245  0
                 treeSet.add("");
 246  
             }
 247  
 
 248  0
             return treeSet.iterator();
 249  
         }
 250  
     }
 251  
 
 252  
     /**
 253  
      * Read the key from the buffer.
 254  
      *
 255  
      * @param arr
 256  
      * @param offset
 257  
      * @throws InvalidDataTypeException if emptyValues are not allowed and the eky was invalid.
 258  
      */
 259  
     public void readByteArray(byte[] arr, int offset) throws InvalidDataTypeException
 260  
     {
 261  6683
         super.readByteArray(arr, offset);
 262  
 
 263  
         //Mismatch:Superclass uses Long, but maps expect Integer
 264  6682
         Integer intValue = ((Long) value).intValue();
 265  6682
         if (!keyToValue.containsKey(intValue))
 266  
         {
 267  13
             if (!hasEmptyValue)
 268  
             {
 269  12
                 throw new InvalidDataTypeException(ErrorMessage.MP3_REFERENCE_KEY_INVALID.getMsg(identifier, intValue));
 270  
             }
 271  1
             else if (identifier.equals(DataTypes.OBJ_PICTURE_TYPE))
 272  
             {
 273  1
                 logger.warning(ErrorMessage.MP3_PICTURE_TYPE_INVALID.getMsg(value));
 274  
             }
 275  
         }
 276  6670
     }
 277  
 
 278  
     /**
 279  
      * @return
 280  
      */
 281  
     public String toString()
 282  
     {
 283  16
         if (value == null)
 284  
         {
 285  0
             return "";
 286  
         }
 287  16
         else if (keyToValue.get(value) == null)
 288  
         {
 289  16
             return "";
 290  
         }
 291  
         else
 292  
         {
 293  0
             return keyToValue.get(value);
 294  
         }
 295  
     }
 296  
 }