Coverage Report - org.jaudiotagger.tag.mp4.field.Mp4DiscNoField
 
Classes in this File Line Coverage Branch Coverage Complexity
Mp4DiscNoField
64%
30/47
60%
3/5
0
 
 1  
 package org.jaudiotagger.tag.mp4.field;
 2  
 
 3  
 import org.jaudiotagger.audio.mp4.atom.Mp4BoxHeader;
 4  
 import org.jaudiotagger.tag.FieldDataInvalidException;
 5  
 import org.jaudiotagger.tag.mp4.Mp4FieldKey;
 6  
 import org.jaudiotagger.tag.mp4.atom.Mp4DataBox;
 7  
 
 8  
 import java.io.UnsupportedEncodingException;
 9  
 import java.nio.ByteBuffer;
 10  
 import java.util.ArrayList;
 11  
 
 12  
 /**
 13  
  * Represents the Disc No field
 14  
  * <p/>
 15  
  * <p>For some reason uses an array of three numbers, but only the last two are of use for display purposes
 16  
  */
 17  
 public class Mp4DiscNoField extends Mp4TagTextNumberField
 18  
 {
 19  
     private static final int NONE_VALUE_INDEX = 0;
 20  
     private static final int DISC_NO_INDEX = 1;
 21  
     private static final int DISC_TOTAL_INDEX = 2;
 22  
 
 23  
     /**
 24  
      * Create new Track Field parsing the String for the trackno/total
 25  
      *
 26  
      * @param discValue
 27  
      */
 28  
     public Mp4DiscNoField(String discValue) throws FieldDataInvalidException
 29  
     {
 30  6
         super(Mp4FieldKey.DISCNUMBER.getFieldName(), discValue);
 31  
 
 32  6
         numbers = new ArrayList<Short>();
 33  6
         numbers.add(new Short("0"));
 34  
 
 35  6
         String values[] = discValue.split("/");
 36  6
         switch (values.length)
 37  
         {
 38  
             case 1:
 39  
 
 40  
                 try
 41  
                 {
 42  6
                     numbers.add(Short.parseShort(values[0]));
 43  
                 }
 44  0
                 catch (NumberFormatException nfe)
 45  
                 {
 46  0
                     throw new FieldDataInvalidException("Value of:" + values[0] + " is invalid for field:" + id);
 47  6
                 }
 48  6
                 numbers.add(new Short("0"));
 49  6
                 break;
 50  
 
 51  
             case 2:
 52  
                 try
 53  
                 {
 54  0
                     numbers.add(Short.parseShort(values[0]));
 55  
                 }
 56  0
                 catch (NumberFormatException nfe)
 57  
                 {
 58  0
                     throw new FieldDataInvalidException("Value of:" + values[0] + " is invalid for field:" + id);
 59  0
                 }
 60  
                 try
 61  
                 {
 62  0
                     numbers.add(Short.parseShort(values[1]));
 63  
                 }
 64  0
                 catch (NumberFormatException nfe)
 65  
                 {
 66  0
                     throw new FieldDataInvalidException("Value of:" + values[1] + " is invalid for field:" + id);
 67  0
                 }
 68  
                 break;
 69  
 
 70  
             default:
 71  0
                 throw new FieldDataInvalidException("Value is invalid for field:" + id);
 72  
         }
 73  6
     }
 74  
 
 75  
 
 76  
     /**
 77  
      * Create new Disc no  Field with only discNo
 78  
      *
 79  
      * @param discNo
 80  
      */
 81  
     public Mp4DiscNoField(int discNo)
 82  
     {
 83  0
         super(Mp4FieldKey.DISCNUMBER.getFieldName(), String.valueOf(discNo));
 84  0
         numbers = new ArrayList<Short>();
 85  0
         numbers.add(new Short("0"));
 86  0
         numbers.add((short) discNo);
 87  0
         numbers.add(new Short("0"));
 88  0
     }
 89  
 
 90  
     /**
 91  
      * Create new Disc No Field with disc No and total tracks
 92  
      *
 93  
      * @param discNo
 94  
      * @param total
 95  
      */
 96  
     public Mp4DiscNoField(int discNo, int total)
 97  
     {
 98  2
         super(Mp4FieldKey.DISCNUMBER.getFieldName(), String.valueOf(discNo));
 99  2
         numbers = new ArrayList<Short>();
 100  2
         numbers.add(new Short("0"));
 101  2
         numbers.add((short) discNo);
 102  2
         numbers.add((short) total);
 103  2
     }
 104  
 
 105  
     public Mp4DiscNoField(String id, ByteBuffer data) throws UnsupportedEncodingException
 106  
     {
 107  59
         super(id, data);
 108  59
     }
 109  
 
 110  
     protected void build(ByteBuffer data) throws UnsupportedEncodingException
 111  
     {
 112  
         //Data actually contains a 'Data' Box so process data using this
 113  59
         Mp4BoxHeader header = new Mp4BoxHeader(data);
 114  59
         Mp4DataBox databox = new Mp4DataBox(header, data);
 115  59
         dataSize = header.getDataLength();
 116  59
         numbers = databox.getNumbers();
 117  
 
 118  
         //Disc number always hold four values, we can discard the first one and last one, the second one is the disc no
 119  
         //and the third is the total no of discs so only use if not zero
 120  59
         StringBuffer sb = new StringBuffer();
 121  59
         sb.append(numbers.get(DISC_NO_INDEX));
 122  59
         if (numbers.get(DISC_TOTAL_INDEX) > 0)
 123  
         {
 124  49
             sb.append("/" + numbers.get(DISC_TOTAL_INDEX));
 125  
         }
 126  59
         content = sb.toString();
 127  59
     }
 128  
 
 129  
     /**
 130  
      * @return
 131  
      */
 132  
     public Short getDiscNo()
 133  
     {
 134  3
         return numbers.get(DISC_NO_INDEX);
 135  
     }
 136  
 
 137  
     /**
 138  
      * @return
 139  
      */
 140  
     public Short getDiscTotal()
 141  
     {
 142  3
         return numbers.get(DISC_TOTAL_INDEX);
 143  
     }
 144  
 }