Coverage Report - org.jaudiotagger.tag.mp4.field.Mp4DiscNoField
 
Classes in this File Line Coverage Branch Coverage Complexity
Mp4DiscNoField
50%
26/51
40%
2/5
2.111
 
 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>Contains some reserved fields that we currently ignore
 16  
  *
 17  
  * Reserved:2 bytes
 18  
  * Disc Number:2 bytes
 19  
  * Total no of Discs:2 bytes
 20  
  * </p>
 21  
  */
 22  
 public class Mp4DiscNoField extends Mp4TagTextNumberField
 23  
 {
 24  
     private static final int NONE_VALUE_INDEX = 0;
 25  
     private static final int DISC_NO_INDEX = 1;
 26  
     private static final int DISC_TOTAL_INDEX = 2;
 27  
 
 28  
     /**
 29  
      * Create new Disc Field parsing the String for the discno/total
 30  
      *
 31  
      * @param discValue
 32  
      * @throws org.jaudiotagger.tag.FieldDataInvalidException
 33  
      */
 34  
     public Mp4DiscNoField(String discValue) throws FieldDataInvalidException
 35  
     {
 36  0
         super(Mp4FieldKey.DISCNUMBER.getFieldName(), discValue);
 37  
 
 38  0
         numbers = new ArrayList<Short>();
 39  0
         numbers.add(new Short("0"));
 40  
 
 41  0
         String values[] = discValue.split("/");
 42  0
         switch (values.length)
 43  
         {
 44  
             case 1:
 45  
 
 46  
                 try
 47  
                 {
 48  0
                     numbers.add(Short.parseShort(values[0]));
 49  
                 }
 50  0
                 catch (NumberFormatException nfe)
 51  
                 {
 52  0
                     throw new FieldDataInvalidException("Value of:" + values[0] + " is invalid for field:" + id);
 53  0
                 }
 54  0
                 numbers.add(new Short("0"));
 55  0
                 break;
 56  
 
 57  
             case 2:
 58  
                 try
 59  
                 {
 60  0
                     numbers.add(Short.parseShort(values[0]));
 61  
                 }
 62  0
                 catch (NumberFormatException nfe)
 63  
                 {
 64  0
                     throw new FieldDataInvalidException("Value of:" + values[0] + " is invalid for field:" + id);
 65  0
                 }
 66  
                 try
 67  
                 {
 68  0
                     numbers.add(Short.parseShort(values[1]));
 69  
                 }
 70  0
                 catch (NumberFormatException nfe)
 71  
                 {
 72  0
                     throw new FieldDataInvalidException("Value of:" + values[1] + " is invalid for field:" + id);
 73  0
                 }
 74  
                 break;
 75  
 
 76  
             default:
 77  0
                 throw new FieldDataInvalidException("Value is invalid for field:" + id);
 78  
         }
 79  0
     }
 80  
 
 81  
 
 82  
     /**
 83  
      * Create new Disc No field with only discNo
 84  
      *
 85  
      * @param discNo
 86  
      */
 87  
     public Mp4DiscNoField(int discNo)
 88  
     {
 89  24
         super(Mp4FieldKey.DISCNUMBER.getFieldName(), String.valueOf(discNo));
 90  24
         numbers = new ArrayList<Short>();
 91  24
         numbers.add(new Short("0"));
 92  24
         numbers.add((short) discNo);
 93  24
         numbers.add(new Short("0"));
 94  24
     }
 95  
 
 96  
     /**
 97  
      * Create new Disc No Field with Disc No and total number of discs
 98  
      *
 99  
      * @param discNo
 100  
      * @param total
 101  
      */
 102  
     public Mp4DiscNoField(int discNo, int total)
 103  
     {
 104  24
         super(Mp4FieldKey.DISCNUMBER.getFieldName(), String.valueOf(discNo));
 105  24
         numbers = new ArrayList<Short>();
 106  24
         numbers.add(new Short("0"));
 107  24
         numbers.add((short) discNo);
 108  24
         numbers.add((short) total);
 109  24
     }
 110  
 
 111  
     public Mp4DiscNoField(String id, ByteBuffer data) throws UnsupportedEncodingException
 112  
     {
 113  209
         super(id, data);
 114  209
     }
 115  
 
 116  
     protected void build(ByteBuffer data) throws UnsupportedEncodingException
 117  
     {
 118  
         //Data actually contains a 'Data' Box so process data using this
 119  209
         Mp4BoxHeader header = new Mp4BoxHeader(data);
 120  209
         Mp4DataBox databox = new Mp4DataBox(header, data);
 121  209
         dataSize = header.getDataLength();
 122  209
         numbers = databox.getNumbers();
 123  
 
 124  
         //Disc number always hold four values, we can discard the first one and last one, the second one is the disc no
 125  
         //and the third is the total no of discs so only use if not zero
 126  209
         StringBuffer sb = new StringBuffer();
 127  209
         sb.append(numbers.get(DISC_NO_INDEX));
 128  209
         if (numbers.get(DISC_TOTAL_INDEX) > 0)
 129  
         {
 130  179
             sb.append("/").append(numbers.get(DISC_TOTAL_INDEX));
 131  
         }
 132  209
         content = sb.toString();
 133  209
     }
 134  
 
 135  
     /**
 136  
      * @return
 137  
      */
 138  
     public Short getDiscNo()
 139  
     {
 140  236
         return numbers.get(DISC_NO_INDEX);
 141  
     }
 142  
 
 143  
     /**
 144  
      * Set Disc No
 145  
      *
 146  
      * @param discNo
 147  
      */
 148  
     public void setDiscNo(int discNo)
 149  
     {
 150  0
         numbers.set(DISC_NO_INDEX, (short) discNo);
 151  0
     }
 152  
 
 153  
     /**
 154  
      * @return
 155  
      */
 156  
     public Short getDiscTotal()
 157  
     {
 158  144
         return numbers.get(DISC_TOTAL_INDEX);
 159  
     }
 160  
 
 161  
     /**
 162  
      * Set total number of discs
 163  
      *
 164  
      * @param discTotal
 165  
      */
 166  
     public void setDiscTotal(int discTotal)
 167  
     {
 168  0
         numbers.set(DISC_TOTAL_INDEX, (short) discTotal);
 169  0
     }
 170  
 }