Coverage Report - org.jaudiotagger.tag.datatype.StringHashMap
 
Classes in this File Line Coverage Branch Coverage Complexity
StringHashMap
56%
28/50
36%
11/30
3.778
 
 1  
 /**
 2  
  *  @author : Paul Taylor
 3  
  *  @author : Eric Farng
 4  
  *
 5  
  *  Version @version:$Id: StringHashMap.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.tag.id3.AbstractTagFrameBody;
 27  
 import org.jaudiotagger.tag.id3.valuepair.TextEncoding;
 28  
 import org.jaudiotagger.tag.reference.Languages;
 29  
 
 30  
 import java.util.Iterator;
 31  
 import java.util.Map;
 32  
 import java.util.TreeSet;
 33  
 
 34  
 
 35  
 /**
 36  
  * Represents a String thats acts as a key into an enumeration of values. The String will be encoded
 37  
  * using the default encoding regardless of what encoding may be specified in the framebody
 38  
  */
 39  
 public class StringHashMap extends StringFixedLength implements HashMapInterface<String, String>
 40  
 {
 41  
 
 42  
     /**
 43  
      *
 44  
      */
 45  1252
     Map<String, String> keyToValue = null;
 46  
 
 47  
     /**
 48  
      *
 49  
      */
 50  1252
     Map<String, String> valueToKey = null;
 51  
 
 52  
     /**
 53  
      *
 54  
      */
 55  1252
     boolean hasEmptyValue = false;
 56  
 
 57  
     /**
 58  
      * Creates a new ObjectStringHashMap datatype.
 59  
      *
 60  
      * @param identifier
 61  
      * @param frameBody
 62  
      * @param size
 63  
      * @throws IllegalArgumentException
 64  
      */
 65  
     public StringHashMap(String identifier, AbstractTagFrameBody frameBody, int size)
 66  
     {
 67  772
         super(identifier, frameBody, size);
 68  
 
 69  772
         if (identifier.equals(DataTypes.OBJ_LANGUAGE))
 70  
         {
 71  772
             valueToKey = Languages.getInstanceOf().getValueToIdMap();
 72  772
             keyToValue = Languages.getInstanceOf().getIdToValueMap();
 73  
         }
 74  
         else
 75  
         {
 76  0
             throw new IllegalArgumentException("Hashmap identifier not defined in this class: " + identifier);
 77  
         }
 78  772
     }
 79  
 
 80  
     public StringHashMap(StringHashMap copyObject)
 81  
     {
 82  480
         super(copyObject);
 83  
 
 84  480
         this.hasEmptyValue = copyObject.hasEmptyValue;
 85  480
         this.keyToValue = copyObject.keyToValue;
 86  480
         this.valueToKey = copyObject.valueToKey;
 87  480
     }
 88  
 
 89  
     /**
 90  
      * @return
 91  
      */
 92  
     public Map<String, String> getKeyToValue()
 93  
     {
 94  0
         return keyToValue;
 95  
     }
 96  
 
 97  
     /**
 98  
      * @return
 99  
      */
 100  
     public Map<String, String> getValueToKey()
 101  
     {
 102  0
         return valueToKey;
 103  
     }
 104  
 
 105  
     /**
 106  
      * @param value
 107  
      */
 108  
     public void setValue(Object value)
 109  
     {
 110  176
         if (value instanceof String)
 111  
         {
 112  
             //Issue #273 temporary hack for MM
 113  172
             if(value.equals("XXX"))
 114  
             {
 115  8
                 this.value=value.toString();
 116  
             }
 117  
             else
 118  
             {
 119  164
                 this.value = ((String) value).toLowerCase();
 120  
             }
 121  
         }
 122  
         else
 123  
         {
 124  4
             this.value = value;
 125  
         }
 126  176
     }
 127  
 
 128  
     /**
 129  
      * @param obj
 130  
      * @return
 131  
      */
 132  
     public boolean equals(Object obj)
 133  
     {
 134  4
         if (!(obj instanceof StringHashMap))
 135  
         {
 136  0
             return false;
 137  
         }
 138  
 
 139  4
         StringHashMap object = (StringHashMap) obj;
 140  
 
 141  4
         if (this.hasEmptyValue != object.hasEmptyValue)
 142  
         {
 143  0
             return false;
 144  
         }
 145  
 
 146  4
         if (this.keyToValue == null)
 147  
         {
 148  0
             if (object.keyToValue != null)
 149  
             {
 150  0
                 return false;
 151  
             }
 152  
         }
 153  
         else
 154  
         {
 155  4
             if (!this.keyToValue.equals(object.keyToValue))
 156  
             {
 157  0
                 return false;
 158  
             }
 159  
         }
 160  
 
 161  4
         if (this.keyToValue == null)
 162  
         {
 163  0
             if (object.keyToValue != null)
 164  
             {
 165  0
                 return false;
 166  
             }
 167  
         }
 168  
         else
 169  
         {
 170  4
             if (!this.valueToKey.equals(object.valueToKey))
 171  
             {
 172  0
                 return false;
 173  
             }
 174  
         }
 175  
 
 176  4
         return super.equals(obj);
 177  
     }
 178  
 
 179  
     /**
 180  
      * @return
 181  
      */
 182  
     public Iterator<String> iterator()
 183  
     {
 184  0
         if (keyToValue == null)
 185  
         {
 186  0
             return null;
 187  
         }
 188  
         else
 189  
         {
 190  
             // put them in a treeset first to sort them
 191  0
             TreeSet<String> treeSet = new TreeSet<String>(keyToValue.values());
 192  
 
 193  0
             if (hasEmptyValue)
 194  
             {
 195  0
                 treeSet.add("");
 196  
             }
 197  
 
 198  0
             return treeSet.iterator();
 199  
         }
 200  
     }
 201  
 
 202  
     /**
 203  
      * @return
 204  
      */
 205  
     public String toString()
 206  
     {
 207  0
         if (value == null)
 208  
         {
 209  0
             return "";
 210  
         }
 211  0
         else if (keyToValue.get(value) == null)
 212  
         {
 213  0
             return "";
 214  
         }
 215  
         else
 216  
         {
 217  0
             return keyToValue.get(value);
 218  
         }
 219  
     }
 220  
 
 221  
     /**
 222  
      * @return the ISO_8859 encoding for Datatypes of this type
 223  
      */
 224  
     protected String getTextEncodingCharSet()
 225  
     {
 226  1012
         return TextEncoding.CHARSET_ISO_8859_1;
 227  
     }
 228  
 }