Coverage Report - org.jaudiotagger.tag.reference.MusicalKey
 
Classes in this File Line Coverage Branch Coverage Complexity
MusicalKey
100%
39/39
91%
20/22
0
 
 1  
 package org.jaudiotagger.tag.reference;
 2  
 
 3  
 import java.util.EnumSet;
 4  
 import java.util.HashMap;
 5  
 
 6  
 /**
 7  
  * Musical key used by the Key tagFieldKey
 8  
  *
 9  
  * It is not enforced but can be used to verify the Musical key according to the ID3 specification of the TKEY field
 10  
  */
 11  4
 public enum MusicalKey
 12  
 {
 13  1
     NOTE_A("A"),
 14  1
     NOTE_B("B"),
 15  1
     NOTE_C("C"),
 16  1
     NOTE_D("D"),
 17  1
     NOTE_E("E"),
 18  1
     NOTE_F("F"),
 19  1
     NOTE_G("G"),
 20  1
     FLAT("b"),
 21  1
     SHARP("#"),
 22  1
     MINOR("m"),
 23  1
     OFF_KEY("o");
 24  
 
 25  
     private String value;
 26  
     MusicalKey(String value)
 27  11
     {
 28  11
         this.value=value;
 29  11
     }
 30  
 
 31  
     public String getValue()
 32  
     {
 33  15
         return value;
 34  
     }
 35  
 
 36  
     private static final int MAX_KEY_LENGTH = 3;
 37  
 
 38  
     private static EnumSet<MusicalKey> groundKey;
 39  
     private static EnumSet<MusicalKey> halfKey;
 40  
     private final static HashMap<String, MusicalKey> groundKeyMap;
 41  
     private final static HashMap<String, MusicalKey> halfKeyMap;
 42  
 
 43  
     static
 44  
     {
 45  1
         groundKey = EnumSet.of(NOTE_A,NOTE_B,NOTE_C,NOTE_D,NOTE_E,NOTE_F,NOTE_G);
 46  1
         groundKeyMap = new HashMap<String, MusicalKey>(MusicalKey.values().length);
 47  1
         for (MusicalKey curr : groundKey)
 48  
         {
 49  7
             groundKeyMap.put(curr.getValue(), curr);
 50  
         }
 51  1
         halfKey   = EnumSet.of(FLAT,SHARP,MINOR);
 52  1
         halfKeyMap = new HashMap<String, MusicalKey>(MusicalKey.values().length);
 53  1
         for (MusicalKey curr : halfKey)
 54  
         {
 55  3
             halfKeyMap.put(curr.getValue(), curr);
 56  
         }
 57  1
     }
 58  
 
 59  
     public static boolean isValid(String musicalKey)
 60  
     {
 61  11
         if(musicalKey==null || musicalKey.length()>MAX_KEY_LENGTH || musicalKey.length()==0)
 62  
         {
 63  1
             return false;
 64  
         }
 65  
 
 66  
 
 67  10
         if(musicalKey.length()==1)
 68  
         {
 69  3
             if(musicalKey.equals(OFF_KEY.getValue()))
 70  
             {
 71  1
                 return true;
 72  
             }
 73  
         }
 74  
 
 75  9
         if(!groundKeyMap.containsKey(musicalKey.substring(0,1)))
 76  
         {
 77  1
             return false;
 78  
         }
 79  
 
 80  8
         if(musicalKey.length()==2||musicalKey.length()==3)
 81  
         {
 82  6
             if(!halfKeyMap.containsKey(musicalKey.substring(1,2)))
 83  
             {
 84  1
                 return false;
 85  
             }
 86  
         }
 87  
 
 88  7
         if(musicalKey.length()==3)
 89  
         {
 90  2
             if(!musicalKey.substring(2,3).equals(MINOR.getValue()))
 91  
             {
 92  1
                 return false;
 93  
             }
 94  
         }
 95  6
         return true;
 96  
     }
 97  
 }