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