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